优化一些option初始值

This commit is contained in:
Yun
2024-04-15 23:40:55 +08:00
parent c7acaa5af6
commit b58849b5b2
3 changed files with 15 additions and 6 deletions
+7 -1
View File
@@ -32,7 +32,7 @@ type filePath struct {
fileName string fileName string
} }
func NewLogger(opts ...Option) *Logger { func NewLogger(ctx context.Context, opts ...Option) *Logger {
opt := defaultOptions() opt := defaultOptions()
for _, apply := range opts { for _, apply := range opts {
apply(&opt) apply(&opt)
@@ -61,6 +61,12 @@ func NewLogger(opts ...Option) *Logger {
// 日志删除 // 日志删除
go l.delete() go l.delete()
// 强制刷盘
go func() {
<-ctx.Done()
l.MustSync()
}()
return l return l
} }
+1
View File
@@ -18,6 +18,7 @@ func TestLogger(t *testing.T) {
b := bytes.NewBuffer(nil) b := bytes.NewBuffer(nil)
l := loggerx.NewLogger( l := loggerx.NewLogger(
context.Background(),
loggerx.SetErrorToInfo(), loggerx.SetErrorToInfo(),
loggerx.SetExtraDriver(b, Print{}), loggerx.SetExtraDriver(b, Print{}),
) )
+6 -4
View File
@@ -16,6 +16,8 @@ type loggerOption struct {
func defaultOptions() loggerOption { func defaultOptions() loggerOption {
return loggerOption{ return loggerOption{
isGinLog: true,
isGid: true,
format: "json", format: "json",
dir: "./log", dir: "./log",
traceField: "trace_id", traceField: "trace_id",
@@ -54,9 +56,9 @@ func SetFormat(format string) Option {
} }
// 是否保存gin的日志 // 是否保存gin的日志
func SetGinLog() Option { func SetGinLog(open bool) Option {
return func(o *loggerOption) { return func(o *loggerOption) {
o.isGinLog = true o.isGinLog = open
} }
} }
@@ -70,9 +72,9 @@ func SetDir(dir string) Option {
} }
// 保存goroutine的ID信息 // 保存goroutine的ID信息
func SetGID() Option { func SetGID(open bool) Option {
return func(o *loggerOption) { return func(o *loggerOption) {
o.isGid = true o.isGid = open
} }
} }