SugaredLogger
// A SugaredLogger wraps the base Logger functionality in a slower, but less
// verbose, API. Any Logger can be converted to a SugaredLogger with its Sugar
// method.
//
// Unlike the Logger, the SugaredLogger doesn't insist on structured logging.
// For each log level, it exposes three methods: one for loosely-typed
// structured logging, one for println-style formatting, and one for
// printf-style formatting. For example, SugaredLoggers can produce InfoLevel
// output with Infow ("info with" structured context), Info, or Infof.
type SugaredLogger struct {
base *Logger
}
先看下注释,SugaredLogger用一个较慢但不太冗长的API包装基本Logger功能。Logger可以使用Sugar方法转换为SugaredLogger。SugaredLogger可以不用结构化log。针对每个log level,提供了三个方法:格式宽松的结构化log,println格式化的log,printf格式化的log。例如,可以使用Infow,Info,Infof生成InfoLevel的输出。
我们就以InfoLevel的三个方法为例,看下具体的处理过程。
InfoLevel
// Info uses fmt.Sprint to construct and log a message.
func (s *SugaredLogger) Info(args ...interface{
}) {
s.log(InfoLevel, "", args, nil)
}
// Infof uses fmt.Sprintf to log a templated message.
func (s *SugaredLogger) Infof(template string, args ...interface{
}) {
s.log(InfoLevel, template, args, nil)
}
// Infow logs a message with some additional context. The variadic key-value

本文深入解析SugaredLogger的工作原理,它通过提供更灵活的日志格式化选项,如Infow、Infof和Info,来增强基本Logger的功能,但可能引入额外的性能开销和内存分配。
最低0.47元/天 解锁文章
1310

被折叠的 条评论
为什么被折叠?



