Skip to content

Commit

Permalink
Add everyday rotate (#1)
Browse files Browse the repository at this point in the history
* v2 is go!

* update readme and mention gopkg.in in godoc

* remove travis.yml, update badges, use drone.io for builds

* fix link in badge

* fix link in badge

* comment to make MaxAge units more obvious

* fix a spot where an error was not properly returned

* add changes to maintain perms and owner of logfile

* fix test failures on windows

* Update README.md

add badge for windows build

* Fixed import in example test to use gopkg.in.

* Fix bug natefinch#12

Fixes bug natefinch#12. If the first write to a file would cause it to rotate, instead
of rotating, we'd just move it aside.  This change fixes that problem
by ensuring that we just run rotate in this situation, which does the
right thing (open new and then cleanup.)  Also added test to verify
the fix.

* add coverage badge

* Switch to using gopkg.in/yaml.v2

* Update rotate_test.go to use v2 of project

Hi there.  I thought it would be nice for the rotate example to use v2 of the package.

* Use gopkg.in provider instead of github

* fix filemode in tests (natefinch#28)

This fixes natefinch#20 by using a more restrictive filemode during tests.

* update docs w/ backup format info

* Add support for log file compression (natefinch#43)

* Check test file content, not just length.

It is insufficient to just check the length of test files,
especially given that many of the tests result in multiple files
that have the same content/length. Instead, actually check that
the file content is what it is expected to be. Vary the content
that is being written so that the test failures become apparent.

This also fixes a case where the length of the wrong value is
checked following a write (it happens to work since the length
of the value checked is the same as that written).

* Make timeFromName actually return a time.

Simplify the timeFromName parsing (we only need to slice once,
not twice) and actually parse the extracted time in the
timeFromName function rather than returning an abitrary string
that may or may not be a time. Also conver the timeFromName
tests into table driven tests.

* Add support for compressing log files.

Rather than scanning for old log files (under lock) when a rotation
occurs, a goroutine is started when we first open or create a log
file. Post-rotation compression (if enabled) and removal of stale
log files is now designated to this goroutine.

Scanning, removal and compression are run in the same goroutine in
order to minimise background disk I/O, with removals being processed
prior to compression in order to free up disk space.

This results in a small change in existing behaviour - previously
only logs would be removed when the first rotation occurs, whereas
now logs will potentially be removed when logging first starts.

* Rework file ownership test.

Previously the test only verified that the code called Chown
but failed to verify what it actually called Chown on. This
reworks the code so that we have a fake file system that tracks
file ownership.

This also simplifies upcoming additional tests.

* Clone file owner and mode on compressed log.

Clone the log file owner and the log file mode to the compressed
log file. Add tests to ensure that this is handled correctly.

* switch to travis (natefinch#44)

* Update docs, adding `Compress` setting details (natefinch#49)

* Fix test timing (natefinch#64)

fix test timeout on CI

* Make default file permissions more restrictive (natefinch#83)

This asures that the process can still read and write its own log file,
but that other users cannot. This is a fairly standard mode for log
files in linux.

* fix a typo (natefinch#62)

* use 0755 to create new dir (natefinch#68)

* cleanup and module support (natefinch#77)

* cleanup and module support

* add rotate everyday

* remove unused code in unit tests

* fix rotate everyday

Co-authored-by: Nate Finch <nate.finch@gmail.com>
Co-authored-by: Matt Silverlock <matt@eatsleeprepeat.net>
Co-authored-by: Martin Packman <martin.packman@canonical.com>
Co-authored-by: Tim Potter <tpot@samba.org>
Co-authored-by: Joel Sing <joel@sing.id.au>
Co-authored-by: Tyler Butters <dapegral@gmail.com>
Co-authored-by: Juan Osorio Robles <jaosorior@gmail.com>
Co-authored-by: 康晓宁 <kxnmei@163.com>
Co-authored-by: Deen <englanq@126.com>
Co-authored-by: Lukas Rist <glaslos@gmail.com>
  • Loading branch information
11 people committed Aug 22, 2021
1 parent 6d54cbc commit 85626ee
Show file tree
Hide file tree
Showing 11 changed files with 1,141 additions and 442 deletions.
10 changes: 9 additions & 1 deletion .travis.yml
@@ -1,3 +1,11 @@
language: go

install: go get -d -t -v github.com/natefinch/lumberjack
go:
- tip
- 1.15.x
- 1.14.x
- 1.13.x
- 1.12.x

env:
- GO111MODULE=on
162 changes: 84 additions & 78 deletions README.md
@@ -1,5 +1,4 @@

# lumberjack [![GoDoc](https://godoc.org/github.com/natefinch/lumberjack?status.png)](https://godoc.org/github.com/natefinch/lumberjack) [![Build Status](https://travis-ci.org/natefinch/lumberjack.png)](https://travis-ci.org/natefinch/lumberjack)
# 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)

# Deprecated

Expand All @@ -9,103 +8,111 @@ Developerment will not continue on this branch, and it is highly recommended tha

### Lumberjack is a Go package for writing logs to rolling files.

Package lumberjack provides a rolling logger.

Note that this is v2.0 of lumberjack, and should be imported using gopkg.in
thusly:

import "gopkg.in/natefinch/lumberjack.v2"

The package name remains simply lumberjack, and the code resides at
https://github.com/natefinch/lumberjack under the v2.0 branch.

Lumberjack is intended to be one part of a logging infrastructure.
It is not an all-in-one solution, but instead is a pluggable
component at the bottom of the logging stack that simply controls the files
to which logs are written.

Lumberjack plays well with any logger that can write to an io.Writer,
including the standard library's log package.
Lumberjack plays well with any logging package that can write to an
io.Writer, including the standard library's log package.

Lumberjack assumes that only one process is writing to the output files.
Using the same lumberjack configuration from multiple processes on the same
machine will result in improper behavior.

#### Example

To use lumberjack with the standard library's log package, just pass it into the
SetOutput function when your application starts.
**Example**

To use lumberjack with the standard library's log package, just pass it into the SetOutput function when your application starts.

Code:

```go
log.SetOutput(&lumberjack.Logger{
Dir: "/var/log/myapp/",
NameFormat: "2006-01-02T15-04-05.000.log",
MaxSize: lumberjack.Gigabyte,
Filename: "/var/log/myapp/foo.log",
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28,
MaxAge: 28, //days
Compress: true, // disabled by default
})
```



## Constants
``` go
const (
Megabyte = 1024 * 1024
Gigabyte = 1024 * Megabyte
)
```



## type Logger
``` go
type Logger struct {
// Dir determines the directory in which to store log files.
// It defaults to os.TempDir() if empty.
Dir string `json:"dir" yaml:"dir"`

// NameFormat is the time formatting layout used to generate filenames.
// It defaults to "2006-01-02T15-04-05.000.log".
NameFormat string `json:"nameformat" yaml:"nameformat"`

// MaxSize is the maximum size in bytes of the log file before it gets
// rolled. It defaults to 100 megabytes.
MaxSize int64 `json:"maxsize" yaml:"maxsize"`

// MaxAge is the maximum number of days to retain old log files based on
// FileInfo.ModTime. Note that a day is defined as 24 hours and may not
// exactly correspond to calendar days due to daylight savings, leap
// seconds, etc. The default is not to remove old log files based on age.
// Filename is the file to write logs to. Backup log files will be retained
// in the same directory. It uses <processname>-lumberjack.log in
// os.TempDir() if empty.
Filename string `json:"filename" yaml:"filename"`

// MaxSize is the maximum size in megabytes of the log file before it gets
// rotated. It defaults to 100 megabytes.
MaxSize int `json:"maxsize" yaml:"maxsize"`

// MaxAge is the maximum number of days to retain old log files based on the
// timestamp encoded in their filename. Note that a day is defined as 24
// hours and may not exactly correspond to calendar days due to daylight
// savings, leap seconds, etc. The default is not to remove old log files
// based on age.
MaxAge int `json:"maxage" yaml:"maxage"`

// MaxBackups is the maximum number of old log files to retain. The default
// is to retain all old log files (though MaxAge may still cause them to get
// deleted.)
MaxBackups int `json:"maxbackups" yaml:"maxbackups"`

// LocalTime determines if the time used for formatting the filename is the
// computer's local time. The default is to use UTC time.
// LocalTime determines if the time used for formatting the timestamps in
// backup files is the computer's local time. The default is to use UTC
// time.
LocalTime bool `json:"localtime" yaml:"localtime"`

// Compress determines if the rotated log files should be compressed
// using gzip. The default is not to perform compression.
Compress bool `json:"compress" yaml:"compress"`
// contains filtered or unexported fields
}
```
Logger is an io.WriteCloser that writes to a log file in the given directory
with the given NameFormat. NameFormat should include a time formatting
layout in it that produces a valid unique filename for the OS. For more
about time formatting layouts, read a http://golang.org/pkg/time/#pkg-constants.

The date encoded in the filename by NameFormat is used to determine which log
files are most recent in several situations.

Logger opens or creates a logfile on first Write. It looks for files in the
directory that match its name format, and if the one with the most recent
NameFormat date is less than MaxSize, it will open and append to that file.
If no such file exists, or the file is >= MaxSize, a new file is created
using the current time with NameFormat to generate the filename.

Whenever a write would cause the current log file exceed MaxSize, a new file
is created using the current time.
Logger is an io.WriteCloser that writes to the specified filename.

Logger opens or creates the logfile on first Write. If the file exists and
is less than MaxSize megabytes, lumberjack will open and append to that file.
If the file exists and its size is >= MaxSize megabytes, the file is renamed
by putting the current time in a timestamp in the name immediately before the
file's extension (or the end of the filename if there's no extension). A new
log file is then created using original filename.

Whenever a write would cause the current log file exceed MaxSize megabytes,
the current file is closed, renamed, and a new log file created with the
original name. Thus, the filename you give Logger is always the "current" log
file.

Backups use the log file name given to Logger, in the form `name-timestamp.ext`
where name is the filename without the extension, timestamp is the time at which
the log was rotated formatted with the time.Time format of
`2006-01-02T15-04-05.000` and the extension is the original extension. For
example, if your Logger.Filename is `/var/log/foo/server.log`, a backup created
at 6:30pm on Nov 11 2016 would use the filename
`/var/log/foo/server-2016-11-04T18-30-00.000.log`

### Cleaning Up Old Log Files
Whenever a new file gets created, old log files may be deleted. The log file
directory is scanned for files that match NameFormat. The most recent files
according to their NameFormat date will be retained, up to a number equal to
MaxBackups (or all of them if MaxBackups is 0). Any files with a last
modified time (based on FileInfo.ModTime) older than MaxAge days are deleted,
regardless of MaxBackups.
Whenever a new logfile gets created, old log files may be deleted. The most
recent files according to the encoded timestamp will be retained, up to a
number equal to MaxBackups (or all of them if MaxBackups is 0). Any files
with an encoded timestamp older than MaxAge days are deleted, regardless of
MaxBackups. Note that the time encoded in the timestamp is the rotation
time, which may differ from the last time that file was written to.

If MaxBackups and MaxAge are both 0, no old log files will be deleted.

Expand Down Expand Up @@ -137,35 +144,34 @@ rotations outside of the normal rotation rules, such as in response to
SIGHUP. After rotating, this initiates a cleanup of old log files according
to the normal rules.


#### Example
**Example**

Example of how to rotate in response to SIGHUP.

Code:

```go
l := &lumberjack.Logger{}
log.SetOutput(l)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)

go func() {
for {
<-c
l.Rotate()
}
}()
l := &lumberjack.Logger{}
log.SetOutput(l)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)

go func() {
for {
<-c
l.Rotate()
}
}()
```


### func (\*Logger) Write
``` go
func (l *Logger) Write(p []byte) (n int, err error)
```
Write implements io.Writer. If a write would cause the log file to be larger
than MaxSize, a new log file is created using the current time formatted with
PathFormat. If the length of the write is greater than MaxSize, an error is
returned.
than MaxSize, the file is closed, renamed to include a timestamp of the
current time, and a new log file is created using the original log file name.
If the length of the write is greater than MaxSize, an error is returned.



Expand Down
11 changes: 11 additions & 0 deletions chown.go
@@ -0,0 +1,11 @@
// +build !linux

package lumberjack

import (
"os"
)

func chown(_ string, _ os.FileInfo) error {
return nil
}
19 changes: 19 additions & 0 deletions chown_linux.go
@@ -0,0 +1,19 @@
package lumberjack

import (
"os"
"syscall"
)

// osChown is a var so we can mock it out during tests.
var osChown = os.Chown

func chown(name string, info os.FileInfo) error {
f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode())
if err != nil {
return err
}
f.Close()
stat := info.Sys().(*syscall.Stat_t)
return osChown(name, int(stat.Uid), int(stat.Gid))
}
14 changes: 6 additions & 8 deletions example_test.go
@@ -1,19 +1,17 @@
package lumberjack_test
package lumberjack

import (
"log"

"github.com/natefinch/lumberjack"
)

// To use lumberjack with the standard library's log package, just pass it into
// the SetOutput function when your application starts.
func Example() {
log.SetOutput(&lumberjack.Logger{
Dir: "/var/log/myapp/",
NameFormat: "2006-01-02T15-04-05.000.log",
MaxSize: lumberjack.Gigabyte,
log.SetOutput(&Logger{
Filename: "/var/log/myapp/foo.log",
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28,
MaxAge: 28, // days
Compress: true, // disabled by default
})
}
8 changes: 8 additions & 0 deletions go.mod
@@ -0,0 +1,8 @@
module github.com/natefinch/lumberjack

require (
github.com/BurntSushi/toml v0.3.1
gopkg.in/yaml.v2 v2.2.2
)

go 1.13
6 changes: 6 additions & 0 deletions go.sum
@@ -0,0 +1,6 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 comments on commit 85626ee

Please sign in to comment.