Skip to content

Commit

Permalink
cleanup and module support (natefinch#77)
Browse files Browse the repository at this point in the history
* cleanup and module support
  • Loading branch information
glaslos committed Oct 21, 2020
1 parent 94d9e49 commit 47ffae2
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 30 deletions.
11 changes: 8 additions & 3 deletions .travis.yml
@@ -1,6 +1,11 @@
language: go

go:
- 1.8
- 1.7
- 1.6
- tip
- 1.15.x
- 1.14.x
- 1.13.x
- 1.12.x

env:
- GO111MODULE=on
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))
}
8 changes: 3 additions & 5 deletions example_test.go
@@ -1,19 +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=
20 changes: 10 additions & 10 deletions linux_test.go
Expand Up @@ -48,11 +48,11 @@ func TestMaintainMode(t *testing.T) {

func TestMaintainOwner(t *testing.T) {
fakeFS := newFakeFS()
os_Chown = fakeFS.Chown
os_Stat = fakeFS.Stat
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)
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestCompressMaintainMode(t *testing.T) {
f.Close()

l := &Logger{
Compress: true,
Compress: true,
Filename: filename,
MaxBackups: 1,
MaxSize: 100, // megabytes
Expand All @@ -123,19 +123,19 @@ func TestCompressMaintainMode(t *testing.T) {
filename2 := backupFile(dir)
info, err := os.Stat(filename)
isNil(err, t)
info2, err := os.Stat(filename2+compressSuffix)
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()
os_Chown = fakeFS.Chown
os_Stat = fakeFS.Stat
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("TestCompressMaintainOwner", t)
Expand Down
10 changes: 5 additions & 5 deletions lumberjack.go
Expand Up @@ -120,7 +120,7 @@ var (
currentTime = time.Now

// os_Stat exists so it can be mocked out by tests.
os_Stat = os.Stat
osStat = os.Stat

// megabyte is the conversion factor between MaxSize and bytes. It is a
// variable so tests can mock it out and not need to write megabytes of data
Expand Down Expand Up @@ -213,7 +213,7 @@ func (l *Logger) openNew() error {

name := l.filename()
mode := os.FileMode(0600)
info, err := os_Stat(name)
info, err := osStat(name)
if err == nil {
// Copy the mode off the old logfile.
mode = info.Mode()
Expand Down Expand Up @@ -265,7 +265,7 @@ func (l *Logger) openExistingOrNew(writeLen int) error {
l.mill()

filename := l.filename()
info, err := os_Stat(filename)
info, err := osStat(filename)
if os.IsNotExist(err) {
return l.openNew()
}
Expand Down Expand Up @@ -376,7 +376,7 @@ func (l *Logger) millRunOnce() error {
// millRun runs in a goroutine to manage post-rotation compression and removal
// of old log files.
func (l *Logger) millRun() {
for _ = range l.millCh {
for range l.millCh {
// what am I going to do, log this?
_ = l.millRunOnce()
}
Expand Down Expand Up @@ -472,7 +472,7 @@ func compressLogFile(src, dst string) (err error) {
}
defer f.Close()

fi, err := os_Stat(src)
fi, err := osStat(src)
if err != nil {
return fmt.Errorf("failed to stat log file: %v", err)
}
Expand Down
6 changes: 2 additions & 4 deletions rotate_test.go
@@ -1,19 +1,17 @@
// +build linux

package lumberjack_test
package lumberjack

import (
"log"
"os"
"os/signal"
"syscall"

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

// Example of how to rotate in response to SIGHUP.
func ExampleLogger_Rotate() {
l := &lumberjack.Logger{}
l := &Logger{}
log.SetOutput(l)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
Expand Down

0 comments on commit 47ffae2

Please sign in to comment.