允许用户自定义打印的拓展字段
This commit is contained in:
+14
-2
@@ -15,6 +15,7 @@ func main() {
|
|||||||
// loggerx.SetTimeZone(time.UTC),
|
// loggerx.SetTimeZone(time.UTC),
|
||||||
loggerx.SetTimeZone(time.FixedZone("CST", 8*3600)),
|
loggerx.SetTimeZone(time.FixedZone("CST", 8*3600)),
|
||||||
loggerx.SetEscapeHTML(false),
|
loggerx.SetEscapeHTML(false),
|
||||||
|
// loggerx.SetExpandData("ddd", "dddd"),
|
||||||
)
|
)
|
||||||
log.WriteAsync().Info(ctx, "{ \"a\": 1, \"b\": 2 }")
|
log.WriteAsync().Info(ctx, "{ \"a\": 1, \"b\": 2 }")
|
||||||
log.Info(ctx, "哈哈哈2")
|
log.Info(ctx, "哈哈哈2")
|
||||||
@@ -25,8 +26,19 @@ func main() {
|
|||||||
log.Info(ctx, "哈哈哈2<")
|
log.Info(ctx, "哈哈哈2<")
|
||||||
log.Info(ctx, "哈哈哈2>")
|
log.Info(ctx, "哈哈哈2>")
|
||||||
|
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 10000; i++ {
|
||||||
log.WriteAsync().Infof(ctx, "异步 %d", i)
|
go func() {
|
||||||
|
log.WriteAsync().Infof(ctx, "异步1 %d", i)
|
||||||
|
}()
|
||||||
|
go log.WriteAsync().Infof(ctx, "异步2 %d", i)
|
||||||
|
log.WriteAsync().Infof(ctx, "异步2 %d", i)
|
||||||
|
}
|
||||||
|
for i := 0; i < 10000; i++ {
|
||||||
|
go func() {
|
||||||
|
log.Infof(ctx, "同步1 %d", i)
|
||||||
|
}()
|
||||||
|
go log.Infof(ctx, "同步2 %d", i)
|
||||||
|
log.Infof(ctx, "同步3 %d", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ func (l *Logger) logger(ctx context.Context, event string, v ...any) {
|
|||||||
Gid: getGID(),
|
Gid: getGID(),
|
||||||
Content: v,
|
Content: v,
|
||||||
TraceId: traceId,
|
TraceId: traceId,
|
||||||
|
Expand: l.option.expandData,
|
||||||
}
|
}
|
||||||
|
|
||||||
if event == "error" {
|
if event == "error" {
|
||||||
@@ -85,4 +86,5 @@ type FormatData struct {
|
|||||||
Content interface{} `json:"content,omitempty"`
|
Content interface{} `json:"content,omitempty"`
|
||||||
TraceId string `json:"traceId,omitempty"`
|
TraceId string `json:"traceId,omitempty"`
|
||||||
Stack string `json:"stack,omitempty"`
|
Stack string `json:"stack,omitempty"`
|
||||||
|
Expand map[string]string `json:"expand,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
+12
@@ -22,6 +22,7 @@ type loggerOption struct {
|
|||||||
sizeSplit int // 根据文件大小切割
|
sizeSplit int // 根据文件大小切割
|
||||||
timeZone *time.Location // 时区
|
timeZone *time.Location // 时区
|
||||||
escapeHTML bool
|
escapeHTML bool
|
||||||
|
expandData map[string]string // 扩展字段
|
||||||
}
|
}
|
||||||
|
|
||||||
type writeType uint8
|
type writeType uint8
|
||||||
@@ -46,6 +47,7 @@ func defaultOptions() loggerOption {
|
|||||||
fileSplit: FileSplitTimeE,
|
fileSplit: FileSplitTimeE,
|
||||||
timeZone: time.Local,
|
timeZone: time.Local,
|
||||||
escapeHTML: true,
|
escapeHTML: true,
|
||||||
|
expandData: make(map[string]string),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,6 +60,16 @@ func SetTraceField(traceField string) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 附加字段
|
||||||
|
func SetExpandData(key string, value string) Option {
|
||||||
|
return func(o *loggerOption) {
|
||||||
|
if o.expandData == nil {
|
||||||
|
o.expandData = make(map[string]string)
|
||||||
|
}
|
||||||
|
o.expandData[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 是否异步写入
|
// 是否异步写入
|
||||||
func SetWriteAsync() Option {
|
func SetWriteAsync() Option {
|
||||||
return func(o *loggerOption) {
|
return func(o *loggerOption) {
|
||||||
|
|||||||
Reference in New Issue
Block a user