fix logger issues

This commit is contained in:
yanghao05
2023-01-30 11:23:04 +08:00
parent c7345f6252
commit f2c893709d
6 changed files with 194 additions and 7 deletions

View File

@@ -0,0 +1,23 @@
package log
import (
"strings"
"go.uber.org/zap/zapcore"
)
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:
Info(str)
case zapcore.ErrorLevel:
Error(str)
}
return len(p), nil
}

144
providers/log/logger.go Normal file
View File

@@ -0,0 +1,144 @@
package log
import (
"atom/container"
"log"
"go.uber.org/zap"
)
func init() {
if err := container.Container.Provide(NewZapLogger); err != nil {
log.Fatal(err)
}
}
var DefaultLogger *Logger
type Logger struct {
logger *zap.SugaredLogger
}
// Debug uses fmt.Sprint to construct and log a message.
func Debug(args ...interface{}) {
DefaultLogger.logger.Debug(args...)
}
// Info uses fmt.Sprint to construct and log a message.
func Info(args ...interface{}) {
DefaultLogger.logger.Info(args...)
}
// Warn uses fmt.Sprint to construct and log a message.
func Warn(args ...interface{}) {
DefaultLogger.logger.Warn(args...)
}
// Error uses fmt.Sprint to construct and log a message.
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 DPanic(args ...interface{}) {
DefaultLogger.logger.DPanic(args...)
}
// Panic uses fmt.Sprint to construct and log a message, then panics.
func Panic(args ...interface{}) {
DefaultLogger.logger.Panic(args...)
}
// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
func Fatal(args ...interface{}) {
DefaultLogger.logger.Fatal(args...)
}
// Debugf uses fmt.Sprintf to log a templated message.
func Debugf(template string, args ...interface{}) {
DefaultLogger.logger.Debugf(template, args...)
}
// Infof uses fmt.Sprintf to log a templated message.
func Infof(template string, args ...interface{}) {
DefaultLogger.logger.Infof(template, args...)
}
// Warnf uses fmt.Sprintf to log a templated message.
func Warnf(template string, args ...interface{}) {
DefaultLogger.logger.Warnf(template, args...)
}
// Errorf uses fmt.Sprintf to log a templated message.
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 DPanicf(template string, args ...interface{}) {
DefaultLogger.logger.DPanicf(template, args...)
}
// Panicf uses fmt.Sprintf to log a templated message, then panics.
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 Fatalf(template string, args ...interface{}) {
DefaultLogger.logger.Fatalf(template, args...)
}
// Debugw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
//
// When debug-level logging is disabled, this is much faster than
//
// s.With(keysAndValues).Debug(msg)
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 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 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 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 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 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 Fatalw(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.Fatalw(msg, keysAndValues...)
}
// Sync flushes any buffered log entries.
func Sync() error {
return DefaultLogger.logger.Sync()
}

18
providers/log/zap.go Normal file
View File

@@ -0,0 +1,18 @@
package log
import (
"atom/providers/config"
"log"
"go.uber.org/zap"
)
func NewZapLogger(conf *config.Config) (*Logger, error) {
log.Print("init logger")
logger, err := zap.NewDevelopment()
if err != nil {
return nil, err
}
DefaultLogger = &Logger{logger: logger.Sugar()}
return DefaultLogger, nil
}