Skip to content

Commit

Permalink
Add MaxBytes and deprecate MaxSize
Browse files Browse the repository at this point in the history
Signed-off-by: Fahed DORGAA <fahed_dorgaa@carrefour.com>

fixes

Signed-off-by: Fahed DORGAA <fahed_dorgaa@carrefour.com>

fixes

Signed-off-by: fahed dorgaa <fahed.dorgaa@gmail.com>

fixes

Signed-off-by: fahed dorgaa <fahed.dorgaa@gmail.com>

upgrade deps

Signed-off-by: fahed dorgaa <fahed.dorgaa@gmail.com>
  • Loading branch information
Fahed DORGAA authored and fahedouch committed May 3, 2022
1 parent 47ffae2 commit afa46a6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/natefinch/lumberjack

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

go 1.13
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
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=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
21 changes: 15 additions & 6 deletions lumberjack.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ var _ io.WriteCloser = (*Logger)(nil)
// 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
// is less than MaxBytes, lumberjack will open and append to that file.
// If the file exists and its size is >= MaxBytes, 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.
Expand Down Expand Up @@ -82,6 +82,12 @@ type Logger struct {
// os.TempDir() if empty.
Filename string `json:"filename" yaml:"filename"`

// MaxBytes is the maximum size in bytes of the log file before it gets
// rotated. It defaults to 104857600 (100 megabytes).
MaxBytes int64 `json:"maxbytes" yaml:"maxbytes"`

// Deprecated: use MaxBytes instead.
// This field is mutually exclusive with the “MaxBytes” field.
// 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"`
Expand Down Expand Up @@ -129,9 +135,9 @@ var (
)

// Write implements io.Writer. If a write would cause the log file to be larger
// than MaxSize, the file is closed, renamed to include a timestamp of the
// than MaxBytes, 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.
// If the length of the write is greater than MaxBytes, an error is returned.
func (l *Logger) Write(p []byte) (n int, err error) {
l.mu.Lock()
defer l.mu.Unlock()
Expand Down Expand Up @@ -259,8 +265,8 @@ func backupName(name string, local bool) string {
}

// openExistingOrNew opens the logfile if it exists and if the current write
// would not put it over MaxSize. If there is no such file or the write would
// put it over the MaxSize, a new file is created.
// would not put it over MaxBytes. If there is no such file or the write would
// put it over the MaxBytes, a new file is created.
func (l *Logger) openExistingOrNew(writeLen int) error {
l.mill()

Expand Down Expand Up @@ -443,6 +449,9 @@ func (l *Logger) timeFromName(filename, prefix, ext string) (time.Time, error) {

// max returns the maximum size in bytes of log files before rolling.
func (l *Logger) max() int64 {
if l.MaxBytes != 0 {
return l.MaxBytes
}
if l.MaxSize == 0 {
return int64(defaultMaxSize * megabyte)
}
Expand Down

0 comments on commit afa46a6

Please sign in to comment.