添加支持更多的文件驱动
This commit is contained in:
+18
-1
@@ -1,7 +1,9 @@
|
|||||||
package loggerx_test
|
package loggerx_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -13,7 +15,12 @@ import (
|
|||||||
|
|
||||||
func TestLogger(t *testing.T) {
|
func TestLogger(t *testing.T) {
|
||||||
|
|
||||||
l := loggerx.NewLogger(loggerx.SetErrorToInfo())
|
b := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
|
l := loggerx.NewLogger(
|
||||||
|
loggerx.SetErrorToInfo(),
|
||||||
|
loggerx.SetExtraDriver(b, Print{}),
|
||||||
|
)
|
||||||
|
|
||||||
l.Error(context.Background(), "test error")
|
l.Error(context.Background(), "test error")
|
||||||
|
|
||||||
@@ -22,5 +29,15 @@ func TestLogger(t *testing.T) {
|
|||||||
|
|
||||||
l.Info(context.Background(), "test info")
|
l.Info(context.Background(), "test info")
|
||||||
|
|
||||||
|
fmt.Println(b.String())
|
||||||
|
|
||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 5)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Print struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pp Print) Write(p []byte) (n int, err error) {
|
||||||
|
fmt.Print("ppppppppppppppp",string(p))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
+20
-3
@@ -1,14 +1,17 @@
|
|||||||
package loggerx
|
package loggerx
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
type loggerOption struct {
|
type loggerOption struct {
|
||||||
prefix string // 日志前缀
|
prefix string // 日志前缀
|
||||||
format string // text json
|
format string // text json
|
||||||
dir string // 文件目录
|
dir string // 文件目录
|
||||||
isGinLog bool
|
isGinLog bool
|
||||||
isGid bool
|
isGid bool
|
||||||
traceField string // trace字段
|
traceField string // trace字段
|
||||||
errorToInfo bool // 错误日志是否写入info日志
|
errorToInfo bool // 错误日志是否写入info日志
|
||||||
days int // 日志保存天数
|
days int // 日志保存天数
|
||||||
|
drivers []io.Writer // 文件落盘驱动器
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultOptions() loggerOption {
|
func defaultOptions() loggerOption {
|
||||||
@@ -80,6 +83,20 @@ func SetDays(days int) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 设置时区
|
||||||
|
func SetTimeZone() Option {
|
||||||
|
return func(o *loggerOption) {
|
||||||
|
// o.timeZone = timeZone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 文件额外的驱动
|
||||||
|
func SetExtraDriver(ds ...io.Writer) Option {
|
||||||
|
return func(o *loggerOption) {
|
||||||
|
o.drivers = ds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 文件切割规则
|
// 文件切割规则
|
||||||
// 1.文件大小
|
// 1.文件大小
|
||||||
// 2.时间A(年/月/日/时)
|
// 2.时间A(年/月/日/时)
|
||||||
|
|||||||
+2
-1
@@ -18,5 +18,6 @@ func (l *Logger) write(event string, b []byte) (n int, err error) {
|
|||||||
// 强制更新
|
// 强制更新
|
||||||
l.getFile(event, true)
|
l.getFile(event, true)
|
||||||
}
|
}
|
||||||
return f.Write(b)
|
d := append(l.option.drivers, f)
|
||||||
|
return io.MultiWriter(d...).Write(b)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user