+8
-3
@@ -1,6 +1,11 @@
|
|||||||
language: go
|
language: go
|
||||||
|
|
||||||
go:
|
go:
|
||||||
- 1.8
|
- tip
|
||||||
- 1.7
|
- 1.15.x
|
||||||
- 1.6
|
- 1.14.x
|
||||||
|
- 1.13.x
|
||||||
|
- 1.12.x
|
||||||
|
|
||||||
|
env:
|
||||||
|
- GO111MODULE=on
|
||||||
|
|||||||
+3
-3
@@ -5,8 +5,8 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
// os_Chown is a var so we can mock it out during tests.
|
// osChown is a var so we can mock it out during tests.
|
||||||
var os_Chown = os.Chown
|
var osChown = os.Chown
|
||||||
|
|
||||||
func chown(name string, info os.FileInfo) error {
|
func chown(name string, info os.FileInfo) error {
|
||||||
f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode())
|
f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode())
|
||||||
@@ -15,5 +15,5 @@ func chown(name string, info os.FileInfo) error {
|
|||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
stat := info.Sys().(*syscall.Stat_t)
|
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))
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-5
@@ -1,19 +1,17 @@
|
|||||||
package lumberjack_test
|
package lumberjack
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"gopkg.in/natefinch/lumberjack.v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// To use lumberjack with the standard library's log package, just pass it into
|
// To use lumberjack with the standard library's log package, just pass it into
|
||||||
// the SetOutput function when your application starts.
|
// the SetOutput function when your application starts.
|
||||||
func Example() {
|
func Example() {
|
||||||
log.SetOutput(&lumberjack.Logger{
|
log.SetOutput(&Logger{
|
||||||
Filename: "/var/log/myapp/foo.log",
|
Filename: "/var/log/myapp/foo.log",
|
||||||
MaxSize: 500, // megabytes
|
MaxSize: 500, // megabytes
|
||||||
MaxBackups: 3,
|
MaxBackups: 3,
|
||||||
MaxAge: 28, // days
|
MaxAge: 28, // days
|
||||||
Compress: true, // disabled by default
|
Compress: true, // disabled by default
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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=
|
||||||
+10
-10
@@ -48,11 +48,11 @@ func TestMaintainMode(t *testing.T) {
|
|||||||
|
|
||||||
func TestMaintainOwner(t *testing.T) {
|
func TestMaintainOwner(t *testing.T) {
|
||||||
fakeFS := newFakeFS()
|
fakeFS := newFakeFS()
|
||||||
os_Chown = fakeFS.Chown
|
osChown = fakeFS.Chown
|
||||||
os_Stat = fakeFS.Stat
|
osStat = fakeFS.Stat
|
||||||
defer func() {
|
defer func() {
|
||||||
os_Chown = os.Chown
|
osChown = os.Chown
|
||||||
os_Stat = os.Stat
|
osStat = os.Stat
|
||||||
}()
|
}()
|
||||||
currentTime = fakeTime
|
currentTime = fakeTime
|
||||||
dir := makeTempDir("TestMaintainOwner", t)
|
dir := makeTempDir("TestMaintainOwner", t)
|
||||||
@@ -98,7 +98,7 @@ func TestCompressMaintainMode(t *testing.T) {
|
|||||||
f.Close()
|
f.Close()
|
||||||
|
|
||||||
l := &Logger{
|
l := &Logger{
|
||||||
Compress: true,
|
Compress: true,
|
||||||
Filename: filename,
|
Filename: filename,
|
||||||
MaxBackups: 1,
|
MaxBackups: 1,
|
||||||
MaxSize: 100, // megabytes
|
MaxSize: 100, // megabytes
|
||||||
@@ -123,7 +123,7 @@ func TestCompressMaintainMode(t *testing.T) {
|
|||||||
filename2 := backupFile(dir)
|
filename2 := backupFile(dir)
|
||||||
info, err := os.Stat(filename)
|
info, err := os.Stat(filename)
|
||||||
isNil(err, t)
|
isNil(err, t)
|
||||||
info2, err := os.Stat(filename2+compressSuffix)
|
info2, err := os.Stat(filename2 + compressSuffix)
|
||||||
isNil(err, t)
|
isNil(err, t)
|
||||||
equals(mode, info.Mode(), t)
|
equals(mode, info.Mode(), t)
|
||||||
equals(mode, info2.Mode(), t)
|
equals(mode, info2.Mode(), t)
|
||||||
@@ -131,11 +131,11 @@ func TestCompressMaintainMode(t *testing.T) {
|
|||||||
|
|
||||||
func TestCompressMaintainOwner(t *testing.T) {
|
func TestCompressMaintainOwner(t *testing.T) {
|
||||||
fakeFS := newFakeFS()
|
fakeFS := newFakeFS()
|
||||||
os_Chown = fakeFS.Chown
|
osChown = fakeFS.Chown
|
||||||
os_Stat = fakeFS.Stat
|
osStat = fakeFS.Stat
|
||||||
defer func() {
|
defer func() {
|
||||||
os_Chown = os.Chown
|
osChown = os.Chown
|
||||||
os_Stat = os.Stat
|
osStat = os.Stat
|
||||||
}()
|
}()
|
||||||
currentTime = fakeTime
|
currentTime = fakeTime
|
||||||
dir := makeTempDir("TestCompressMaintainOwner", t)
|
dir := makeTempDir("TestCompressMaintainOwner", t)
|
||||||
|
|||||||
+5
-5
@@ -120,7 +120,7 @@ var (
|
|||||||
currentTime = time.Now
|
currentTime = time.Now
|
||||||
|
|
||||||
// os_Stat exists so it can be mocked out by tests.
|
// 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
|
// 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
|
// variable so tests can mock it out and not need to write megabytes of data
|
||||||
@@ -213,7 +213,7 @@ func (l *Logger) openNew() error {
|
|||||||
|
|
||||||
name := l.filename()
|
name := l.filename()
|
||||||
mode := os.FileMode(0600)
|
mode := os.FileMode(0600)
|
||||||
info, err := os_Stat(name)
|
info, err := osStat(name)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// Copy the mode off the old logfile.
|
// Copy the mode off the old logfile.
|
||||||
mode = info.Mode()
|
mode = info.Mode()
|
||||||
@@ -265,7 +265,7 @@ func (l *Logger) openExistingOrNew(writeLen int) error {
|
|||||||
l.mill()
|
l.mill()
|
||||||
|
|
||||||
filename := l.filename()
|
filename := l.filename()
|
||||||
info, err := os_Stat(filename)
|
info, err := osStat(filename)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return l.openNew()
|
return l.openNew()
|
||||||
}
|
}
|
||||||
@@ -376,7 +376,7 @@ func (l *Logger) millRunOnce() error {
|
|||||||
// millRun runs in a goroutine to manage post-rotation compression and removal
|
// millRun runs in a goroutine to manage post-rotation compression and removal
|
||||||
// of old log files.
|
// of old log files.
|
||||||
func (l *Logger) millRun() {
|
func (l *Logger) millRun() {
|
||||||
for _ = range l.millCh {
|
for range l.millCh {
|
||||||
// what am I going to do, log this?
|
// what am I going to do, log this?
|
||||||
_ = l.millRunOnce()
|
_ = l.millRunOnce()
|
||||||
}
|
}
|
||||||
@@ -472,7 +472,7 @@ func compressLogFile(src, dst string) (err error) {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
fi, err := os_Stat(src)
|
fi, err := osStat(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to stat log file: %v", err)
|
return fmt.Errorf("failed to stat log file: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-4
@@ -1,19 +1,17 @@
|
|||||||
// +build linux
|
// +build linux
|
||||||
|
|
||||||
package lumberjack_test
|
package lumberjack
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"gopkg.in/natefinch/lumberjack.v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Example of how to rotate in response to SIGHUP.
|
// Example of how to rotate in response to SIGHUP.
|
||||||
func ExampleLogger_Rotate() {
|
func ExampleLogger_Rotate() {
|
||||||
l := &lumberjack.Logger{}
|
l := &Logger{}
|
||||||
log.SetOutput(l)
|
log.SetOutput(l)
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, syscall.SIGHUP)
|
signal.Notify(c, syscall.SIGHUP)
|
||||||
|
|||||||
Reference in New Issue
Block a user