英文:
Is there a built-in go logger that can roll
问题
有没有内置的Go日志记录器可以在达到文件大小限制时滚动日志文件?
谢谢。
英文:
Is there a built-in Go logger that can roll a log file when it reaches a file size limit?
Thanks.
答案1
得分: 3
不,目前没有内置的记录器具有此功能。当搜索时,您会发现推荐使用_log4go_,但它目前存在一些问题,导致消息丢失(如果主程序退出并且某些消息仍在写入之前的通道缓冲区中)。大多数(如果不是全部)示例中都存在此问题。
另请参阅此问题。
英文:
No, there is no built-in in logger that currently has this feature.
log4go, which you will find recommended when searching, <del>is currently broken.</del> has some issues, that lead to messages getting lost (in case the main program exits and some messages are still in the channelbuffer before being written).
this is present in most if not all the examples.
see also this question
答案2
得分: 2
从syslog包上面来看,这可能不是你真正想要的,标准库中没有这样的东西。
从第三方包中,例如log4go声称具有这个功能。
英文:
Above the syslog package, which probably is not what you really want, no such thing is in the standard library.
From the 3rd party packages, for example log4go claims to have this feature.
答案3
得分: 0
import (
"os"
"github.com/nikandfor/tlog"
"github.com/nikandfor/tlog/rotated"
)
func main() {
f, err := rotated.Create("logfile_template_#.log") // # will be substituted by time of file creation
if err != nil {
panic(err)
}
defer f.Close()
f.MaxSize = 1 << 30 // 1GiB
f.Fallback = os.Stderr // in case of failure to write to file, last chance to save log message
tlog.DefaultLogger = tlog.New(tlog.NewConsoleWriter(f, tlog.LstdFlags))
tlog.Printf("now use it much like %v", "log.Logger")
log.SetOutput(f) // also works for any logger or what ever needs io.Writer
log.Printf("also appears in the log")
}
- logger https://github.com/nikandfor/tlog
- rotated file https://godoc.org/github.com/nikandfor/tlog/rotated
英文:
import (
"os"
"github.com/nikandfor/tlog"
"github.com/nikandfor/tlog/rotated"
)
func main() {
f, err := rotated.Create("logfile_template_#.log") // # will be substituted by time of file creation
if err != nil {
panic(err)
}
defer f.Close()
f.MaxSize = 1 << 30 // 1GiB
f.Fallback = os.Stderr // in case of failure to write to file, last chance to save log message
tlog.DefaultLogger = tlog.New(tlog.NewConsoleWriter(f, tlog.LstdFlags))
tlog.Printf("now use it much like %v", "log.Logger")
log.SetOutput(f) // also works for any logger or what ever needs io.Writer
log.Printf("also appears in the log")
}
- logger https://github.com/nikandfor/tlog
- rotated file https://godoc.org/github.com/nikandfor/tlog/rotated
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论