{"id":13413315,"url":"https://github.com/natefinch/lumberjack","last_synced_at":"2025-05-08T22:18:03.568Z","repository":{"id":37663508,"uuid":"20831114","full_name":"natefinch/lumberjack","owner":"natefinch","description":"lumberjack is a log rolling package for Go","archived":false,"fork":false,"pushed_at":"2024-08-05T20:55:26.000Z","size":75,"stargazers_count":5082,"open_issues_count":99,"forks_count":615,"subscribers_count":68,"default_branch":"v2.0","last_synced_at":"2025-05-08T22:17:53.390Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/natefinch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-06-14T11:55:47.000Z","updated_at":"2025-05-08T14:18:05.000Z","dependencies_parsed_at":"2024-10-22T21:31:32.224Z","dependency_job_id":null,"html_url":"https://github.com/natefinch/lumberjack","commit_stats":{"total_commits":61,"total_committers":12,"mean_commits":5.083333333333333,"dds":"0.19672131147540983","last_synced_commit":"4cb27fcfbb0f35cb48c542c5ea80b7c1d18933d0"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natefinch%2Flumberjack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natefinch%2Flumberjack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natefinch%2Flumberjack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natefinch%2Flumberjack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/natefinch","download_url":"https://codeload.github.com/natefinch/lumberjack/tar.gz/refs/heads/v2.0","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253155004,"owners_count":21862625,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T20:01:37.521Z","updated_at":"2025-05-08T22:18:03.543Z","avatar_url":"https://github.com/natefinch.png","language":"Go","funding_links":[],"categories":["Go","开源类库","Log","Misc","Logging","语言资源库","日誌","\u003cspan id=\"日志-logging\"\u003e日志 Logging\u003c/span\u003e","日志","日志记录","Logging 日志库","Relational Databases"],"sub_categories":["日志","Search and Analytic Databases","go","高級控制台界面","Advanced Console UIs","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","高级控制台界面","检索及分析资料库","SQL 查询语句构建库","交流"],"readme":"# lumberjack  [![GoDoc](https://godoc.org/gopkg.in/natefinch/lumberjack.v2?status.png)](https://godoc.org/gopkg.in/natefinch/lumberjack.v2) [![Build Status](https://travis-ci.org/natefinch/lumberjack.svg?branch=v2.0)](https://travis-ci.org/natefinch/lumberjack) [![Build status](https://ci.appveyor.com/api/projects/status/00gchpxtg4gkrt5d)](https://ci.appveyor.com/project/natefinch/lumberjack) [![Coverage Status](https://coveralls.io/repos/natefinch/lumberjack/badge.svg?branch=v2.0)](https://coveralls.io/r/natefinch/lumberjack?branch=v2.0)\n\n### Lumberjack is a Go package for writing logs to rolling files.\n\nPackage lumberjack provides a rolling logger.\n\nNote that this is v2.0 of lumberjack, and should be imported using gopkg.in\nthusly:\n\n    import \"gopkg.in/natefinch/lumberjack.v2\"\n\nThe package name remains simply lumberjack, and the code resides at\nhttps://github.com/natefinch/lumberjack under the v2.0 branch.\n\nLumberjack is intended to be one part of a logging infrastructure.\nIt is not an all-in-one solution, but instead is a pluggable\ncomponent at the bottom of the logging stack that simply controls the files\nto which logs are written.\n\nLumberjack plays well with any logging package that can write to an\nio.Writer, including the standard library's log package.\n\nLumberjack assumes that only one process is writing to the output files.\nUsing the same lumberjack configuration from multiple processes on the same\nmachine will result in improper behavior.\n\n\n**Example**\n\nTo use lumberjack with the standard library's log package, just pass it into the SetOutput function when your application starts.\n\nCode:\n\n```go\nlog.SetOutput(\u0026lumberjack.Logger{\n    Filename:   \"/var/log/myapp/foo.log\",\n    MaxSize:    500, // megabytes\n    MaxBackups: 3,\n    MaxAge:     28, //days\n    Compress:   true, // disabled by default\n})\n```\n\n\n\n## type Logger\n``` go\ntype Logger struct {\n    // Filename is the file to write logs to.  Backup log files will be retained\n    // in the same directory.  It uses \u003cprocessname\u003e-lumberjack.log in\n    // os.TempDir() if empty.\n    Filename string `json:\"filename\" yaml:\"filename\"`\n\n    // MaxSize is the maximum size in megabytes of the log file before it gets\n    // rotated. It defaults to 100 megabytes.\n    MaxSize int `json:\"maxsize\" yaml:\"maxsize\"`\n\n    // MaxAge is the maximum number of days to retain old log files based on the\n    // timestamp encoded in their filename.  Note that a day is defined as 24\n    // hours and may not exactly correspond to calendar days due to daylight\n    // savings, leap seconds, etc. The default is not to remove old log files\n    // based on age.\n    MaxAge int `json:\"maxage\" yaml:\"maxage\"`\n\n    // MaxBackups is the maximum number of old log files to retain.  The default\n    // is to retain all old log files (though MaxAge may still cause them to get\n    // deleted.)\n    MaxBackups int `json:\"maxbackups\" yaml:\"maxbackups\"`\n\n    // LocalTime determines if the time used for formatting the timestamps in\n    // backup files is the computer's local time.  The default is to use UTC\n    // time.\n    LocalTime bool `json:\"localtime\" yaml:\"localtime\"`\n\n    // Compress determines if the rotated log files should be compressed\n    // using gzip. The default is not to perform compression.\n    Compress bool `json:\"compress\" yaml:\"compress\"`\n    // contains filtered or unexported fields\n}\n```\nLogger is an io.WriteCloser that writes to the specified filename.\n\nLogger opens or creates the logfile on first Write.  If the file exists and\nis less than MaxSize megabytes, lumberjack will open and append to that file.\nIf the file exists and its size is \u003e= MaxSize megabytes, the file is renamed\nby putting the current time in a timestamp in the name immediately before the\nfile's extension (or the end of the filename if there's no extension). A new\nlog file is then created using original filename.\n\nWhenever a write would cause the current log file exceed MaxSize megabytes,\nthe current file is closed, renamed, and a new log file created with the\noriginal name. Thus, the filename you give Logger is always the \"current\" log\nfile.\n\nBackups use the log file name given to Logger, in the form `name-timestamp.ext`\nwhere name is the filename without the extension, timestamp is the time at which\nthe log was rotated formatted with the time.Time format of\n`2006-01-02T15-04-05.000` and the extension is the original extension.  For\nexample, if your Logger.Filename is `/var/log/foo/server.log`, a backup created\nat 6:30pm on Nov 11 2016 would use the filename\n`/var/log/foo/server-2016-11-04T18-30-00.000.log`\n\n### Cleaning Up Old Log Files\nWhenever a new logfile gets created, old log files may be deleted.  The most\nrecent files according to the encoded timestamp will be retained, up to a\nnumber equal to MaxBackups (or all of them if MaxBackups is 0).  Any files\nwith an encoded timestamp older than MaxAge days are deleted, regardless of\nMaxBackups.  Note that the time encoded in the timestamp is the rotation\ntime, which may differ from the last time that file was written to.\n\nIf MaxBackups and MaxAge are both 0, no old log files will be deleted.\n\n\n\n\n\n\n\n\n\n\n\n### func (\\*Logger) Close\n``` go\nfunc (l *Logger) Close() error\n```\nClose implements io.Closer, and closes the current logfile.\n\n\n\n### func (\\*Logger) Rotate\n``` go\nfunc (l *Logger) Rotate() error\n```\nRotate causes Logger to close the existing log file and immediately create a\nnew one.  This is a helper function for applications that want to initiate\nrotations outside of the normal rotation rules, such as in response to\nSIGHUP.  After rotating, this initiates a cleanup of old log files according\nto the normal rules.\n\n**Example**\n\nExample of how to rotate in response to SIGHUP.\n\nCode:\n\n```go\nl := \u0026lumberjack.Logger{}\nlog.SetOutput(l)\nc := make(chan os.Signal, 1)\nsignal.Notify(c, syscall.SIGHUP)\n\ngo func() {\n    for {\n        \u003c-c\n        l.Rotate()\n    }\n}()\n```\n\n### func (\\*Logger) Write\n``` go\nfunc (l *Logger) Write(p []byte) (n int, err error)\n```\nWrite implements io.Writer.  If a write would cause the log file to be larger\nthan MaxSize, the file is closed, renamed to include a timestamp of the\ncurrent time, and a new log file is created using the original log file name.\nIf the length of the write is greater than MaxSize, an error is returned.\n\n\n\n\n\n\n\n\n\n- - -\nGenerated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnatefinch%2Flumberjack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnatefinch%2Flumberjack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnatefinch%2Flumberjack/lists"}