有没有内置的go日志记录器可以滚动?

huangapple go评论85阅读模式
英文:

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")

}
英文:
import (
    &quot;os&quot;

    &quot;github.com/nikandfor/tlog&quot;
    &quot;github.com/nikandfor/tlog/rotated&quot;
)
    
func main() {
    f, err := rotated.Create(&quot;logfile_template_#.log&quot;) // # will be substituted by time of file creation
    if err != nil {
        panic(err)
    }
    defer f.Close()

    f.MaxSize = 1 &lt;&lt; 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(&quot;now use it much like %v&quot;, &quot;log.Logger&quot;)

    log.SetOutput(f) // also works for any logger or what ever needs io.Writer

    log.Printf(&quot;also appears in the log&quot;)

}

huangapple
  • 本文由 发表于 2013年1月15日 15:13:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/14332773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定