remove zap log package Close #19
	
		
	
				
					
				
			
							parent
							
								
									37eb578795
								
							
						
					
					
						commit
						bebf43e2eb
					
				| @ -1,120 +1,103 @@ | ||||
| package log | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"log" | ||||
| 	"os" | ||||
| 	"strings" | ||||
| 
 | ||||
| 	"go.uber.org/zap" | ||||
| 	"go.uber.org/zap/zapcore" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
| 	Prefix = "[x]: " | ||||
| ) | ||||
| 
 | ||||
| var ( | ||||
| 	formatLogger *zap.SugaredLogger | ||||
| 	levelMap     = map[string]zapcore.Level{ | ||||
| 		"debug": zapcore.DebugLevel, | ||||
| 		"info":  zapcore.InfoLevel, | ||||
| 		"warn":  zapcore.WarnLevel, | ||||
| 		"error": zapcore.ErrorLevel, | ||||
| 		"panic": zapcore.PanicLevel, | ||||
| 		"fatal": zapcore.FatalLevel, | ||||
| 	} | ||||
| ) | ||||
| 
 | ||||
| func InitLog(level string) { | ||||
| 	logger := newLogger(level) | ||||
| 	formatLogger = logger.Sugar() | ||||
| } | ||||
| type Level int | ||||
| 
 | ||||
| func newLogger(level string) *zap.Logger { | ||||
| 	core := newCore(level) | ||||
| 	return zap.New(core, | ||||
| 		zap.AddCaller(), | ||||
| 		zap.AddCallerSkip(1), | ||||
| 		zap.Development(), | ||||
| 	) | ||||
| } | ||||
| const ( | ||||
| 	LevelDebug Level = iota | ||||
| 	LevelWarn | ||||
| 	LevelError | ||||
| ) | ||||
| 
 | ||||
| func newCore(level string) zapcore.Core { | ||||
| 	encoderConfig := zapcore.EncoderConfig{ | ||||
| 		TimeKey:        "time", | ||||
| 		LevelKey:       "level", | ||||
| 		NameKey:        "logger", | ||||
| 		CallerKey:      "line", | ||||
| 		MessageKey:     "msg", | ||||
| 		StacktraceKey:  "stacktrace", | ||||
| 		LineEnding:     zapcore.DefaultLineEnding, | ||||
| 		EncodeLevel:    zapcore.CapitalLevelEncoder, | ||||
| 		EncodeTime:     zapcore.ISO8601TimeEncoder, | ||||
| 		EncodeDuration: zapcore.SecondsDurationEncoder, | ||||
| 		EncodeCaller:   zapcore.ShortCallerEncoder, | ||||
| 		EncodeName:     zapcore.FullNameEncoder, | ||||
| func (l Level) String() string { | ||||
| 	switch l { | ||||
| 	case LevelDebug: | ||||
| 		return "debug" | ||||
| 	case LevelError: | ||||
| 		return "error" | ||||
| 	} | ||||
| 	return zapcore.NewCore( | ||||
| 		zapcore.NewConsoleEncoder(encoderConfig), | ||||
| 		zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout)), | ||||
| 		zap.NewAtomicLevelAt(getLoggerLevel(level)), | ||||
| 	) | ||||
| 	return "" | ||||
| } | ||||
| 
 | ||||
| func getLoggerLevel(lvl string) zapcore.Level { | ||||
| 	if level, ok := levelMap[strings.ToLower(lvl)]; ok { | ||||
| 		return level | ||||
| var ( | ||||
| 	formatLogger *Logger | ||||
| 	levelMap     = map[string]Level{ | ||||
| 		"debug": LevelDebug, | ||||
| 		"error": LevelError, | ||||
| 	} | ||||
| 	return zapcore.InfoLevel | ||||
| } | ||||
| ) | ||||
| 
 | ||||
| func Debug(args ...interface{}) { | ||||
| 	formatLogger.Debug(args...) | ||||
| func InitLog(l string) { | ||||
| 	formatLogger = newLog(os.Stdout).setLevel(levelMap[l]).setFlags(log.Lshortfile) | ||||
| } | ||||
| 
 | ||||
| func Debugf(template string, args ...interface{}) { | ||||
| 	formatLogger.Debugf(template, args...) | ||||
| type Logger struct { | ||||
| 	level Level | ||||
| 	l     *log.Logger | ||||
| } | ||||
| 
 | ||||
| func Info(args ...interface{}) { | ||||
| 	formatLogger.Info(args...) | ||||
| func newLog(w io.Writer) *Logger { | ||||
| 	return &Logger{ | ||||
| 		l: log.New(w, "", 0), | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func Infof(template string, args ...interface{}) { | ||||
| 	formatLogger.Infof(template, args...) | ||||
| func (l *Logger) setFlags(flag int) *Logger { | ||||
| 	l.l.SetFlags(flag) | ||||
| 	return l | ||||
| } | ||||
| 
 | ||||
| func Warn(args ...interface{}) { | ||||
| 	formatLogger.Warn(args...) | ||||
| func (l *Logger) setLevel(level Level) *Logger { | ||||
| 	l.level = level | ||||
| 	return l | ||||
| } | ||||
| 
 | ||||
| func Warnf(template string, args ...interface{}) { | ||||
| 	formatLogger.Warnf(template, args...) | ||||
| func (l *Logger) doLog(level Level, v ...interface{}) bool { | ||||
| 	if level < l.level { | ||||
| 		return false | ||||
| 	} | ||||
| 	l.l.Output(3, level.String()+" "+fmt.Sprintln(v...)) | ||||
| 	return true | ||||
| } | ||||
| 
 | ||||
| func Error(args ...interface{}) { | ||||
| 	formatLogger.Error(args...) | ||||
| func (l *Logger) doLogf(level Level, format string, v ...interface{}) bool { | ||||
| 	if level < l.level { | ||||
| 		return false | ||||
| 	} | ||||
| 	l.l.Output(3, level.String()+" "+fmt.Sprintln(fmt.Sprintf(format, v...))) | ||||
| 	return true | ||||
| } | ||||
| 
 | ||||
| func Errorf(template string, args ...interface{}) { | ||||
| 	formatLogger.Errorf(template, args...) | ||||
| func Debug(v ...interface{}) { | ||||
| 	formatLogger.doLog(LevelDebug, v...) | ||||
| } | ||||
| 
 | ||||
| func Panic(args ...interface{}) { | ||||
| 	formatLogger.Panic(args...) | ||||
| func Warn(v ...interface{}) { | ||||
| 	formatLogger.doLog(LevelWarn, v...) | ||||
| } | ||||
| 
 | ||||
| func Panicf(template string, args ...interface{}) { | ||||
| 	formatLogger.Panicf(template, args...) | ||||
| func Error(v ...interface{}) { | ||||
| 	formatLogger.doLog(LevelError, v...) | ||||
| } | ||||
| 
 | ||||
| func Fatal(args ...interface{}) { | ||||
| 	formatLogger.Fatal(args...) | ||||
| func Errorf(format string, v ...interface{}) { | ||||
| 	formatLogger.doLogf(LevelError, format, v...) | ||||
| } | ||||
| 
 | ||||
| func Fatalf(template string, args ...interface{}) { | ||||
| 	formatLogger.Fatalf(template, args...) | ||||
| func Warnf(format string, v ...interface{}) { | ||||
| 	formatLogger.doLogf(LevelWarn, format, v...) | ||||
| } | ||||
| 
 | ||||
| func Println(args ...interface{}) { | ||||
| 	formatLogger.Debug(args...) | ||||
| func Debugf(format string, v ...interface{}) { | ||||
| 	formatLogger.doLogf(LevelDebug, format, v...) | ||||
| } | ||||
|  | ||||
					Loading…
					
					
				
		Reference in new issue