优化zap

This commit is contained in:
2024-11-11 18:51:39 +08:00
parent edb8d30605
commit 355880c3f9
2 changed files with 26 additions and 32 deletions

View File

@@ -82,7 +82,8 @@ func Run() {
defer cancel() defer cancel()
router := leaf.Default(ctx) router := leaf.Default(ctx)
log := zap.NewStdLog(logger.DefaultLogger.Zap())
log, _ := zap.NewStdLogAt(logger.DefaultLogger.ZapLogger(), zap.DebugLevel)
router.SetDebugLogger(log) router.SetDebugLogger(log)
router.DefaultHandler(func(c *leaf.Context) { router.DefaultHandler(func(c *leaf.Context) {

View File

@@ -2,7 +2,6 @@ package logger
import ( import (
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore"
) )
type Logger struct { type Logger struct {
@@ -11,15 +10,15 @@ type Logger struct {
} }
func NewLogger() *Logger { func NewLogger() *Logger {
logger, _ := zap.NewDevelopment(zap.AddCallerSkip(1)) logger, _ := zap.NewDevelopment()
sugar := logger.Sugar() sugar := logger.WithOptions(zap.AddCallerSkip(1)).Sugar()
return &Logger{ return &Logger{
zl: logger, zl: logger,
zs: *sugar, zs: *sugar,
} }
} }
func (l *Logger) Zap() *zap.Logger { func (l *Logger) ZapLogger() *zap.Logger {
return l.zl return l.zl
} }
@@ -68,53 +67,47 @@ func Fatal(args ...interface{}) {
DefaultLogger.zs.Log(zap.FatalLevel, args...) DefaultLogger.zs.Log(zap.FatalLevel, args...)
} }
// Logf formats the message according to the format specifier
// and logs it at provided level.
func Logf(lvl zapcore.Level, template string, args ...interface{}) {
DefaultLogger.zs.Logf(lvl, template, args...)
}
// Debugf formats the message according to the format specifier // Debugf formats the message according to the format specifier
// and logs it at [DebugLevel]. // and logs it at [DebugLevel].
func Debugf(template string, args ...interface{}) { func Debugf(template string, args ...interface{}) {
DefaultLogger.zs.Debugf(template, args...) DefaultLogger.zs.Logf(zap.DebugLevel, template, args...)
} }
// Infof formats the message according to the format specifier // Infof formats the message according to the format specifier
// and logs it at [InfoLevel]. // and logs it at [InfoLevel].
func Infof(template string, args ...interface{}) { func Infof(template string, args ...interface{}) {
DefaultLogger.zs.Infof(template, args...) DefaultLogger.zs.Logf(zap.InfoLevel, template, args...)
} }
// Warnf formats the message according to the format specifier // Warnf formats the message according to the format specifier
// and logs it at [WarnLevel]. // and logs it at [WarnLevel].
func Warnf(template string, args ...interface{}) { func Warnf(template string, args ...interface{}) {
DefaultLogger.zs.Warnf(template, args...) DefaultLogger.zs.Logf(zap.WarnLevel, template, args...)
} }
// Errorf formats the message according to the format specifier // Errorf formats the message according to the format specifier
// and logs it at [ErrorLevel]. // and logs it at [ErrorLevel].
func Errorf(template string, args ...interface{}) { func Errorf(template string, args ...interface{}) {
DefaultLogger.zs.Errorf(template, args...) DefaultLogger.zs.Logf(zap.ErrorLevel, template, args...)
} }
// DPanicf formats the message according to the format specifier // DPanicf formats the message according to the format specifier
// and logs it at [DPanicLevel]. // and logs it at [DPanicLevel].
// In development, the logger then panics. (See [DPanicLevel] for details.) // In development, the logger then panics. (See [DPanicLevel] for details.)
func DPanicf(template string, args ...interface{}) { func DPanicf(template string, args ...interface{}) {
DefaultLogger.zs.DPanicf(template, args...) DefaultLogger.zs.Logf(zap.DPanicLevel, template, args...)
} }
// Panicf formats the message according to the format specifier // Panicf formats the message according to the format specifier
// and panics. // and panics.
func Panicf(template string, args ...interface{}) { func Panicf(template string, args ...interface{}) {
DefaultLogger.zs.Panicf(template, args...) DefaultLogger.zs.Logf(zap.PanicLevel, template, args...)
} }
// Fatalf formats the message according to the format specifier // Fatalf formats the message according to the format specifier
// and calls os.Exit. // and calls os.Exit.
func Fatalf(template string, args ...interface{}) { func Fatalf(template string, args ...interface{}) {
DefaultLogger.zs.Fatalf(template, args...) DefaultLogger.zs.Logf(zap.FatalLevel, template, args...)
} }
// Debugw logs a message with some additional context. The variadic key-value // Debugw logs a message with some additional context. The variadic key-value
@@ -124,85 +117,85 @@ func Fatalf(template string, args ...interface{}) {
// //
// s.With(keysAndValues).Debug(msg) // s.With(keysAndValues).Debug(msg)
func Debugw(msg string, keysAndValues ...interface{}) { func Debugw(msg string, keysAndValues ...interface{}) {
DefaultLogger.zs.Debugw(msg, keysAndValues...) DefaultLogger.zs.Logw(zap.DebugLevel, msg, keysAndValues...)
} }
// Infow logs a message with some additional context. The variadic key-value // Infow logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With. // pairs are treated as they are in With.
func Infow(msg string, keysAndValues ...interface{}) { func Infow(msg string, keysAndValues ...interface{}) {
DefaultLogger.zs.Infow(msg, keysAndValues...) DefaultLogger.zs.Logw(zap.InfoLevel, msg, keysAndValues...)
} }
// Warnw logs a message with some additional context. The variadic key-value // Warnw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With. // pairs are treated as they are in With.
func Warnw(msg string, keysAndValues ...interface{}) { func Warnw(msg string, keysAndValues ...interface{}) {
DefaultLogger.zs.Warnw(msg, keysAndValues...) DefaultLogger.zs.Logw(zap.WarnLevel, msg, keysAndValues...)
} }
// Errorw logs a message with some additional context. The variadic key-value // Errorw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With. // pairs are treated as they are in With.
func Errorw(msg string, keysAndValues ...interface{}) { func Errorw(msg string, keysAndValues ...interface{}) {
DefaultLogger.zs.Errorw(msg, keysAndValues...) DefaultLogger.zs.Logw(zap.ErrorLevel, msg, keysAndValues...)
} }
// DPanicw logs a message with some additional context. In development, the // DPanicw logs a message with some additional context. In development, the
// logger then panics. (See DPanicLevel for details.) The variadic key-value // logger then panics. (See DPanicLevel for details.) The variadic key-value
// pairs are treated as they are in With. // pairs are treated as they are in With.
func DPanicw(msg string, keysAndValues ...interface{}) { func DPanicw(msg string, keysAndValues ...interface{}) {
DefaultLogger.zs.DPanicw(msg, keysAndValues...) DefaultLogger.zs.Logw(zap.DPanicLevel, msg, keysAndValues...)
} }
// Panicw logs a message with some additional context, then panics. The // Panicw logs a message with some additional context, then panics. The
// variadic key-value pairs are treated as they are in With. // variadic key-value pairs are treated as they are in With.
func Panicw(msg string, keysAndValues ...interface{}) { func Panicw(msg string, keysAndValues ...interface{}) {
DefaultLogger.zs.Panicw(msg, keysAndValues...) DefaultLogger.zs.Logw(zap.PanicLevel, msg, keysAndValues...)
} }
// Fatalw logs a message with some additional context, then calls os.Exit. The // Fatalw logs a message with some additional context, then calls os.Exit. The
// variadic key-value pairs are treated as they are in With. // variadic key-value pairs are treated as they are in With.
func Fatalw(msg string, keysAndValues ...interface{}) { func Fatalw(msg string, keysAndValues ...interface{}) {
DefaultLogger.zs.Fatalw(msg, keysAndValues...) DefaultLogger.zs.Logw(zap.FatalLevel, msg, keysAndValues...)
} }
// Debugln logs a message at [DebugLevel]. // Debugln logs a message at [DebugLevel].
// Spaces are always added between arguments. // Spaces are always added between arguments.
func Debugln(args ...interface{}) { func Debugln(args ...interface{}) {
DefaultLogger.zs.Debugln(args...) DefaultLogger.zs.Logln(zap.DebugLevel, args...)
} }
// Infoln logs a message at [InfoLevel]. // Infoln logs a message at [InfoLevel].
// Spaces are always added between arguments. // Spaces are always added between arguments.
func Infoln(args ...interface{}) { func Infoln(args ...interface{}) {
DefaultLogger.zs.Infoln(args...) DefaultLogger.zs.Logln(zap.InfoLevel, args...)
} }
// Warnln logs a message at [WarnLevel]. // Warnln logs a message at [WarnLevel].
// Spaces are always added between arguments. // Spaces are always added between arguments.
func Warnln(args ...interface{}) { func Warnln(args ...interface{}) {
DefaultLogger.zs.Warnln(args...) DefaultLogger.zs.Logln(zap.WarnLevel, args...)
} }
// Errorln logs a message at [ErrorLevel]. // Errorln logs a message at [ErrorLevel].
// Spaces are always added between arguments. // Spaces are always added between arguments.
func Errorln(args ...interface{}) { func Errorln(args ...interface{}) {
DefaultLogger.zs.Errorln(args...) DefaultLogger.zs.Logln(zap.ErrorLevel, args...)
} }
// DPanicln logs a message at [DPanicLevel]. // DPanicln logs a message at [DPanicLevel].
// In development, the logger then panics. (See [DPanicLevel] for details.) // In development, the logger then panics. (See [DPanicLevel] for details.)
// Spaces are always added between arguments. // Spaces are always added between arguments.
func DPanicln(args ...interface{}) { func DPanicln(args ...interface{}) {
DefaultLogger.zs.DPanicln(args...) DefaultLogger.zs.Logln(zap.DPanicLevel, args...)
} }
// Panicln logs a message at [PanicLevel] and panics. // Panicln logs a message at [PanicLevel] and panics.
// Spaces are always added between arguments. // Spaces are always added between arguments.
func Panicln(args ...interface{}) { func Panicln(args ...interface{}) {
DefaultLogger.zs.Panicln(args...) DefaultLogger.zs.Logln(zap.PanicLevel, args...)
} }
// Fatalln logs a message at [FatalLevel] and calls os.Exit. // Fatalln logs a message at [FatalLevel] and calls os.Exit.
// Spaces are always added between arguments. // Spaces are always added between arguments.
func Fatalln(args ...interface{}) { func Fatalln(args ...interface{}) {
DefaultLogger.zs.Fatalln(args...) DefaultLogger.zs.Logln(zap.FatalLevel, args...)
} }