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
|
|
|
|
2024-06-07 17:55:45 +03:00
|
|
|
// Logger is a logging interface.
|
2023-11-24 15:23:07 +03:00
|
|
|
type Logger interface {
|
2024-06-07 17:55:45 +03:00
|
|
|
// With adds fields to the logger and returns a new logger with those fields.
|
2023-11-24 15:23:07 +03:00
|
|
|
With(fields ...zap.Field) Logger
|
2024-06-07 17:55:45 +03:00
|
|
|
// WithLazy adds fields to the logger lazily and returns a new logger with those fields.
|
2023-11-24 15:23:07 +03:00
|
|
|
WithLazy(fields ...zap.Field) Logger
|
2024-06-07 17:55:45 +03:00
|
|
|
// Level returns the logging level of the logger.
|
2023-11-24 15:23:07 +03:00
|
|
|
Level() zapcore.Level
|
2024-06-07 17:55:45 +03:00
|
|
|
// Check checks if the log message meets the given level.
|
2023-11-24 15:23:07 +03:00
|
|
|
Check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry
|
2024-06-07 17:55:45 +03:00
|
|
|
// Log logs a message with the given level and fields.
|
2023-11-24 15:23:07 +03:00
|
|
|
Log(lvl zapcore.Level, msg string, fields ...zap.Field)
|
2024-06-07 17:55:45 +03:00
|
|
|
// Debug logs a debug-level message with the given fields.
|
2023-11-24 15:23:07 +03:00
|
|
|
Debug(msg string, fields ...zap.Field)
|
2024-06-07 17:55:45 +03:00
|
|
|
// Info logs an info-level message with the given fields.
|
2023-11-24 15:23:07 +03:00
|
|
|
Info(msg string, fields ...zap.Field)
|
2024-06-07 17:55:45 +03:00
|
|
|
// Warn logs a warning-level message with the given fields.
|
2023-11-24 15:23:07 +03:00
|
|
|
Warn(msg string, fields ...zap.Field)
|
2024-06-07 17:55:45 +03:00
|
|
|
// Error logs an error-level message with the given fields.
|
2023-11-24 15:23:07 +03:00
|
|
|
Error(msg string, fields ...zap.Field)
|
2024-06-14 18:10:48 +03:00
|
|
|
// DPanic logs a debug-panic-level message with the given fields and panics
|
|
|
|
// if the logger's panic level is set to a non-zero value.
|
2023-11-24 15:23:07 +03:00
|
|
|
DPanic(msg string, fields ...zap.Field)
|
2024-06-07 17:55:45 +03:00
|
|
|
// Panic logs a panic-level message with the given fields and panics immediately.
|
2023-11-24 15:23:07 +03:00
|
|
|
Panic(msg string, fields ...zap.Field)
|
2024-06-07 17:55:45 +03:00
|
|
|
// Fatal logs a fatal-level message with the given fields, then calls os.Exit(1).
|
2023-11-24 15:23:07 +03:00
|
|
|
Fatal(msg string, fields ...zap.Field)
|
2024-06-07 17:55:45 +03:00
|
|
|
// ForHandler returns a new logger that is associated with the given handler.
|
2024-01-19 13:17:04 +03:00
|
|
|
ForHandler(handler any) Logger
|
2024-06-07 17:55:45 +03:00
|
|
|
// ForConnection returns a new logger that is associated with the given connection.
|
2024-01-19 13:17:04 +03:00
|
|
|
ForConnection(conn any) Logger
|
2024-06-07 17:55:45 +03:00
|
|
|
// ForAccount returns a new logger that is associated with the given account.
|
2024-01-19 13:17:04 +03:00
|
|
|
ForAccount(acc any) Logger
|
2024-06-07 17:55:45 +03:00
|
|
|
// Sync returns an error if there's a problem writing log messages to disk, or nil if all writes were successful.
|
2023-11-24 15:23:07 +03:00
|
|
|
Sync() error
|
2023-10-18 14:27:23 +03:00
|
|
|
}
|
|
|
|
|
2024-09-26 17:36:41 +03:00
|
|
|
type previousField uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
previousFieldHandler previousField = iota + 1
|
|
|
|
previousFieldConnection
|
|
|
|
previousFieldAccount
|
|
|
|
)
|
|
|
|
|
2024-06-07 17:55:45 +03:00
|
|
|
// Default is a default logger implementation.
|
2023-11-24 15:23:07 +03:00
|
|
|
type Default struct {
|
|
|
|
*zap.Logger
|
2024-09-26 17:36:41 +03:00
|
|
|
parent *zap.Logger
|
|
|
|
previous previousField
|
2023-10-18 14:27:23 +03:00
|
|
|
}
|
|
|
|
|
2024-06-07 17:55:45 +03:00
|
|
|
// NewDefault creates a new default logger with the given format and debug level.
|
2024-04-10 14:06:41 +03:00
|
|
|
func NewDefault(format string, debug bool) Logger {
|
2023-11-24 15:23:07 +03:00
|
|
|
return &Default{
|
2024-04-10 14:06:41 +03:00
|
|
|
Logger: NewZap(format, debug),
|
2023-11-24 15:23:07 +03:00
|
|
|
}
|
2023-10-18 14:27:23 +03:00
|
|
|
}
|
|
|
|
|
2024-06-07 17:55:45 +03:00
|
|
|
// With adds fields to the logger and returns a new logger with those fields.
|
2023-11-24 15:23:07 +03:00
|
|
|
func (l *Default) With(fields ...zap.Field) Logger {
|
2024-01-19 14:48:18 +03:00
|
|
|
return l.clone(l.Logger.With(fields...))
|
2023-10-18 14:27:23 +03:00
|
|
|
}
|
|
|
|
|
2024-06-07 17:55:45 +03:00
|
|
|
// WithLazy adds fields to the logger lazily and returns a new logger with those fields.
|
2023-11-24 15:23:07 +03:00
|
|
|
func (l *Default) WithLazy(fields ...zap.Field) Logger {
|
2024-01-19 14:48:18 +03:00
|
|
|
return l.clone(l.Logger.WithLazy(fields...))
|
2023-10-18 14:27:23 +03:00
|
|
|
}
|
|
|
|
|
2024-06-07 17:55:45 +03:00
|
|
|
// ForHandler returns a new logger that is associated with the given handler.
|
2024-09-26 17:36:41 +03:00
|
|
|
// This will replace "handler" field if it was set before.
|
|
|
|
// Note: chain calls like ForHandler().With().ForHandler() will DUPLICATE handler field!
|
2024-01-19 13:17:04 +03:00
|
|
|
func (l *Default) ForHandler(handler any) Logger {
|
2024-09-26 17:36:41 +03:00
|
|
|
if l.previous != previousFieldHandler {
|
|
|
|
result := l.With(zap.Any(HandlerAttr, handler))
|
|
|
|
result.(*Default).setPrevious(previousFieldHandler)
|
|
|
|
result.(*Default).parent = l.Logger
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
result := l.clone(l.parentOrCurrent().With(zap.Any(HandlerAttr, handler)))
|
|
|
|
result.(*Default).setPrevious(previousFieldHandler)
|
|
|
|
return result
|
2024-01-19 13:17:04 +03:00
|
|
|
}
|
|
|
|
|
2024-06-07 17:55:45 +03:00
|
|
|
// ForConnection returns a new logger that is associated with the given connection.
|
2024-09-26 17:36:41 +03:00
|
|
|
// This will replace "connection" field if it was set before.
|
|
|
|
// Note: chain calls like ForConnection().With().ForConnection() will DUPLICATE connection field!
|
2024-01-19 13:17:04 +03:00
|
|
|
func (l *Default) ForConnection(conn any) Logger {
|
2024-09-26 17:36:41 +03:00
|
|
|
if l.previous != previousFieldConnection {
|
|
|
|
result := l.With(zap.Any(ConnectionAttr, conn))
|
|
|
|
result.(*Default).setPrevious(previousFieldConnection)
|
|
|
|
result.(*Default).parent = l.Logger
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
result := l.clone(l.parentOrCurrent().With(zap.Any(ConnectionAttr, conn)))
|
|
|
|
result.(*Default).setPrevious(previousFieldConnection)
|
|
|
|
return result
|
2024-01-19 13:17:04 +03:00
|
|
|
}
|
|
|
|
|
2024-06-07 17:55:45 +03:00
|
|
|
// ForAccount returns a new logger that is associated with the given account.
|
2024-09-26 17:36:41 +03:00
|
|
|
// This will replace "account" field if it was set before.
|
|
|
|
// Note: chain calls like ForAccount().With().ForAccount() will DUPLICATE account field!
|
2024-01-19 13:17:04 +03:00
|
|
|
func (l *Default) ForAccount(acc any) Logger {
|
2024-09-26 17:36:41 +03:00
|
|
|
if l.previous != previousFieldAccount {
|
|
|
|
result := l.With(zap.Any(AccountAttr, acc))
|
|
|
|
result.(*Default).setPrevious(previousFieldAccount)
|
|
|
|
result.(*Default).parent = l.Logger
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
result := l.clone(l.parentOrCurrent().With(zap.Any(AccountAttr, acc)))
|
|
|
|
result.(*Default).setPrevious(previousFieldAccount)
|
|
|
|
return result
|
2023-10-18 14:27:23 +03:00
|
|
|
}
|
|
|
|
|
2024-06-07 17:55:45 +03:00
|
|
|
// clone creates a copy of the given logger.
|
2024-01-19 14:48:18 +03:00
|
|
|
func (l *Default) clone(log *zap.Logger) Logger {
|
2024-09-26 16:48:52 +03:00
|
|
|
parent := l.parent
|
|
|
|
if parent == nil {
|
|
|
|
parent = l.Logger
|
|
|
|
}
|
|
|
|
return &Default{Logger: log, parent: parent}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parentOrCurrent returns parent logger if it exists or current logger otherwise.
|
|
|
|
func (l *Default) parentOrCurrent() *zap.Logger {
|
|
|
|
if l.parent != nil {
|
|
|
|
return l.parent
|
|
|
|
}
|
|
|
|
return l.Logger
|
2024-01-19 14:48:18 +03:00
|
|
|
}
|
|
|
|
|
2024-09-26 17:36:41 +03:00
|
|
|
func (l *Default) setPrevious(prev previousField) {
|
|
|
|
l.previous = prev
|
|
|
|
}
|
|
|
|
|
2024-06-07 17:55:45 +03:00
|
|
|
// AnyZapFields converts an array of values to zap fields.
|
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
|
|
|
}
|