go-logger 是一个轻量级的日志库,提供了灵活的日志记录功能,方便在应用程序中实现不同级别的日志输出.
go-logger
提供了一个简单直观的 API,使得开发者能够快速上手并集成到现有项目中。CustomHandler func(lc *LogContext) bool
通过Option对象添加 CustomHandler 函数,go-logger的执行日志打印前,将执行CustomHandler 函数,如果CustomHandler 返回true,则继续执行go-logger的打印程序,如果CustomHandler 返回false,则不再执行打印程序。
示例:
func TestCustomHandler(t *testing.T) {
SetOption(&Option{Console: true, CustomHandler: func(lc *LogContext) bool {
fmt.Println("level:", levelname(lc.Level))
fmt.Println("message:", fmt.Sprint(lc.Args...))
if lc.Level == LEVEL_ERROR {
return false //if error message , do not print
}
return true
},
})
Debug("this is a debug message")
Info("this is a info message")
Warn("this is a warn message")
Error("this is a error message")
}
执行结果:根据CustomHandler的逻辑,Error日志返回false,则Error日志将不被打印出来
level: debug
message: this is a debug message
[DEBUG]2024/08/07 18:57:06 logging_test.go:126 this is a debug message
level: info
message: this is a info message
[INFO]2024/08/07 18:57:06 logging_test.go:127 this is a info message
level: warn
message: this is a warn message
[WARN]2024/08/07 18:57:06 logging_test.go:128 this is a warn message
level: error
message: this is a error message
日志堆栈信息具备一定的日志追踪功能,可以追踪到调用打印日志函数的每个函数的层层嵌套关系,包括函数名,位置信息等
Stacktrace
示例
func TestStacktrace(t *testing.T) {
SetOption(&Option{Console: true, Stacktrace: LEVEL_WARN, Format: FORMAT_LEVELFLAG | FORMAT_DATE | FORMAT_TIME | FORMAT_SHORTFILENAME | FORMAT_FUNC})
Debug("this is a debug message")
Stacktrace1()
}
func Stacktrace1() {
Info("this is a info message")
Stacktrace2()
}
func Stacktrace2() {
Warn("this is a warn message")
Stacktrace3()
}
func Stacktrace3() {
Error("this is a error message")
Fatal("this is a fatal message")
}
执行结果
[DEBUG]2024/08/07 20:22:40 logging_test.go:TestStacktrace:151 this is a debug message
[INFO]2024/08/07 20:22:40 logging_test.go:Stacktrace1:156 this is a info message
[WARN]2024/08/07 20:22:40 logging_test.go:Stacktrace2:161#logging_test.go:Stacktrace1:157#logging_test.go:TestStacktrace:152#testing.go:tRunner:1689#asm_amd64.s:goexit:1695 this is a warn message
[ERROR]2024/08/07 20:22:40 logging_test.go:Stacktrace3:166#logging_test.go:Stacktrace2:162#logging_test.go:Stacktrace1:157#logging_test.go:TestStacktrace:152#testing.go:tRunner:1689#asm_amd64.s:goexit:1695 this is a error message
[FATAL]2024/08/07 20:22:40 logging_test.go:Stacktrace3:167#logging_test.go:Stacktrace2:162#logging_test.go:Stacktrace1:157#logging_test.go:TestStacktrace:152#testing.go:tRunner:1689#asm_amd64.s:goexit:1695 this is a fatal message
可以看到,Warn,Error,Fatal级别的日志,记录了调用日志打印函数的堆栈信息
由日志内容:
[FATAL]2024/08/07 20:22:40 logging_test.go:Stacktrace3:167#logging_test.go:Stacktrace2:162#logging_test.go:Stacktrace1:157#logging_test.go:TestStacktrace:152
可以看到FATAL日志调用函数 Stacktrace3 >Stacktrace2 >Stacktrace1 >TestStacktrace 等函数之间的调用关系
通过 SetLevelOption
函数,可以设置不同日志级别的独立日志输出格式
示例
func TestLevelOptions(t *testing.T) {
SetLevelOption(LEVEL_DEBUG, &LevelOption{Format: FORMAT_LEVELFLAG | FORMAT_TIME | FORMAT_SHORTFILENAME})
SetLevelOption(LEVEL_INFO, &LevelOption{Format: FORMAT_LEVELFLAG})
SetLevelOption(LEVEL_WARN, &LevelOption{Format: FORMAT_LEVELFLAG | FORMAT_TIME | FORMAT_SHORTFILENAME | FORMAT_DATE | FORMAT_FUNC})
Debug("this is a debug message")
Info("this is a info message")
Warn("this is a warn message")
}
执行结果
[DEBUG]18:58:18 logging_test.go:175 this is a debug message
[INFO]this is a info message
[WARN]2024/08/07 18:58:18 logging_test.go:TestLevelOptions:177 this is a warn message
可以看到,Debug,Info,Warn 级别的日志,分别是不同的日志输出格式
所有的功能增加与优化,都建立在不影响go-logger性能的基础上,当前版本性能压测数据如下:
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
BenchmarkSerialZap
BenchmarkSerialZap-4 714796 5469 ns/op 336 B/op 6 allocs/op
BenchmarkSerialZap-8 675508 5316 ns/op 337 B/op 6 allocs/op
BenchmarkSerialLogger
BenchmarkSerialLogger-4 749774 4458 ns/op 152 B/op 4 allocs/op
BenchmarkSerialLogger-8 793208 4321 ns/op 152 B/op 4 allocs/op
BenchmarkSerialLoggerNoFORMAT
BenchmarkSerialLoggerNoFORMAT-4 977128 3767 ns/op 128 B/op 2 allocs/op
BenchmarkSerialLoggerNoFORMAT-8 1000000 3669 ns/op 128 B/op 2 allocs/op
BenchmarkSerialLoggerWrite
BenchmarkSerialLoggerWrite-4 856617 3659 ns/op 112 B/op 1 allocs/op
BenchmarkSerialLoggerWrite-8 1000000 3576 ns/op 112 B/op 1 allocs/op
BenchmarkSerialNativeGoLog
BenchmarkSerialNativeGoLog-4 892172 4488 ns/op 232 B/op 2 allocs/op
BenchmarkSerialNativeGoLog-8 798291 4327 ns/op 232 B/op 2 allocs/op
BenchmarkSerialSlog
BenchmarkSerialSlog-4 634228 5602 ns/op 328 B/op 6 allocs/op
BenchmarkSerialSlog-8 646191 5481 ns/op 328 B/op 6 allocs/op
BenchmarkSerialSlogAndLogger
BenchmarkSerialSlogAndLogger-4 626898 5671 ns/op 328 B/op 6 allocs/op
BenchmarkSerialSlogAndLogger-8 657820 5622 ns/op 328 B/op 6 allocs/op
BenchmarkParallelZap
BenchmarkParallelZap-4 430472 7818 ns/op 336 B/op 6 allocs/op
BenchmarkParallelZap-8 449402 7771 ns/op 337 B/op 6 allocs/op
BenchmarkParallelLogger
BenchmarkParallelLogger-4 639826 5398 ns/op 152 B/op 4 allocs/op
BenchmarkParallelLogger-8 604308 5532 ns/op 152 B/op 4 allocs/op
BenchmarkParallelLoggerNoFORMAT
BenchmarkParallelLoggerNoFORMAT-4 806749 4311 ns/op 128 B/op 2 allocs/op
BenchmarkParallelLoggerNoFORMAT-8 790284 4592 ns/op 128 B/op 2 allocs/op
BenchmarkParallelLoggerWrite
BenchmarkParallelLoggerWrite-4 764610 4141 ns/op 112 B/op 1 allocs/op
BenchmarkParallelLoggerWrite-8 880222 4079 ns/op 112 B/op 1 allocs/op
BenchmarkParallelNativeGoLog
BenchmarkParallelNativeGoLog-4 609134 5652 ns/op 232 B/op 2 allocs/op
BenchmarkParallelNativeGoLog-8 588201 5806 ns/op 232 B/op 2 allocs/op
BenchmarkParallelSLog
BenchmarkParallelSLog-4 620878 5624 ns/op 328 B/op 6 allocs/op
BenchmarkParallelSLog-8 636448 5532 ns/op 328 B/op 6 allocs/op
BenchmarkParallelSLogAndgoLogger
BenchmarkParallelSLogAndgoLogger-4 612314 5612 ns/op 328 B/op 6 allocs/op
BenchmarkParallelSLogAndgoLogger-8 633426 5596 ns/op 328 B/op 6 allocs/op
log
包。-4
表示使用 4 个核心,而 -8
表示使用 8 个核心。