mg-transport-core/core/logger/default.go

60 lines
1.4 KiB
Go
Raw Normal View History

2023-10-18 14:27:23 +03:00
package logger
import (
2023-11-24 15:23:07 +03:00
"strconv"
2023-10-18 14:27:23 +03:00
2023-11-24 15:23:07 +03:00
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
2023-10-18 14:27:23 +03:00
2023-11-24 15:23:07 +03:00
type Logger interface {
With(fields ...zap.Field) Logger
WithLazy(fields ...zap.Field) Logger
Level() zapcore.Level
Check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry
Log(lvl zapcore.Level, msg string, fields ...zap.Field)
Debug(msg string, fields ...zap.Field)
Info(msg string, fields ...zap.Field)
Warn(msg string, fields ...zap.Field)
Error(msg string, fields ...zap.Field)
DPanic(msg string, fields ...zap.Field)
Panic(msg string, fields ...zap.Field)
Fatal(msg string, fields ...zap.Field)
ForAccount(handler, conn, acc any) Logger
Sync() error
2023-10-18 14:27:23 +03:00
}
2023-11-24 15:23:07 +03:00
type Default struct {
*zap.Logger
2023-10-18 14:27:23 +03:00
}
2023-11-24 15:23:07 +03:00
func NewDefault(debug bool) Logger {
return &Default{
Logger: NewZap(debug),
}
2023-10-18 14:27:23 +03:00
}
2023-11-24 15:23:07 +03:00
func (l *Default) With(fields ...zap.Field) Logger {
return l.With(fields...).(Logger)
2023-10-18 14:27:23 +03:00
}
2023-11-24 15:23:07 +03:00
func (l *Default) WithLazy(fields ...zap.Field) Logger {
return l.WithLazy(fields...).(Logger)
2023-10-18 14:27:23 +03:00
}
2023-11-24 15:23:07 +03:00
func (l *Default) ForAccount(handler, conn, acc any) Logger {
return l.WithLazy(zap.Any(HandlerAttr, handler), zap.Any(ConnectionAttr, conn), zap.Any(AccountAttr, acc))
2023-10-18 14:27:23 +03:00
}
2023-11-24 15:23:07 +03:00
func AnyZapFields(args []interface{}) []zap.Field {
fields := make([]zap.Field, len(args))
for i := 0; i < len(fields); i++ {
if val, ok := args[i].(zap.Field); ok {
fields[i] = val
continue
}
fields[i] = zap.Any("arg"+strconv.Itoa(i), args[i])
}
return fields
2023-10-18 14:27:23 +03:00
}