123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package zlog
- import (
- "context"
- "fmt"
- "os"
- "sync"
- "github.com/rotisserie/eris"
- "go.uber.org/zap"
- "go.uber.org/zap/zapcore"
- )
- var (
- l *Logger
- once = sync.Once{}
- )
- type Logger struct {
- log *zap.Logger
- ctx context.Context
- }
- func NewLogger(writer Writer) *Logger {
- // 限制日志输出级别, >= DebugLevel 会打印所有级别的日志
- // 生产环境中一般使用 >= ErrorLevel
- lowPriority := zap.LevelEnablerFunc(func(lv zapcore.Level) bool {
- return lv >= zapcore.DebugLevel
- })
- // 控制台展示方便调试,使用 TEXT 的方式
- consoleEncoderConfig := zap.NewDevelopmentEncoderConfig()
- consoleEncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
- consoleEncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
- consoleEncoder := zapcore.NewConsoleEncoder(consoleEncoderConfig)
- stdCore := zapcore.NewCore(consoleEncoder, zapcore.Lock(os.Stdout), lowPriority)
- // 日志格式化
- productionEncoderConfig := zap.NewProductionEncoderConfig()
- productionEncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
- jsonEnc := zapcore.NewJSONEncoder(productionEncoderConfig)
- // 使用 JSON 格式日志
- var core zapcore.Core
- if writer.GetEnv() != notPushEnvironment {
- syncer := zapcore.AddSync(writer)
- esCore := zapcore.NewCore(jsonEnc, syncer, lowPriority).With([]zap.Field{zap.String("env", writer.GetEnv())})
- core = zapcore.NewTee(stdCore, esCore)
- } else {
- core = stdCore
- }
- // logger 输出到 console 且标识调用代码行
- l := zap.New(core).WithOptions(zap.AddCallerSkip(1), zap.AddCaller())
- return &Logger{log: l}
- }
- func NewLoggerWithZapCode(l *zap.Logger) *Logger {
- return &Logger{log: l}
- }
- func WithZapLogger(log *Logger) {
- once.Do(func() {
- l = log
- })
- }
- // Debug logs a message at info level.
- func Debug(args ...any) {
- l.log.Debug(fmt.Sprint(args...))
- }
- // Debugf logs a message at info level.
- func Debugf(msg string, args ...any) {
- l.log.Debug(fmt.Sprintf(msg, args...))
- }
- // Debugv logs a message at info level.
- func Debugv(arg any) {
- l.log.Debug(fmt.Sprint(arg))
- }
- // Debugw logs a message at info level.
- func Debugw(msg string, fields ...zap.Field) {
- l.log.Debug(fmt.Sprintf(msg, toValues(fields)...))
- }
- // Error logs a message at error level.
- func Error(args ...any) {
- l.log.Error(fmt.Sprint(args...))
- }
- func ErrorStack(err error) {
- format := eris.NewDefaultStringFormat(eris.FormatOptions{
- InvertOutput: true,
- WithTrace: true,
- InvertTrace: true,
- })
- formattedStr := eris.ToCustomString(err, format)
- l.log.Error(formattedStr)
- }
- // Errorf logs a message at error level.
- func Errorf(msg string, args ...any) {
- l.log.Error(fmt.Sprintf(msg, args...))
- }
- // Errorv logs a message at error level.
- func Errorv(arg any) {
- l.log.Error(fmt.Sprint(arg))
- }
- // Errorw logs a message at error level.
- func Errorw(msg string, fields ...zap.Field) {
- l.log.Error(fmt.Sprintf(msg, toValues(fields)...))
- }
- // Info logs a message at info level.
- func Info(args ...any) {
- l.log.Info(fmt.Sprint(args...))
- }
- // Infof logs a message at info level.
- func Infof(msg string, args ...any) {
- l.log.Info(fmt.Sprintf(msg, args...))
- }
- // Infov logs a message at info level.
- func Infov(arg any) {
- l.log.Info(fmt.Sprint(arg))
- }
- // Infow logs a message at info level.
- func Infow(msg string, fields ...zap.Field) {
- l.log.Info(fmt.Sprintf(msg, toValues(fields)...))
- }
- func addLineBreakSymbol(msg string) string {
- return fmt.Sprintln(msg)
- }
- func toValues(fields []zap.Field) []any {
- var str []any = make([]any, len(fields))
- for i, field := range fields {
- str[i] = field.String
- }
- return str
- }
|