go-logger 高性能golang日志库
性能优化是该版本最重要的更新内容。性能优化的结果:
由于压测数据篇幅过长,可以通过 使用文档 或 《go日志库性能基准压力测试:go-logger+slog+zap+log》 查看
如果应用场景为高并发场景,非常建议适用go-logger作为日志库工具,特别是在高并发场景中,需要记录大量日志,或需要调试的情况下,go-logger的并发高效性,使得开启日志记录时,即使大量记录日志数据,也不会影响项目整体性能,项目的正常服务功能可以正常运作。
注意:在实际部署前,还是建议进行充分的测试,确保其满足特定应用的需求。
AttrFormat
通过 AttrFormat
可以对日志的各个属性标识进行自定义格式化设置
示例1:
func Test_AttrFormat(t *testing.T) {
attrformat := &logger.AttrFormat{
SetLevelFmt: func(level logger.LEVELTYPE) string {
switch level {
case logger.LEVEL_DEBUG:
return "debug:"
case logger.LEVEL_INFO:
return "info:"
case logger.LEVEL_WARN:
return "warn:"
case logger.LEVEL_ERROR:
return "error>>>>"
case logger.LEVEL_FATAL:
return "[fatal]"
default:
return "[unknown]"
}
},
SetTimeFmt: func() (string, string, string) {
s := time.Now().Format("2006-01-02 15:04:05")
return s, "", ""
},
}
logger.SetOption(&logger.Option{AttrFormat: attrformat, Console: true, FileOption: &logger.FileTimeMode{Filename: "testlogtime.log", Maxbuckup: 3, IsCompress: false, Timemode: logger.MODE_MONTH}})
logger.Debug("this is a debug message", 1111111111111111111)
logger.Info("this is a info message", 2222222222222222222)
logger.Warn("this is a warn message", 33333333333333333)
logger.Error("this is a error message", 4444444444444444444)
logger.Fatal("this is a fatal message", 555555555555555555)
}
执行结果:
debug:2024-10-26 12:03:21 0_27_0_test.go:33 this is a debug message1111111111111111111
info:2024-10-26 12:03:21 0_27_0_test.go:34 this is a info message2222222222222222222
warn:2024-10-26 12:03:21 0_27_0_test.go:35 this is a warn message33333333333333333
error>>>>2024-10-26 12:03:21 0_27_0_test.go:36 this is a error message4444444444444444444
[fatal]2024-10-26 12:03:21 0_27_0_test.go:37 this is a fatal message555555555555555555
说明:修改了 LEVEL的标识 与时间的格式
示例2
func Test_AttrFormat2(t *testing.T) {
attrformat := &logger.AttrFormat{
SetBodyFmt: func(level logger.LEVELTYPE, bs []byte) []byte {
//处理日志末尾换行符
if size := len(bs); bs[size-1] == '\n' {
bs = append(bs[:size-1], []byte("\x1b[0m\n")...)
} else {
bs = append(bs, []byte("\x1b[0m\n")...)
}
switch level {
case logger.LEVEL_DEBUG:
return append([]byte("\x1b[34m"), bs...)
case logger.LEVEL_INFO:
return append([]byte("\x1b[32m"), bs...)
case logger.LEVEL_WARN:
return append([]byte("\x1b[33m"), bs...)
case logger.LEVEL_ERROR:
return append([]byte("\x1b[31m"), bs...)
case logger.LEVEL_FATAL:
return append([]byte("\x1b[41m"), bs...)
default:
return bs
}
},
}
logger.SetOption(&logger.Option{AttrFormat: attrformat, Console: true, FileOption: &logger.FileTimeMode{Filename: "testlogtime.log", Maxbuckup: 3, IsCompress: false, Timemode: logger.MODE_MONTH}})
logger.Debug("this is a debug message:", 111111111111111110)
logger.Info("this is a info message:", 222222222222222220)
logger.Warn("this is a warn message:", 333333333333333330)
logger.Error("this is a error message:", 4444444444444444440)
logger.Fatal("this is a fatal message:", 5555555555555555550)
}
执行结果
通过 Debugf
, Infof
, Warnf
, Errorf
, Fatalf
等函数,支持在打印函数中使用 % 符号,用于指定输出格式化模式。
示例
func Test_format(t *testing.T) {
logger.Debugf("this is a debugf message:%d", 1)
logger.Infof("this is a infof message:%s", "hi,logger")
logger.Warnf("this is a warnf message:%x,%x", 14, 15)
logger.Errorf("this is a errorf message:%f", 44.4444)
logger.Fatalf("this is a fatalf message:%t", true)
logger.Debugf("this is a debugf message:%p", new(int))
}
执行结果
[DEBUG]2024/10/26 12:08:55 0_27_0_test.go:74 this is a debugf message:1
[INFO]2024/10/26 12:08:55 0_27_0_test.go:75 this is a infof message:hi,logger
[WARN]2024/10/26 12:08:55 0_27_0_test.go:76 this is a warnf message:e,f
[ERROR]2024/10/26 12:08:55 0_27_0_test.go:77 this is a errorf message:44.444400
[FATAL]2024/10/26 12:08:55 0_27_0_test.go:78 this is a fatalf message:true
[DEBUG]2024/10/26 12:08:55 0_27_0_test.go:79 this is a debugf message:0xc00000a938