2017-02-20 09:39:36 -06:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2023-11-29 01:33:50 -06:00
|
|
|
"context"
|
|
|
|
"math"
|
2017-02-20 10:05:15 -06:00
|
|
|
"os"
|
2023-11-29 01:33:50 -06:00
|
|
|
"runtime"
|
2017-02-20 09:39:36 -06:00
|
|
|
"time"
|
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
"golang.org/x/exp/slog"
|
2017-02-20 09:39:36 -06:00
|
|
|
)
|
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
const errorKey = "LOG_ERROR"
|
2017-02-20 09:39:36 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
const (
|
|
|
|
legacyLevelCrit = iota
|
|
|
|
legacyLevelError
|
|
|
|
legacyLevelWarn
|
|
|
|
legacyLevelInfo
|
|
|
|
legacyLevelDebug
|
|
|
|
legacyLevelTrace
|
|
|
|
)
|
2017-02-20 09:39:36 -06:00
|
|
|
|
|
|
|
const (
|
2023-11-29 01:33:50 -06:00
|
|
|
levelMaxVerbosity slog.Level = math.MinInt
|
|
|
|
LevelTrace slog.Level = -8
|
|
|
|
LevelDebug = slog.LevelDebug
|
|
|
|
LevelInfo = slog.LevelInfo
|
|
|
|
LevelWarn = slog.LevelWarn
|
|
|
|
LevelError = slog.LevelError
|
|
|
|
LevelCrit slog.Level = 12
|
|
|
|
|
|
|
|
// for backward-compatibility
|
|
|
|
LvlTrace = LevelTrace
|
|
|
|
LvlInfo = LevelInfo
|
|
|
|
LvlDebug = LevelDebug
|
2017-02-20 09:39:36 -06:00
|
|
|
)
|
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
// convert from old Geth verbosity level constants
|
|
|
|
// to levels defined by slog
|
|
|
|
func FromLegacyLevel(lvl int) slog.Level {
|
|
|
|
switch lvl {
|
|
|
|
case legacyLevelCrit:
|
|
|
|
return LevelCrit
|
|
|
|
case legacyLevelError:
|
|
|
|
return slog.LevelError
|
|
|
|
case legacyLevelWarn:
|
|
|
|
return slog.LevelWarn
|
|
|
|
case legacyLevelInfo:
|
|
|
|
return slog.LevelInfo
|
|
|
|
case legacyLevelDebug:
|
|
|
|
return slog.LevelDebug
|
|
|
|
case legacyLevelTrace:
|
|
|
|
return LevelTrace
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: should we allow use of custom levels or force them to match existing max/min if they fall outside the range as I am doing here?
|
|
|
|
if lvl > legacyLevelTrace {
|
|
|
|
return LevelTrace
|
|
|
|
}
|
|
|
|
return LevelCrit
|
|
|
|
}
|
|
|
|
|
|
|
|
// LevelAlignedString returns a 5-character string containing the name of a Lvl.
|
|
|
|
func LevelAlignedString(l slog.Level) string {
|
2017-02-23 11:30:32 -06:00
|
|
|
switch l {
|
2023-11-29 01:33:50 -06:00
|
|
|
case LevelTrace:
|
2017-02-23 11:30:32 -06:00
|
|
|
return "TRACE"
|
2023-11-29 01:33:50 -06:00
|
|
|
case slog.LevelDebug:
|
2017-02-23 11:30:32 -06:00
|
|
|
return "DEBUG"
|
2023-11-29 01:33:50 -06:00
|
|
|
case slog.LevelInfo:
|
2017-02-23 11:30:32 -06:00
|
|
|
return "INFO "
|
2023-11-29 01:33:50 -06:00
|
|
|
case slog.LevelWarn:
|
2017-02-23 11:30:32 -06:00
|
|
|
return "WARN "
|
2023-11-29 01:33:50 -06:00
|
|
|
case slog.LevelError:
|
2017-02-23 11:30:32 -06:00
|
|
|
return "ERROR"
|
2023-11-29 01:33:50 -06:00
|
|
|
case LevelCrit:
|
2017-02-23 11:30:32 -06:00
|
|
|
return "CRIT "
|
|
|
|
default:
|
2023-11-29 01:33:50 -06:00
|
|
|
return "unknown level"
|
2017-02-23 11:30:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
// LevelString returns a 5-character string containing the name of a Lvl.
|
|
|
|
func LevelString(l slog.Level) string {
|
2017-02-20 09:39:36 -06:00
|
|
|
switch l {
|
2023-11-29 01:33:50 -06:00
|
|
|
case LevelTrace:
|
|
|
|
return "trace"
|
|
|
|
case slog.LevelDebug:
|
|
|
|
return "debug"
|
|
|
|
case slog.LevelInfo:
|
2017-02-20 09:39:36 -06:00
|
|
|
return "info"
|
2023-11-29 01:33:50 -06:00
|
|
|
case slog.LevelWarn:
|
2017-02-20 09:39:36 -06:00
|
|
|
return "warn"
|
2023-11-29 01:33:50 -06:00
|
|
|
case slog.LevelError:
|
2017-02-20 09:39:36 -06:00
|
|
|
return "eror"
|
2023-11-29 01:33:50 -06:00
|
|
|
case LevelCrit:
|
2017-02-20 09:39:36 -06:00
|
|
|
return "crit"
|
|
|
|
default:
|
2023-11-29 01:33:50 -06:00
|
|
|
return "unknown"
|
2017-02-20 09:39:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Logger writes key/value pairs to a Handler
|
|
|
|
type Logger interface {
|
2023-11-29 01:33:50 -06:00
|
|
|
// With returns a new Logger that has this logger's attributes plus the given attributes
|
|
|
|
With(ctx ...interface{}) Logger
|
2017-02-20 09:39:36 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
// With returns a new Logger that has this logger's attributes plus the given attributes. Identical to 'With'.
|
|
|
|
New(ctx ...interface{}) Logger
|
2017-02-20 09:39:36 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
// Log logs a message at the specified level with context key/value pairs
|
|
|
|
Log(level slog.Level, msg string, ctx ...interface{})
|
2017-02-20 09:39:36 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
// Trace log a message at the trace level with context key/value pairs
|
2017-02-20 10:05:15 -06:00
|
|
|
Trace(msg string, ctx ...interface{})
|
2023-02-22 06:39:41 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
// Debug logs a message at the debug level with context key/value pairs
|
2017-02-20 09:39:36 -06:00
|
|
|
Debug(msg string, ctx ...interface{})
|
2023-02-22 06:39:41 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
// Info logs a message at the info level with context key/value pairs
|
2017-02-20 09:39:36 -06:00
|
|
|
Info(msg string, ctx ...interface{})
|
2023-02-22 06:39:41 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
// Warn logs a message at the warn level with context key/value pairs
|
2017-02-20 09:39:36 -06:00
|
|
|
Warn(msg string, ctx ...interface{})
|
2023-02-22 06:39:41 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
// Error logs a message at the error level with context key/value pairs
|
2017-02-20 09:39:36 -06:00
|
|
|
Error(msg string, ctx ...interface{})
|
2023-02-22 06:39:41 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
// Crit logs a message at the crit level with context key/value pairs, and exits
|
2017-02-20 09:39:36 -06:00
|
|
|
Crit(msg string, ctx ...interface{})
|
2023-11-29 01:33:50 -06:00
|
|
|
|
|
|
|
// Write logs a message at the specified level
|
|
|
|
Write(level slog.Level, msg string, attrs ...any)
|
2017-02-20 09:39:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type logger struct {
|
2023-11-29 01:33:50 -06:00
|
|
|
inner *slog.Logger
|
2017-02-20 09:39:36 -06:00
|
|
|
}
|
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
// NewLogger returns a logger with the specified handler set
|
|
|
|
func NewLogger(h slog.Handler) Logger {
|
|
|
|
return &logger{
|
|
|
|
slog.New(h),
|
2023-09-07 07:48:49 -05:00
|
|
|
}
|
2017-02-20 09:39:36 -06:00
|
|
|
}
|
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
// write logs a message at the specified level:
|
|
|
|
func (l *logger) Write(level slog.Level, msg string, attrs ...any) {
|
|
|
|
if !l.inner.Enabled(context.Background(), level) {
|
|
|
|
return
|
|
|
|
}
|
2017-02-20 09:39:36 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
var pcs [1]uintptr
|
|
|
|
runtime.Callers(3, pcs[:])
|
2017-02-20 09:39:36 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
if len(attrs)%2 != 0 {
|
|
|
|
attrs = append(attrs, nil, errorKey, "Normalized odd number of arguments by adding nil")
|
|
|
|
}
|
2017-02-20 10:05:15 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
// evaluate lazy values
|
|
|
|
var hadErr bool
|
|
|
|
for i := 1; i < len(attrs); i += 2 {
|
|
|
|
lz, ok := attrs[i].(Lazy)
|
|
|
|
if ok {
|
|
|
|
v, err := evaluateLazy(lz)
|
|
|
|
if err != nil {
|
|
|
|
hadErr = true
|
|
|
|
attrs[i] = err
|
|
|
|
} else {
|
|
|
|
attrs[i] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-20 09:39:36 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
if hadErr {
|
|
|
|
attrs = append(attrs, errorKey, "bad lazy")
|
|
|
|
}
|
2017-02-20 09:39:36 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
r := slog.NewRecord(time.Now(), level, msg, pcs[0])
|
|
|
|
r.Add(attrs...)
|
|
|
|
l.inner.Handler().Handle(context.Background(), r)
|
2017-02-20 09:39:36 -06:00
|
|
|
}
|
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
func (l *logger) Log(level slog.Level, msg string, attrs ...any) {
|
|
|
|
l.Write(level, msg, attrs...)
|
2017-02-20 09:39:36 -06:00
|
|
|
}
|
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
func (l *logger) With(ctx ...interface{}) Logger {
|
|
|
|
return &logger{l.inner.With(ctx...)}
|
2017-02-20 09:39:36 -06:00
|
|
|
}
|
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
func (l *logger) New(ctx ...interface{}) Logger {
|
|
|
|
return l.With(ctx...)
|
2017-02-20 09:39:36 -06:00
|
|
|
}
|
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
func (l *logger) Trace(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(LevelTrace, msg, ctx...)
|
2017-02-20 09:39:36 -06:00
|
|
|
}
|
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
func (l *logger) Debug(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(slog.LevelDebug, msg, ctx...)
|
2017-02-20 09:39:36 -06:00
|
|
|
}
|
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
func (l *logger) Info(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(slog.LevelInfo, msg, ctx...)
|
2017-02-20 09:39:36 -06:00
|
|
|
}
|
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
func (l *logger) Warn(msg string, ctx ...any) {
|
|
|
|
l.Write(slog.LevelWarn, msg, ctx...)
|
|
|
|
}
|
2017-02-20 09:39:36 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
func (l *logger) Error(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(slog.LevelError, msg, ctx...)
|
|
|
|
}
|
2017-02-20 09:39:36 -06:00
|
|
|
|
2023-11-29 01:33:50 -06:00
|
|
|
func (l *logger) Crit(msg string, ctx ...interface{}) {
|
|
|
|
l.Write(LevelCrit, msg, ctx...)
|
|
|
|
os.Exit(1)
|
2017-02-20 09:39:36 -06:00
|
|
|
}
|