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-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
|
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-01-19 13:17:04 +03:00
|
|
|
func (l *Default) ForHandler(handler any) Logger {
|
|
|
|
return l.WithLazy(zap.Any(HandlerAttr, handler))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (l *Default) ForConnection(conn any) Logger {
|
|
|
|
return l.WithLazy(zap.Any(ConnectionAttr, conn))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (l *Default) ForAccount(acc any) Logger {
|
|
|
|
return l.WithLazy(zap.Any(AccountAttr, acc))
|
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 {
|
|
|
|
return &Default{Logger: log}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|