support logger auto call

This commit is contained in:
yanghao05
2023-01-29 15:27:50 +08:00
parent 34b3978c5a
commit 7151c52543
13 changed files with 284 additions and 589 deletions

View File

@@ -2,119 +2,93 @@ package logger
import (
"atom/container"
"atom/providers/config"
"log"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func init() {
if err := container.Container.Provide(NewLogger); err != nil {
if err := container.Container.Provide(NewZapLogger); err != nil {
log.Fatal(err)
}
}
var DefaultLogger *Logger
type Logger struct {
logger *zap.SugaredLogger
}
type LevelWriter struct {
Logger *Logger
Level zapcore.Level
}
func (w LevelWriter) Write(p []byte) (n int, err error) {
str := strings.TrimSpace(string(p))
switch w.Level {
case zapcore.InfoLevel:
w.Logger.Info(str)
case zapcore.ErrorLevel:
w.Logger.Info(str)
}
return len(p), nil
}
func NewLogger(conf *config.Config) (*Logger, error) {
logger, err := zap.NewDevelopment()
if err != nil {
return nil, err
}
return &Logger{logger: logger.Sugar()}, nil
}
// Debug uses fmt.Sprint to construct and log a message.
func (l *Logger) Debug(args ...interface{}) {
l.logger.Debug(args...)
func Debug(args ...interface{}) {
DefaultLogger.logger.Debug(args...)
}
// Info uses fmt.Sprint to construct and log a message.
func (l *Logger) Info(args ...interface{}) {
l.logger.Info(args...)
func Info(args ...interface{}) {
DefaultLogger.logger.Info(args...)
}
// Warn uses fmt.Sprint to construct and log a message.
func (l *Logger) Warn(args ...interface{}) {
l.logger.Warn(args...)
func Warn(args ...interface{}) {
DefaultLogger.logger.Warn(args...)
}
// Error uses fmt.Sprint to construct and log a message.
func (l *Logger) Error(args ...interface{}) {
l.logger.Error(args...)
func Error(args ...interface{}) {
DefaultLogger.logger.Error(args...)
}
// DPanic uses fmt.Sprint to construct and log a message. In development, the
// logger then panics. (See DPanicLevel for details.)
func (l *Logger) DPanic(args ...interface{}) {
l.logger.DPanic(args...)
func DPanic(args ...interface{}) {
DefaultLogger.logger.DPanic(args...)
}
// Panic uses fmt.Sprint to construct and log a message, then panics.
func (l *Logger) Panic(args ...interface{}) {
l.logger.Panic(args...)
func Panic(args ...interface{}) {
DefaultLogger.logger.Panic(args...)
}
// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
func (l *Logger) Fatal(args ...interface{}) {
l.logger.Fatal(args...)
func Fatal(args ...interface{}) {
DefaultLogger.logger.Fatal(args...)
}
// Debugf uses fmt.Sprintf to log a templated message.
func (l *Logger) Debugf(template string, args ...interface{}) {
l.logger.Debugf(template, args...)
func Debugf(template string, args ...interface{}) {
DefaultLogger.logger.Debugf(template, args...)
}
// Infof uses fmt.Sprintf to log a templated message.
func (l *Logger) Infof(template string, args ...interface{}) {
l.logger.Infof(template, args...)
func Infof(template string, args ...interface{}) {
DefaultLogger.logger.Infof(template, args...)
}
// Warnf uses fmt.Sprintf to log a templated message.
func (l *Logger) Warnf(template string, args ...interface{}) {
l.logger.Warnf(template, args...)
func Warnf(template string, args ...interface{}) {
DefaultLogger.logger.Warnf(template, args...)
}
// Errorf uses fmt.Sprintf to log a templated message.
func (l *Logger) Errorf(template string, args ...interface{}) {
l.logger.Errorf(template, args...)
func Errorf(template string, args ...interface{}) {
DefaultLogger.logger.Errorf(template, args...)
}
// DPanicf uses fmt.Sprintf to log a templated message. In development, the
// logger then panics. (See DPanicLevel for details.)
func (l *Logger) DPanicf(template string, args ...interface{}) {
l.logger.DPanicf(template, args...)
func DPanicf(template string, args ...interface{}) {
DefaultLogger.logger.DPanicf(template, args...)
}
// Panicf uses fmt.Sprintf to log a templated message, then panics.
func (l *Logger) Panicf(template string, args ...interface{}) {
l.logger.Panicf(template, args...)
func Panicf(template string, args ...interface{}) {
DefaultLogger.logger.Panicf(template, args...)
}
// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
func (l *Logger) Fatalf(template string, args ...interface{}) {
l.logger.Fatalf(template, args...)
func Fatalf(template string, args ...interface{}) {
DefaultLogger.logger.Fatalf(template, args...)
}
// Debugw logs a message with some additional context. The variadic key-value
@@ -123,48 +97,48 @@ func (l *Logger) Fatalf(template string, args ...interface{}) {
// When debug-level logging is disabled, this is much faster than
//
// s.With(keysAndValues).Debug(msg)
func (l *Logger) Debugw(msg string, keysAndValues ...interface{}) {
l.logger.Debugw(msg, keysAndValues...)
func Debugw(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.Debugw(msg, keysAndValues...)
}
// Infow logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l *Logger) Infow(msg string, keysAndValues ...interface{}) {
l.logger.Infow(msg, keysAndValues...)
func Infow(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.Infow(msg, keysAndValues...)
}
// Warnw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l *Logger) Warnw(msg string, keysAndValues ...interface{}) {
l.logger.Warnw(msg, keysAndValues...)
func Warnw(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.Warnw(msg, keysAndValues...)
}
// Errorw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l *Logger) Errorw(msg string, keysAndValues ...interface{}) {
l.logger.Errorw(msg, keysAndValues...)
func Errorw(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.Errorw(msg, keysAndValues...)
}
// DPanicw logs a message with some additional context. In development, the
// logger then panics. (See DPanicLevel for details.) The variadic key-value
// pairs are treated as they are in With.
func (l *Logger) DPanicw(msg string, keysAndValues ...interface{}) {
l.logger.DPanicw(msg, keysAndValues...)
func DPanicw(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.DPanicw(msg, keysAndValues...)
}
// Panicw logs a message with some additional context, then panics. The
// variadic key-value pairs are treated as they are in With.
func (l *Logger) Panicw(msg string, keysAndValues ...interface{}) {
l.logger.Panicw(msg, keysAndValues...)
func Panicw(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.Panicw(msg, keysAndValues...)
}
// Fatalw logs a message with some additional context, then calls os.Exit. The
// variadic key-value pairs are treated as they are in With.
func (l *Logger) Fatalw(msg string, keysAndValues ...interface{}) {
l.logger.Fatalw(msg, keysAndValues...)
func Fatalw(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.Fatalw(msg, keysAndValues...)
}
// Sync flushes any buffered log entries.
func (l *Logger) Sync() error {
return l.logger.Sync()
func Sync() error {
return DefaultLogger.logger.Sync()
}