1

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?

1
  • 1
    Systemd logging is about capturing standard outpout. Systemd handles log rotation with journald. In order to get logging stuff in systemd journal, just write information on stdout, then view logs with journalctl -u myservice Commented May 22, 2017 at 7:24

2 Answers 2

1

The recommendation with systemd is to log to STDOUT, which systemd automatically captures and stores in the journal for you, accessible by journalctl -u yourproject.service. It also handles log rotation for you.

For more information, you can review man journalctl or man journald.conf

Sign up to request clarification or add additional context in comments.

Comments

0

lumberjack It's hard to know what's wrong here without knowing the values of property.AppProperties.Logging.Path and logFile. Also, you are opening the file as WRONLY (write only) and RDWR (read-write).

try os.OpenFile(logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)

log4go appears to be unmaintained. But this is probably where the error is coming from: https://github.com/alecthomas/log4go/blob/master/filelog.go#L169

Maybe you still have another logging implementation in your app that could still be using that file? filelog.go seems to close the file before attempting the rename.

12factor way it might also be useful to consider this. https://12factor.net/logs

A twelve-factor app never concerns itself with routing or storage of its output stream.

And offload the rotation of logs to another process that reads from stdout, e.g. systemd, docker, logstash

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.