Skip to content

Commit

Permalink
up: update the handler builder logic, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 13, 2022
1 parent 5d2c1b6 commit a59492b
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 49 deletions.
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
- `simple` output log to the specified file, write directly to the file without buffering
- `rotate_file` outputs logs to the specified file, and supports splitting files by time and size, and `buffer` buffered writing is enabled by default
- See ./handler folder for more built-in implementations
- Output logs to file
- Support enabling `buffer` for log writing
- Support splitting log files by `time` and `size`
- Support configuration to compress log files via `gzip`
- Support clean old log files by `BackupNum` `BackupTime`

### Output logs to file

- Support enabling `buffer` for log writing
- Support splitting log files by `time` and `size`
- Support configuration to compress log files via `gzip`
- Support clean old log files by `BackupNum` `BackupTime`

> NEW: `v0.3.0` discards the various handlers that were originally implemented, and the unified abstraction is
> `FlushCloseHandler` `SyncCloseHandler` `WriteCloserHandler` `IOWriterHandler`
Expand Down Expand Up @@ -513,7 +515,9 @@ size-rotate-file.log.122915_00001
size-rotate-file.log.122915_00002
```

### Quickly create a Handler instance based on config
### Quickly create a Handler based on config

This is config struct for create a Handler:

```go
// Config struct
Expand Down Expand Up @@ -565,18 +569,19 @@ type Config struct {
l := slog.NewWithHandlers(h)
```

### Use Builder to quickly create Handler instances
### Use Builder to quickly create Handler

Use `handler.Builder` to easily and quickly create Handler instances.

```go
testFile := "testdata/info.log"

h := handler.NewBuilder().
With(
handler.WithLogfile(testFile),
handler.WithBuffSize(1024*8),
handler.WithLogLevels(slog.NormalLevels),
handler.WithBuffMode(handler.BuffModeBite),
).
WithLogfile(testFile).
WithLogLevels(slog.NormalLevels).
WithBuffSize(1024*8).
WithLogLevels(slog.NormalLevels).

This comment has been minimized.

Copy link
@iredmail

iredmail Jun 14, 2022

这一行和 581 行重复了。

WithBuffMode(handler.BuffModeBite).
Build()

l := slog.NewWithHandlers(h)
Expand Down
25 changes: 14 additions & 11 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
- `simple` 输出日志到指定文件,无缓冲直接写入文件
- `rotate_file` 输出日志到指定文件,并且同时支持按时间、按大小分割文件,默认启用 `buffer` 缓冲写入
- 更多内置实现请查看 ./handler 文件夹
- 输出日志到指定文件
- 支持启用 `buffer` 缓冲日志写入
- 支持按时间、按大小自动分割文件
- 支持配置通过 `gzip` 压缩日志文件
- 支持清理旧日志文件 配置: `BackupNum` `BackupTime`

### 输出日志到文件

- 支持启用 `buffer` 缓冲日志写入
- 支持按时间、按大小自动分割文件
- 支持配置通过 `gzip` 压缩日志文件
- 支持清理旧日志文件 配置: `BackupNum` `BackupTime`

> NEW: `v0.3.0` 废弃原来实现的纷乱的各种handler,统一抽象为
> `FlushCloseHandler` `SyncCloseHandler` `WriteCloserHandler` `IOWriterHandler`
Expand Down Expand Up @@ -577,16 +579,17 @@ type Config struct {

### 使用Builder快速创建Handler实例

使用 `handler.Builder` 可以方便快速的创建Handler实例。

```go
testFile := "testdata/info.log"

h := handler.NewBuilder().
With(
handler.WithLogfile(testFile),
handler.WithBuffSize(1024*8),
handler.WithLogLevels(slog.NormalLevels),
handler.WithBuffMode(handler.BuffModeBite),
).
WithLogfile(testFile).
WithLogLevels(slog.NormalLevels).
WithBuffSize(1024*8).
WithLogLevels(slog.NormalLevels).

This comment has been minimized.

Copy link
@iredmail

iredmail Jun 14, 2022

这一行和581行重复了。

WithBuffMode(handler.BuffModeBite).
Build()

l := slog.NewWithHandlers(h)
Expand Down
84 changes: 69 additions & 15 deletions handler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ func WithLogfile(logfile string) ConfigFn {
}
}

// WithLogLevels setting
func WithLogLevels(levels slog.Levels) ConfigFn {
return func(c *Config) {
c.Levels = levels
}
}

// WithRotateTime setting
func WithRotateTime(rt rotatefile.RotateTime) ConfigFn {
return func(c *Config) {
Expand All @@ -200,13 +207,6 @@ func WithBuffMode(buffMode string) ConfigFn {
}
}

// WithCompress setting
func WithCompress(compress bool) ConfigFn {
return func(c *Config) {
c.Compress = compress
}
}

// WithBuffSize setting
func WithBuffSize(buffSize int) ConfigFn {
return func(c *Config) {
Expand All @@ -215,26 +215,32 @@ func WithBuffSize(buffSize int) ConfigFn {
}

// WithMaxSize setting
func WithMaxSize(maxSize int) ConfigFn {
func WithMaxSize(maxSize uint64) ConfigFn {
return func(c *Config) {
c.MaxSize = uint64(maxSize)
c.MaxSize = maxSize
}
}

// WithUseJSON setting
func WithUseJSON(useJSON bool) ConfigFn {
// WithCompress setting
func WithCompress(compress bool) ConfigFn {
return func(c *Config) {
c.UseJSON = useJSON
c.Compress = compress
}
}

// WithLogLevels setting
func WithLogLevels(levels slog.Levels) ConfigFn {
// WithUseJSON setting
func WithUseJSON(useJSON bool) ConfigFn {
return func(c *Config) {
c.Levels = levels
c.UseJSON = useJSON
}
}

//
// ---------------------------------------------------------------------------
// handler builder
// ---------------------------------------------------------------------------
//

// Builder struct for create handler
type Builder struct {
*Config
Expand All @@ -260,6 +266,54 @@ func (b *Builder) With(fns ...ConfigFn) *Builder {
return b
}

// WithLogfile setting
func (b *Builder) WithLogfile(logfile string) *Builder {
b.Logfile = logfile
return b
}

// WithLogLevels setting
func (b *Builder) WithLogLevels(levels []slog.Level) *Builder {
b.Levels = levels
return b
}

// WithBuffMode setting
func (b *Builder) WithBuffMode(bufMode string) *Builder {
b.BuffMode = bufMode
return b
}

// WithBuffSize setting
func (b *Builder) WithBuffSize(bufSize int) *Builder {
b.BuffSize = bufSize
return b
}

// WithMaxSize setting
func (b *Builder) WithMaxSize(maxSize uint64) *Builder {
b.MaxSize = maxSize
return b
}

// WithRotateTime setting
func (b *Builder) WithRotateTime(rt rotatefile.RotateTime) *Builder {
b.RotateTime = rt
return b
}

// WithCompress setting
func (b *Builder) WithCompress(compress bool) *Builder {
b.Compress = compress
return b
}

// WithUseJSON setting
func (b *Builder) WithUseJSON(useJSON bool) *Builder {
b.UseJSON = useJSON
return b
}

// Build slog handler.
func (b *Builder) Build() slog.Handler {
if b.Output != nil {
Expand Down
30 changes: 21 additions & 9 deletions handler/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"bytes"
"testing"

"github.com/gookit/goutil/fmtutil"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/slog"
"github.com/gookit/slog/handler"
"github.com/gookit/slog/rotatefile"
"github.com/stretchr/testify/assert"
)

Expand All @@ -22,20 +25,29 @@ func TestNewBuilder(t *testing.T) {
testFile := "testdata/builder.log"
assert.NoError(t, fsutil.DeleteIfFileExist(testFile))

h := handler.NewBuilder().
With(
handler.WithLogfile(testFile),
handler.WithBuffSize(128),
handler.WithBuffMode(handler.BuffModeBite),
handler.WithCompress(true),
).
Build()
b := handler.NewBuilder().
WithLogfile(testFile).
WithLogLevels(slog.AllLevels).
WithBuffSize(128).
WithBuffMode(handler.BuffModeBite).
WithMaxSize(fmtutil.OneMByte * 3).
WithRotateTime(rotatefile.Every30Min).
WithCompress(true).
With(func(c *handler.Config) {
c.BackupNum = 3
})

assert.Equal(t, uint(3), b.BackupNum)
assert.Equal(t, handler.BuffModeBite, b.BuffMode)
assert.Equal(t, rotatefile.Every30Min, b.RotateTime)

h := b.Build()
assert.NotNil(t, h)
assert.NoError(t, h.Close())

h2 := handler.NewBuilder().
WithOutput(new(bytes.Buffer)).
With(handler.WithUseJSON(true)).
WithUseJSON(true).
Build()
assert.NotNil(t, h2)

Expand Down
2 changes: 1 addition & 1 deletion handler/rotate_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewSizeRotateFile(logfile string, maxSize int, fns ...ConfigFn) (*SyncClose
// NewSizeRotateFileHandler instance
func NewSizeRotateFileHandler(logfile string, maxSize int, fns ...ConfigFn) (*SyncCloseHandler, error) {
// close rotate by time.
fns = append(fns, WithMaxSize(maxSize))
fns = append(fns, WithMaxSize(uint64(maxSize)))

return NewRotateFileHandler(logfile, 0, fns...)
}
Expand Down

0 comments on commit a59492b

Please sign in to comment.