Was using default log from go but found two issues, which are log rotation and log wasn't being printed when run the program using systemd. So I tried using libraries as follows:
https://github.com/alecthomas/log4go
https://github.com/natefinch/lumberjack
log4go
Seems a perfect library for logging because provides max size and line for rotation. However when set rotate to true, it did create new log file but with error then the app terminated.
FileLogWriter("logs/app.log"): Rotate: rename logs/stream.log logs/app.log.2017-05-21.001: The process cannot access the file because it is being used by another process.
Configurations:
logger:=log4go.NewDefaultLogger(log4go.DEBUG)
logger.AddFilter("log", log4go.FINE, log4go.NewFileLogWriter("/log/app.log", true))
logger.Info("success")
Also modified existing library and set daily to true so when rotate it shows date on the file
lumberjack
Next I tried this library. Was happy to find because there's nothing much to do but add struct log configuration. Worked well as the log file didn't go any bigger that the param specify, but there's no sign of new file created. Configuration
var PrintLog *log.Logger
func main() {
_ = os.Mkdir(property.AppProperties.Logging.Path, os.ModePerm)
f, e := os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if e != nil {
fmt.Printf("error opening file: %v", e)
}
PrintLog = log.New(f, "", log.Ldate|log.Ltime)
l:= &lumberjack.Logger{
Filename: logFile,
MaxSize: 2, // megabytes
MaxBackups: 3,
MaxAge: 20, //days
}
log.SetOutput(l)
}
Where am I missing?
journalctl -u myservice