英文:
Rotate log file in golang with systemd
问题
使用默认的日志记录功能时发现了两个问题,即日志轮转和在使用systemd运行程序时无法打印日志。因此,我尝试使用以下库:
log4go
log4go似乎是一个完美的日志记录库,因为它提供了最大文件大小和行数进行轮转。然而,当将rotate设置为true时,它确实创建了一个新的日志文件,但是出现了错误,然后应用程序终止。
> 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.
配置:
logger := log4go.NewDefaultLogger(log4go.DEBUG)
logger.AddFilter("log", log4go.FINE, log4go.NewFileLogWriter("/log/app.log", true))
logger.Info("success")
我还修改了现有的库,并将daily设置为true,这样在轮转时文件名上会显示日期。
lumberjack
接下来我尝试了这个库。很高兴发现它不需要做太多的工作,只需添加结构化的日志配置即可。它的工作很好,因为日志文件的大小没有超过指定的参数,但是没有出现新文件创建的迹象。配置如下:
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)
}
我错过了什么地方吗?
英文:
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
的建议是将日志记录到STDOUT,systemd
会自动捕获并存储在日志中,可以通过journalctl -u yourproject.service
来访问。它还会为你处理日志轮转。
要获取更多信息,你可以查看man journalctl
或man journald.conf
。
英文:
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
答案2
得分: 0
伐木工
在不知道property.AppProperties.Logging.Path
和logFile
的值的情况下,很难知道这里出了什么问题。此外,你以WRONLY(只写)和RDWR(读写)的方式打开了文件。
尝试使用os.OpenFile(logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
。
log4go似乎已经不再维护了。但是错误可能来自于这里:
https://github.com/alecthomas/log4go/blob/master/filelog.go#L169
也许你的应用程序中仍然有另一个日志记录实现可能仍在使用该文件?filelog.go
似乎在尝试重命名之前关闭了文件。
12因素方法也许考虑一下这个会有帮助。
https://12factor.net/logs
> 十二因素应用程序不关心其输出流的路由或存储。
并将日志的轮换转移到另一个从stdout读取的进程中,例如systemd、docker、logstash。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论