支持异步写入

This commit is contained in:
Yun
2024-11-22 20:08:25 +08:00
parent 7c70f805db
commit 33706c1072
5 changed files with 90 additions and 12 deletions
+18
View File
@@ -13,6 +13,7 @@ type loggerOption struct {
isGinLog bool
isGid bool
isPrintFile bool
writeType writeType // 是否异步罗盘
traceField string // trace字段
errorToInfo bool // 错误日志是否写入info日志
days int // 日志保存天数
@@ -22,11 +23,21 @@ type loggerOption struct {
timeZone *time.Location // 时区
}
type writeType uint8
const (
// 0.默认(同步) 1.指定同步 2.指定异步
writeTypeDefault writeType = iota
writeTypeSync
writeTypeAsync
)
func defaultOptions() loggerOption {
return loggerOption{
isGinLog: true,
isGid: true,
isPrintFile: true,
writeType: writeTypeDefault, // 默认同步
format: "json",
dir: "./log",
traceField: "trace_id",
@@ -45,6 +56,13 @@ func SetTraceField(traceField string) Option {
}
}
// 是否异步写入
func SetWriteAsync() Option {
return func(o *loggerOption) {
o.writeType = writeTypeAsync
}
}
// 打印到控制台
func SetToConsole() Option {
return func(o *loggerOption) {