Skip to content

Commit

Permalink
👔 up: handler.Config add new option DebugMode, update some tests and …
Browse files Browse the repository at this point in the history
…comments
  • Loading branch information
inhere committed Jul 27, 2023
1 parent 21d1056 commit 3a86900
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 14 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,10 @@ f.SetTemplate(myTemplate)
Custom `Processor` and `Formatter` are relatively simple, just implement a corresponding method.

### Create new logger

`slog.Info, slog.Warn` and other methods use the default logger and output logs to the console by default.

You can create a brand new instance of `slog.Logger`:
You can create a brand-new instance of `slog.Logger`:

**Method 1**

Expand Down Expand Up @@ -468,7 +469,7 @@ import (
)

func main() {
defer slog.MustFlush()
defer slog.MustClose()

// DangerLevels contains: slog.PanicLevel, slog.ErrorLevel, slog.WarnLevel
h1 := handler.MustFileHandler("/tmp/error.log", handler.WithLogLevels(slog.DangerLevels))
Expand All @@ -490,7 +491,7 @@ func main() {
}
```

> Tip: If write buffering `buffer` is enabled, be sure to call `logger.Flush()` at the end of the program to flush the contents of the buffer to the file.
> **Note**: If write buffering `buffer` is enabled, be sure to call `logger.Close()` at the end of the program to flush the contents of the buffer to the file.
### Log to file with automatic rotating

Expand Down
4 changes: 2 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ import (
)

func main() {
defer slog.MustFlush()
defer slog.MustClose()

// DangerLevels 包含: slog.PanicLevel, slog.ErrorLevel, slog.WarnLevel
h1 := handler.MustFileHandler("/tmp/error.log", handler.WithLogLevels(slog.DangerLevels))
Expand All @@ -489,7 +489,7 @@ func main() {
}
```

> 提示: 如果启用了写入缓冲 `buffer`,一定要在程序结束时调用 `logger.Flush()` 刷出缓冲区的内容到文件
> 提示: 如果启用了写入缓冲 `buffer`,一定要在程序结束时调用 `logger.Close()/MustClose()` 刷出缓冲区的内容到文件并关闭句柄
### 带自动切割的日志处理器

Expand Down
1 change: 1 addition & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func ExampleFlushDaemon() {

go slog.FlushDaemon(func() {
fmt.Println("flush daemon stopped")
slog.MustClose()
wg.Done()
})

Expand Down
1 change: 1 addition & 0 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Formattable interface {
type FormattableTrait = FormatterWrapper

// FormatterWrapper use for format log record.
// default use the TextFormatter
type FormatterWrapper struct {
// if not set, default use the TextFormatter
formatter Formatter
Expand Down
4 changes: 4 additions & 0 deletions handler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type Config struct {

// RenameFunc build filename for rotate file
RenameFunc func(filepath string, rotateNum uint) string

// DebugMode for debug on development.
DebugMode bool
}

// NewEmptyConfig new config instance
Expand Down Expand Up @@ -168,6 +171,7 @@ func (c *Config) CreateWriter() (output SyncCloseWriter, err error) {
rc.CloseLock = true
rc.Filepath = c.Logfile
rc.FilePerm = c.FilePerm
rc.DebugMode = c.DebugMode

// copy settings
rc.MaxSize = c.MaxSize
Expand Down
2 changes: 2 additions & 0 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler_test

import (
"fmt"
"testing"

"github.com/gookit/goutil"
Expand All @@ -20,6 +21,7 @@ var (
)

func TestMain(m *testing.M) {
fmt.Println("TestMain: remove all test files in ./testdata")
goutil.PanicErr(fsutil.RemoveSub("./testdata", fsutil.ExcludeNames(".keep")))
m.Run()
}
Expand Down
8 changes: 4 additions & 4 deletions handler/rotatefile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestNewRotateFileHandler(t *testing.T) {
l.Info("info", "message", i)
l.Warn("warn message", i)
}
l.MustFlush()
l.MustClose()

// by time
logfile = "./testdata/both-rotate-bytime.log"
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestNewTimeRotateFileHandler_EveryDay(t *testing.T) {
// time.Sleep(time.Second * 1)
}

l.MustFlush()
l.MustClose()
checkLogFileContents(t, logfile)
checkLogFileContents(t, newFile)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestNewTimeRotateFileHandler_EveryHour(t *testing.T) {
sec++
fmt.Println("log number ", (i+1)*2)
}
l.MustFlush()
l.MustClose()

checkLogFileContents(t, logfile)
checkLogFileContents(t, newFile)
Expand All @@ -193,7 +193,7 @@ func TestNewTimeRotateFileHandler_someSeconds(t *testing.T) {
fmt.Println("second ", i+1)
time.Sleep(time.Second * 1)
}
l.MustFlush()
l.MustClose()
// assert.NoErr(t, os.Remove(fpath))
}

Expand Down
2 changes: 1 addition & 1 deletion issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestIssues_27(t *testing.T) {
// https://github.com/gookit/slog/issues/31
func TestIssues_31(t *testing.T) {
defer slog.Reset()
defer slog.MustFlush()
defer slog.MustClose()

// slog.DangerLevels equals slog.Levels{slog.PanicLevel, slog.PanicLevel, slog.ErrorLevel, slog.WarnLevel}
h1 := handler.MustFileHandler("testdata/error_issue31.log", handler.WithLogLevels(slog.DangerLevels))
Expand Down
11 changes: 7 additions & 4 deletions sugared.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
// SugaredLoggerFn func type.
type SugaredLoggerFn func(sl *SugaredLogger)

// SugaredLogger definition.
// Is a fast and usable Logger, which already contains
// SugaredLogger Is a fast and usable Logger, which already contains
// the default formatting and handling capabilities
type SugaredLogger struct {
*Logger
Expand Down Expand Up @@ -109,10 +108,14 @@ func (sl *SugaredLogger) Handle(record *Record) error {
return err
}

// Close all log handlers
// Close all log handlers, will flush and close all handlers.
//
// IMPORTANT:
//
// if enable async/buffer mode, please call the Close() before exit.
func (sl *SugaredLogger) Close() error {
_ = sl.Logger.VisitAll(func(handler Handler) error {
// TIP: must exclude self
// TIP: must exclude self, because self is a handler
if _, ok := handler.(*SugaredLogger); !ok {
if err := handler.Close(); err != nil {
sl.err = err
Expand Down

0 comments on commit 3a86900

Please sign in to comment.