Skip to content

Commit

Permalink
refactor: rename all instances of lumberjack to woodcutter
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajil1213 committed Sep 2, 2023
1 parent c99b5a7 commit dcde694
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,28 @@ Package woodcutter provides a rolling logger that enhances lumberjack with the f

## From the original library

Lumberjack is intended to be one part of a logging infrastructure.
(The following has been retained from the docs for the lumberjack library with all instances of `lumberjack` replaced with `woodcutter`)

Woodcutter is intended to be one part of a logging infrastructure.
It is not an all-in-one solution, but instead is a pluggable
component at the bottom of the logging stack that simply controls the files
to which logs are written.

Lumberjack plays well with any logging package that can write to an
Woodcutter plays well with any logging package that can write to an
io.Writer, including the standard library's log package.

Lumberjack assumes that only one process is writing to the output files.
Using the same lumberjack configuration from multiple processes on the same
Woodcutter assumes that only one process is writing to the output files.
Using the same woodcutter configuration from multiple processes on the same
machine will result in improper behavior.

### Example

To use lumberjack with the standard library's log package, just pass it into the SetOutput function when your application starts.
To use woodcutter with the standard library's log package, just pass it into the SetOutput function when your application starts.

Code:

```go
log.SetOutput(&lumberjack.Logger{
log.SetOutput(&woodcutter.Logger{

})
```
Expand All @@ -48,7 +50,7 @@ log.SetOutput(&lumberjack.Logger{
``` go
type Logger struct {
// Filename is the file to write logs to. Backup log files will be retained
// in the same directory. It uses <processname>-lumberjack.log in
// in the same directory. It uses <processname>-woodcutter.log in
// os.TempDir() if empty.
Filename string `json:"filename" yaml:"filename"`

Expand Down Expand Up @@ -83,7 +85,7 @@ type Logger struct {
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.
is less than MaxSize megabytes, woodcutter will open and append to that file.
If the file exists and its size is >= MaxSize megabytes, 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
Expand Down Expand Up @@ -138,7 +140,7 @@ Example of how to rotate in response to SIGHUP.
Code:

```go
l := &lumberjack.Logger{}
l := &woodcutter.Logger{}
log.SetOutput(l)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
Expand Down
8 changes: 4 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (
"github.com/stretchr/testify/assert"
)

// To use lumberjack with the standard library's log package, just pass it into
// To use woodcutter with the standard library's log package, just pass it into
// the SetOutput function when your application starts.
func TestExample_UsageWithStandardLogger(t *testing.T) {
cwd := t.TempDir()
logfilename := filepath.Join(cwd, "foo.log")
lumberjacklogger := Logger{
woodcutterlogger := Logger{
Filename: logfilename,
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, // days
Compress: true, // disabled by default
}
log.SetOutput(&lumberjacklogger)
defer lumberjacklogger.Close()
log.SetOutput(&woodcutterlogger)
defer woodcutterlogger.Close()

logger := log.Default()

Expand Down
26 changes: 13 additions & 13 deletions slogrotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
)

func newLumberjackLogger(
func newWoodcutterLogger(
logfilepath string,
maxBackups, maxAge, maxSize int,
localtime, compress bool,
Expand All @@ -26,16 +26,16 @@ func newLumberjackLogger(
}
}

func newSlogRotateLogger(lumberjackLogger *Logger) *slog.Logger {
return slog.New(slog.NewTextHandler(lumberjackLogger, &slog.HandlerOptions{}))
func newSlogRotateLogger(woodcutterLogger *Logger) *slog.Logger {
return slog.New(slog.NewTextHandler(woodcutterLogger, &slog.HandlerOptions{}))
}

func TestSlog_CreationOfLogFile(t *testing.T) {
resetMocks()
cwd := t.TempDir()
logfile := filepath.Join(cwd, "test.log")
lumberjackLogger := newLumberjackLogger(logfile, 1, 1, 1, true, true)
logger := newSlogRotateLogger(lumberjackLogger)
woodcutterLogger := newWoodcutterLogger(logfile, 1, 1, 1, true, true)
logger := newSlogRotateLogger(woodcutterLogger)

logger.Warn("this is a test", "test_key", "test_value")

Expand All @@ -47,14 +47,14 @@ func TestSlog_Rotation(t *testing.T) {
cwd := t.TempDir()
filename := "test.log"
logfile := filepath.Join(cwd, filename)
lumberjackLogger := newLumberjackLogger(logfile, 0, 0, 0, false, false)
logger := newSlogRotateLogger(lumberjackLogger)
woodcutterLogger := newWoodcutterLogger(logfile, 0, 0, 0, false, false)
logger := newSlogRotateLogger(woodcutterLogger)

logger.Warn("this is a test", "test_key", "test_value")

assert.FileExists(t, logfile)

err := lumberjackLogger.Rotate()
err := woodcutterLogger.Rotate()
assert.Nil(t, err)

filesInLogDir, readErr := os.ReadDir(cwd)
Expand All @@ -76,8 +76,8 @@ func TestSlog_ConcurrentLogging(t *testing.T) {
cwd := t.TempDir()
filename := "test.log"
logfile := filepath.Join(cwd, filename)
lumberjackLogger := newLumberjackLogger(logfile, 0, 0, 0, false, false)
logger := newSlogRotateLogger(lumberjackLogger)
woodcutterLogger := newWoodcutterLogger(logfile, 0, 0, 0, false, false)
logger := newSlogRotateLogger(woodcutterLogger)

numRoutines := 5
done := make(chan bool, numRoutines)
Expand Down Expand Up @@ -113,8 +113,8 @@ func TestSlog_RotateInConcurrent(t *testing.T) {
cwd := t.TempDir()
filename := "test.log"
logfile := filepath.Join(cwd, filename)
lumberjackLogger := newLumberjackLogger(logfile, 0, 0, 0, false, false)
logger := newSlogRotateLogger(lumberjackLogger)
woodcutterLogger := newWoodcutterLogger(logfile, 0, 0, 0, false, false)
logger := newSlogRotateLogger(woodcutterLogger)

numRoutines := 5
done := make(chan bool, numRoutines)
Expand All @@ -123,7 +123,7 @@ func TestSlog_RotateInConcurrent(t *testing.T) {
go func() {
logger.Warn("concurrency test", "test_num", testNum)
if testNum%2 == 0 {
rotateErr := lumberjackLogger.Rotate()
rotateErr := woodcutterLogger.Rotate()
assert.Nil(t, rotateErr)
}
done <- true
Expand Down
8 changes: 4 additions & 4 deletions woodcutter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ 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.
// is less than MaxSize megabytes, woodcutter will open and append to that file.
// If the file exists and its size is >= MaxSize megabytes, 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
Expand Down Expand Up @@ -72,7 +72,7 @@ var _ io.WriteCloser = (*Logger)(nil)
// If MaxBackups and MaxAge are both 0, no old log files will be deleted.
type Logger struct {
// Filename is the file to write logs to. Backup log files will be retained
// in the same directory. It uses <processname>-lumberjack.log in
// in the same directory. It uses <processname>-woodcutter.log in
// os.TempDir() if empty.
Filename string `json:"filename" yaml:"filename"`

Expand Down Expand Up @@ -305,7 +305,7 @@ func (l *Logger) filename() string {
if l.Filename != "" {
return l.Filename
}
name := filepath.Base(os.Args[0]) + "-lumberjack.log"
name := filepath.Base(os.Args[0]) + "-woodcutter.log"
return filepath.Join(os.TempDir(), name)
}

Expand Down Expand Up @@ -443,7 +443,7 @@ func (l *Logger) oldLogFiles() ([]logInfo, error) {
continue
}
// error parsing means that the suffix at the end was not generated
// by lumberjack, and therefore it's not a backup file.
// by woodcutter, and therefore it's not a backup file.
}

sort.Sort(byFormatTime(logFiles))
Expand Down
2 changes: 1 addition & 1 deletion woodcutter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestMain_DefaultFilename(t *testing.T) {
newUUID = fakeUUID
// use `os` instead of `t` to fit implementation of `Logger.filename()`
dir := os.TempDir()
filename := filepath.Join(dir, filepath.Base(os.Args[0])+"-lumberjack.log")
filename := filepath.Join(dir, filepath.Base(os.Args[0])+"-woodcutter.log")
defer os.Remove(filename)

l := &Logger{}
Expand Down

0 comments on commit dcde694

Please sign in to comment.