Skip to content

Commit

Permalink
⬆️ dep: update the gookit/goutil to v0.6.9 and update some README docs
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 11, 2023
1 parent 699ecd8 commit c00791b
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 19 deletions.
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
- 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`
> Several processors that support different types of writers. Makes it easier to build custom handlers, built-in handlers are basically composed of them.
- The `rotatefile` package can also be used with other logging libraries. For example: `log`, `glog`, `zap`, etc.

## [中文说明](README.zh-CN.md)

Expand Down Expand Up @@ -521,6 +518,33 @@ size-rotate-file.log.122915_0001
size-rotate-file.log.122915_0002
```

### Use rotatefile on another logger

`rotatefile.Writer` can also be used with other logging packages, such as: `log`, `glog`, etc.

For example, using `rotatefile` on golang `log`:

```go
package main

import (
"log"

"github.com/gookit/slog/rotatefile"
)

func main() {
logFile := "testdata/go_logger.log"
writer, err := rotatefile.NewConfig(logFile).Create()
if err != nil {
panic(err)
}

log.SetOutput(writer)
log.Println("log message")
}
```

### Quickly create a Handler based on config

This is config struct for create a Handler:
Expand Down
5 changes: 3 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- 支持按时间、按大小自动分割文件
- 支持配置通过 `gzip` 压缩日志文件
- 支持清理旧日志文件 配置: `BackupNum` `BackupTime`
- `rotatefile` 包也可以用在其他日志库。例如:`log``glog``zap` 等等。

> NEW: `v0.3.0` 废弃原来实现的纷乱的各种handler,统一抽象为
> `FlushCloseHandler` `SyncCloseHandler` `WriteCloserHandler` `IOWriterHandler`
Expand Down Expand Up @@ -623,9 +624,9 @@ type Config struct {
- `rotatefile.Writer` 实现对日志文件按大小和指定时间进行自动切割,同时也支持自动清理日志文件
- `handler/rotate_file` 即是通过使用它对日志文件进行切割处理

### 在其他日志包上使用rotatefile
### 在其他日志包上使用 rotatefile

当然,`rotatefile.Writer` 也可以用在其他日志包上,例如:`log``glog` 等等。
`rotatefile.Writer` 也可以用在其他日志包上,例如:`log``glog` 等等。

例如,在 golang `log` 上使用 rotatefile:

Expand Down
1 change: 1 addition & 0 deletions bufwrite/bufio_writer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package bufwrite provides buffered io.Writer with sync and close methods.
package bufwrite

import (
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ go 1.18

require (
github.com/gookit/color v1.5.3
github.com/gookit/goutil v0.6.8
github.com/gookit/goutil v0.6.9
github.com/gookit/gsr v0.0.8
github.com/valyala/bytebufferpool v1.0.0
)

require (
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
)
17 changes: 9 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/gookit/color v1.5.3 h1:twfIhZs4QLCtimkP7MOxlF3A0U/5cDPseRT9M/+2SCE=
github.com/gookit/color v1.5.3/go.mod h1:NUzwzeehUfl7GIb36pqId+UGmRfQcU/WiiyTTeNjHtE=
github.com/gookit/goutil v0.6.8 h1:B2XXSCGav5TXWtKRT9i/s/owOLXXB7sY6UsfqeSLroE=
github.com/gookit/goutil v0.6.8/go.mod h1:u+Isykc6RQcZ4GQzulsaGm+Famd97U5Tzp3aQyo+jyA=
github.com/gookit/goutil v0.6.9 h1:NrbGKt3cnWoqxjZonC5bTr0FrltTCRdzGReGQwnwhUk=
github.com/gookit/goutil v0.6.9/go.mod h1:ZYNl/t+EIMo0rCRe1mwfC7jKyF/q1FdOEOgrXzJpr5o=
github.com/gookit/gsr v0.0.8 h1:zmkRYe4irObBAskD9UlZCRf8j5VwhhntlYQaSjrrUQk=
github.com/gookit/gsr v0.0.8/go.mod h1:Q3CLTuluDDyk9/Du6xM721lG9/LQ3ywZde9bjmHyWA8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -12,10 +12,11 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
70 changes: 70 additions & 0 deletions rotatefile/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
# Rotate File

`rotatefile` provides simple file rotation, compression and cleanup.

## Install

```bash
go get github.com/gookit/slog/rotatefile
```

## Usage

### Create a file writer

```go
logFile := "testdata/go_logger.log"
writer, err := rotatefile.NewConfig(logFile).Create()
if err != nil {
panic(err)
}
```

### Use on another logger

```go
Expand All @@ -24,3 +42,55 @@ func main() {
log.Println("log message")
}
```

## Available config options

```go
// Config struct for rotate dispatcher
type Config struct {
// Filepath the log file path, will be rotating
Filepath string `json:"filepath" yaml:"filepath"`

// FilePerm for create log file. default DefaultFilePerm
FilePerm os.FileMode `json:"file_perm" yaml:"file_perm"`

// MaxSize file contents max size, unit is bytes.
// If is equals zero, disable rotate file by size
//
// default see DefaultMaxSize
MaxSize uint64 `json:"max_size" yaml:"max_size"`

// RotateTime the file rotate interval time, unit is seconds.
// If is equals zero, disable rotate file by time
//
// default see EveryHour
RotateTime RotateTime `json:"rotate_time" yaml:"rotate_time"`

// CloseLock use sync lock on write contents, rotating file.
//
// default: false
CloseLock bool `json:"close_lock" yaml:"close_lock"`

// BackupNum max number for keep old files.
//
// 0 is not limit, default is DefaultBackNum
BackupNum uint `json:"backup_num" yaml:"backup_num"`

// BackupTime max time for keep old files, unit is hours.
//
// 0 is not limit, default is DefaultBackTime
BackupTime uint `json:"backup_time" yaml:"backup_time"`

// Compress determines if the rotated log files should be compressed using gzip.
// The default is not to perform compression.
Compress bool `json:"compress" yaml:"compress"`

// RenameFunc you can custom-build filename for rotate file by size.
//
// default see DefaultFilenameFn
RenameFunc func(filePath string, rotateNum uint) string

// TimeClock for rotate
TimeClock Clocker
}
```
1 change: 1 addition & 0 deletions rotatefile/rotatefile.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package rotatefile provides simple file rotation, compression and cleanup.
package rotatefile

import (
Expand Down
4 changes: 2 additions & 2 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestUtil_formatArgsWithSpaces(t *testing.T) {
{[]any{float32(12.12), 12.12}, "12.12 12.12"},
{[]any{true, false}, "true false"},
{[]any{[]byte("abc"), []byte("123")}, "abc 123"},
{[]any{timex.OneHour}, "1h0m0s"},
{[]any{timex.OneHour}, "3600000000000"},
{[]any{errorx.Raw("a error message")}, "a error message"},
{[]any{[]int{1, 2, 3}}, "[1 2 3]"},
}
Expand All @@ -68,5 +68,5 @@ func TestUtil_formatArgsWithSpaces(t *testing.T) {
assert.Eq(t, tt.want, formatArgsWithSpaces(tt.args))
}

assert.NotEmpty(t, formatArgsWithSpaces([]any{timex.Now()}))
assert.NotEmpty(t, formatArgsWithSpaces([]any{timex.Now().T()}))
}

0 comments on commit c00791b

Please sign in to comment.