Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3.0 #132

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open

V3.0 #132

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
78d709c
v2 is go!
natefinch Jun 27, 2014
3aa94be
update readme and mention gopkg.in in godoc
natefinch Jun 27, 2014
cbb980c
remove travis.yml, update badges, use drone.io for builds
natefinch Jun 27, 2014
5265fd1
fix link in badge
natefinch Jun 27, 2014
afe0d23
fix link in badge
natefinch Jun 27, 2014
2837c0e
comment to make MaxAge units more obvious
natefinch Jul 18, 2014
8b22400
fix a spot where an error was not properly returned
natefinch Jul 23, 2014
689fc50
add changes to maintain perms and owner of logfile
natefinch Jul 25, 2014
6d3dfa6
fix test failures on windows
natefinch Jul 25, 2014
d28785c
Update README.md
natefinch Jul 25, 2014
894827f
Fixed import in example test to use gopkg.in.
elithrar May 11, 2015
75cb349
Merge pull request #11 from elithrar/v2.0
natefinch May 12, 2015
588a21f
Fix bug #12
natefinch May 21, 2015
20b71e5
add coverage badge
natefinch Jun 22, 2015
a8ed6f4
Switch to using gopkg.in/yaml.v2
bz2 Oct 11, 2015
600ceb4
Merge pull request #14 from bz2/use_yaml_v2
natefinch Oct 13, 2015
3cfd7a4
Update rotate_test.go to use v2 of project
Jan 25, 2016
5bae0dc
Use gopkg.in provider instead of github
Jan 25, 2016
514cbda
Merge pull request #19 from tpot/patch-1
natefinch Jan 25, 2016
e21e5cb
fix filemode in tests (#28)
natefinch Oct 7, 2016
dd45e6a
update docs w/ backup format info
natefinch Nov 4, 2016
a96e638
Add support for log file compression (#43)
4a6f656c May 31, 2017
df99d62
switch to travis (#44)
natefinch May 31, 2017
aee4629
Update docs, adding `Compress` setting details (#49)
tbutts Sep 11, 2017
7d6a187
Fix test timing (#64)
natefinch Aug 17, 2018
2e8fbee
Make default file permissions more restrictive (#83)
JAORMX Apr 11, 2019
4b74a4d
fix a typo (#62)
kangxiaoning Apr 11, 2019
94d9e49
use 0755 to create new dir (#68)
caibirdme Apr 11, 2019
47ffae2
cleanup and module support (#77)
glaslos Oct 21, 2020
ac7d08a
WiP
Jun 29, 2021
7918c36
WiP
Jun 29, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ _cgo_gotypes.go
_cgo_export.*

_testmain.go
coverage.out

*.exe
*.test
55 changes: 55 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
run:
timeout: 1m

linters:
enable-all: true
disable:
- wrapcheck
- testpackage
- gochecknoglobals
- exhaustivestruct
- paralleltest
- godox
- cyclop
- tagliatelle
fast: false

# Settings for specific linters
linters-settings:
funlen:
lines: 150
statements: 45

issues:
exclude-rules:
- path: cmd/
linters:
- gochecknoinits
- gomnd
- forbidigo

- path: internal/http/
linters:
- unparam
- nlreturn

- path: _test\.go
linters:
- scopelint
- wsl
- nlreturn
- funlen
- dupl

- path: doc.go
linters:
- lll

- linters:
- lll
source: "json:"

- linters:
- gocritic
- godot
source: "//////"
3 changes: 0 additions & 3 deletions .travis.yml

This file was deleted.

22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
HAS_GOLANGCI := $(shell command -v golangci-lint;)

lint:
ifndef HAS_GOLANGCI
$(error You must install github.com/golangci/golangci-lint)
endif
@golangci-lint run -v -c .golangci.yml && echo "Lint OK"

test:
@go test -p 1 -cover -coverprofile=coverage.out -run . ./... && echo "Test OK"

coverage: test
@go tool cover -func=coverage.out && echo "Coverage OK"

clean:
@go clean ./...
@rm -rf $(BINDIR)
@rm -f coverage.*

ci: lint test coverage

.PHONY: lint test coverage clean ci
174 changes: 2 additions & 172 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,173 +1,3 @@
## Overview

# 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 is a Go package for writing logs to rolling files.

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 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.

Code:

```go
log.SetOutput(&lumberjack.Logger{
Dir: "/var/log/myapp/",
NameFormat: "2006-01-02T15-04-05.000.log",
MaxSize: lumberjack.Gigabyte,
MaxBackups: 3,
MaxAge: 28,
})
```



## 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.
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 bool `json:"localtime" yaml:"localtime"`
// 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.

### 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.

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











### func (\*Logger) Close
``` go
func (l *Logger) Close() error
```
Close implements io.Closer, and closes the current logfile.



### func (\*Logger) Rotate
``` go
func (l *Logger) Rotate() error
```
Rotate causes Logger to close the existing log file and immediately create a
new one. This is a helper function for applications that want to initiate
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 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()
}
}()
```


### 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.









- - -
Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)
This is latest `master` of [Lumberjack](https://github.com/natefinch/lumberjack) merged with work from [spacewander](https://github.com/spacewander/lumberjack/commit/a748684e09fb28daea4956f2e764123c02bd3273?branch=a748684e09fb28daea4956f2e764123c02bd3273&diff=split) (use `Bytes` instead of `MegaBytes`) merged in.
11 changes: 11 additions & 0 deletions chown.go
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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))
}
10 changes: 5 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package lumberjack_test
import (
"log"

"github.com/natefinch/lumberjack"
"github.com/thalesfsp/lumberjack/v3"
)

// 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,
Filename: "/var/log/myapp/foo.log",
MaxBytes: 500 * 1024 * 1024, // 500 MiB
MaxBackups: 3,
MaxAge: 28,
MaxAge: 28, // days
Compress: true, // disabled by default
})
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/thalesfsp/lumberjack/v3

require (
github.com/BurntSushi/toml v0.3.1
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.2.2
)

go 1.13
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
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/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Loading