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

add BackupDir specify #174

Open
wants to merge 5 commits into
base: v2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 19 additions & 4 deletions lumberjack.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ type Logger struct {
// os.TempDir() if empty.
Filename string `json:"filename" yaml:"filename"`

// BackupDir is the dir to moving the backup logs to.
// It uses the same directory of Filename if empty.
BackupDir string `json:"backupdir" yaml:"backupdir"`

// 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 @@ -218,7 +222,7 @@ func (l *Logger) openNew() error {
// Copy the mode off the old logfile.
mode = info.Mode()
// move the existing file
newname := backupName(name, l.LocalTime)
newname := backupName(l.bkdir(), name, l.LocalTime)
if err := os.Rename(name, newname); err != nil {
return fmt.Errorf("can't rename log file: %s", err)
}
Expand All @@ -244,8 +248,13 @@ func (l *Logger) openNew() error {
// backupName creates a new filename from the given name, inserting a timestamp
// between the filename and the extension, using the local time if requested
// (otherwise UTC).
func backupName(name string, local bool) string {
dir := filepath.Dir(name)
func backupName(bkdir string, name string, local bool) string {
var dir string
if (bkdir != "") {
dir = bkdir
} else {
dir = filepath.Dir(name)
}
filename := filepath.Base(name)
ext := filepath.Ext(filename)
prefix := filename[:len(filename)-len(ext)]
Expand Down Expand Up @@ -398,7 +407,7 @@ func (l *Logger) mill() {
// oldLogFiles returns the list of backup log files stored in the same
// directory as the current log file, sorted by ModTime
func (l *Logger) oldLogFiles() ([]logInfo, error) {
files, err := ioutil.ReadDir(l.dir())
files, err := ioutil.ReadDir(l.bkdir())
if err != nil {
return nil, fmt.Errorf("can't read log file directory: %s", err)
}
Expand Down Expand Up @@ -453,6 +462,12 @@ func (l *Logger) max() int64 {
func (l *Logger) dir() string {
return filepath.Dir(l.filename())
}
func (l *Logger) bkdir() string {
if (l.BackupDir != "") {
return l.BackupDir
}
return l.dir()
}

// prefixAndExt returns the filename part and extension part from the Logger's
// filename.
Expand Down
23 changes: 22 additions & 1 deletion lumberjack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"fmt"
"io/ioutil"
"os"
//"os/signal"
//"syscall"
"path/filepath"
"testing"
"time"

"log"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -814,3 +816,22 @@ func exists(path string, t testing.TB) {
_, err := os.Stat(path)
assertUp(err == nil, t, 1, "expected file to exist, but got error from os.Stat: %v", err)
}



func TestLogger_Rotate(t *testing.T) {

l := &Logger{
Filename: "./t.log",
BackupDir: "./bk",
MaxSize: 200,
MaxBackups: 2,
}
log.SetOutput(l)

for i := 0; i <= 1009000; i++ {
log.Println(">>>>>>>>>>>>>>>>>>>>")
}

l.Rotate()
}
11 changes: 9 additions & 2 deletions rotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ import (
"os"
"os/signal"
"syscall"
"testing"
)

// Example of how to rotate in response to SIGHUP.
func ExampleLogger_Rotate() {
l := &Logger{}
func TestExampleLogger_Rotate(t *testing.T) {

t.FailNow()
l := &Logger{
Filename: "./t.log"
BackupDir: "./bk"
MaxSize: 200
}
log.SetOutput(l)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
Expand Down