Skip to content

Commit

Permalink
Merge remote-tracking branch 'cilium/v2.0' into master_v2
Browse files Browse the repository at this point in the history
* cilium/v2.0:
  cleanup and module support (natefinch#77)
  use 0755 to create new dir (natefinch#68)
  fix a typo (natefinch#62)
  Make default file permissions more restrictive (natefinch#83)
  Fix test timing (natefinch#64)
  Update docs, adding `Compress` setting details (natefinch#49)
  switch to travis (natefinch#44)
  Add support for log file compression (natefinch#43)

Signed-off-by: Chance Zibolski <chance.zibolski@gmail.com>
  • Loading branch information
chancez committed Mar 14, 2022
2 parents ed8ef50 + 47ffae2 commit ebcf603
Show file tree
Hide file tree
Showing 10 changed files with 499 additions and 129 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
@@ -0,0 +1,11 @@
language: go

go:
- tip
- 1.15.x
- 1.14.x
- 1.13.x
- 1.12.x

env:
- GO111MODULE=on
7 changes: 6 additions & 1 deletion README.md
@@ -1,4 +1,4 @@
# lumberjack [![GoDoc](https://godoc.org/gopkg.in/natefinch/lumberjack.v2?status.png)](https://godoc.org/gopkg.in/natefinch/lumberjack.v2) [![Build Status](https://drone.io/github.com/natefinch/lumberjack/status.png)](https://drone.io/github.com/natefinch/lumberjack/latest) [![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)
# 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 Down Expand Up @@ -43,6 +43,7 @@ log.SetOutput(&lumberjack.Logger{
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
Compress: true, // disabled by default
})
```

Expand Down Expand Up @@ -76,6 +77,10 @@ type Logger struct {
// 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
}
```
Expand Down
6 changes: 3 additions & 3 deletions chown_linux.go
Expand Up @@ -5,8 +5,8 @@ import (
"syscall"
)

// os_Chown is a var so we can mock it out during tests.
var os_Chown = os.Chown
// 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())
Expand All @@ -15,5 +15,5 @@ func chown(name string, info os.FileInfo) error {
}
f.Close()
stat := info.Sys().(*syscall.Stat_t)
return os_Chown(name, int(stat.Uid), int(stat.Gid))
return osChown(name, int(stat.Uid), int(stat.Gid))
}
9 changes: 4 additions & 5 deletions example_test.go
@@ -1,18 +1,17 @@
package lumberjack_test
package lumberjack

import (
"log"

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

// 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{
log.SetOutput(&Logger{
Filename: "/var/log/myapp/foo.log",
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, // days
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=
135 changes: 118 additions & 17 deletions linux_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"syscall"
"testing"
"time"
)

func TestMaintainMode(t *testing.T) {
Expand Down Expand Up @@ -46,19 +47,23 @@ func TestMaintainMode(t *testing.T) {
}

func TestMaintainOwner(t *testing.T) {
fakeC := fakeChown{}
os_Chown = fakeC.Set
os_Stat = fakeStat
fakeFS := newFakeFS()
osChown = fakeFS.Chown
osStat = fakeFS.Stat
defer func() {
os_Chown = os.Chown
os_Stat = os.Stat
osChown = os.Chown
osStat = os.Stat
}()
currentTime = fakeTime
dir := makeTempDir("TestMaintainOwner", t)
defer os.RemoveAll(dir)

filename := logFile(dir)

f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0644)
isNil(err, t)
f.Close()

l := &Logger{
Filename: filename,
MaxBackups: 1,
Expand All @@ -75,27 +80,123 @@ func TestMaintainOwner(t *testing.T) {
err = l.Rotate()
isNil(err, t)

equals(555, fakeC.uid, t)
equals(666, fakeC.gid, t)
equals(555, fakeFS.files[filename].uid, t)
equals(666, fakeFS.files[filename].gid, t)
}

func TestCompressMaintainMode(t *testing.T) {
currentTime = fakeTime

dir := makeTempDir("TestCompressMaintainMode", t)
defer os.RemoveAll(dir)

filename := logFile(dir)

mode := os.FileMode(0600)
f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, mode)
isNil(err, t)
f.Close()

l := &Logger{
Compress: true,
Filename: filename,
MaxBackups: 1,
MaxSize: 100, // megabytes
}
defer l.Close()
b := []byte("boo!")
n, err := l.Write(b)
isNil(err, t)
equals(len(b), n, t)

newFakeTime()

err = l.Rotate()
isNil(err, t)

// we need to wait a little bit since the files get compressed on a different
// goroutine.
<-time.After(10 * time.Millisecond)

// a compressed version of the log file should now exist with the correct
// mode.
filename2 := backupFile(dir)
info, err := os.Stat(filename)
isNil(err, t)
info2, err := os.Stat(filename2 + compressSuffix)
isNil(err, t)
equals(mode, info.Mode(), t)
equals(mode, info2.Mode(), t)
}

func TestCompressMaintainOwner(t *testing.T) {
fakeFS := newFakeFS()
osChown = fakeFS.Chown
osStat = fakeFS.Stat
defer func() {
osChown = os.Chown
osStat = os.Stat
}()
currentTime = fakeTime
dir := makeTempDir("TestCompressMaintainOwner", t)
defer os.RemoveAll(dir)

filename := logFile(dir)

f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0644)
isNil(err, t)
f.Close()

l := &Logger{
Compress: true,
Filename: filename,
MaxBackups: 1,
MaxSize: 100, // megabytes
}
defer l.Close()
b := []byte("boo!")
n, err := l.Write(b)
isNil(err, t)
equals(len(b), n, t)

newFakeTime()

err = l.Rotate()
isNil(err, t)

// we need to wait a little bit since the files get compressed on a different
// goroutine.
<-time.After(10 * time.Millisecond)

// a compressed version of the log file should now exist with the correct
// owner.
filename2 := backupFile(dir)
equals(555, fakeFS.files[filename2+compressSuffix].uid, t)
equals(666, fakeFS.files[filename2+compressSuffix].gid, t)
}

type fakeFile struct {
uid int
gid int
}

type fakeFS struct {
files map[string]fakeFile
}

type fakeChown struct {
name string
uid int
gid int
func newFakeFS() *fakeFS {
return &fakeFS{files: make(map[string]fakeFile)}
}

func (f *fakeChown) Set(name string, uid, gid int) error {
f.name = name
f.uid = uid
f.gid = gid
func (fs *fakeFS) Chown(name string, uid, gid int) error {
fs.files[name] = fakeFile{uid: uid, gid: gid}
return nil
}

func fakeStat(name string) (os.FileInfo, error) {
func (fs *fakeFS) Stat(name string) (os.FileInfo, error) {
info, err := os.Stat(name)
if err != nil {
return info, err
return nil, err
}
stat := info.Sys().(*syscall.Stat_t)
stat.Uid = 555
Expand Down

0 comments on commit ebcf603

Please sign in to comment.