Documentation
¶
Index ¶
- Constants
- Variables
- func NewConsole(w io.Writer, addSource bool, level slog.Leveler, color bool) (slog.Handler, *slog.LevelVar)
- func NewConsoleHandler(w io.Writer, opts *tint.Options) slog.Handler
- func NewJSON(w io.Writer, addSource bool, level slog.Leveler, _ bool) (slog.Handler, *slog.LevelVar)
- func NewJSONHandler(w io.Writer, opts *slogjson.HandlerOptions) slog.Handler
- func NewTint(w io.Writer, addSource bool, level slog.Leveler, color bool) (slog.Handler, *slog.LevelVar)
- type ConsoleHandler
- type FlexLogger
- type JsonHandler
- type LevelColor
- type LevelColors
- type LevelColorsMapping
- type Logger
- func (l *Logger) AddSource() bool
- func (l *Logger) Color() bool
- func (l *Logger) Copy() FlexLogger
- func (l *Logger) Debug(msg string, args ...any)
- func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Enabled(ctx context.Context, level slog.Level) bool
- func (l *Logger) Error(msg string, args ...any)
- func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Fatal(msg string, args ...interface{})
- func (l *Logger) FatalContext(ctx context.Context, msg string, args ...interface{})
- func (l *Logger) GetLevel() slog.Level
- func (l *Logger) GetLogger() *slog.Logger
- func (l *Logger) Handler() slog.Handler
- func (l *Logger) Info(msg string, args ...any)
- func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Level() *slog.LevelVar
- func (l *Logger) Log(ctx context.Context, level slog.Level, msg string, args ...any)
- func (l *Logger) LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
- func (l *Logger) LogWithSource(ctx context.Context, level slog.Level, frames int, msg string, ...)
- func (l *Logger) SetLevel(level slog.Leveler)
- func (l *Logger) SetLevelString(level string) error
- func (l *Logger) SetLogger(logger *slog.Logger)
- func (l *Logger) SetReportCaller(reportCaller bool)
- func (l *Logger) Trace(msg string, args ...interface{})
- func (l *Logger) TraceContext(ctx context.Context, msg string, args ...interface{})
- func (l *Logger) Warn(msg string, args ...any)
- func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) With(args ...any) *slog.Logger
- func (l *Logger) WithGroup(name string) *slog.Logger
- func (l *Logger) Writer() io.Writer
- type NewLoggerFunc
Constants ¶
const ( LevelTrace = slog.Level(-8) LevelFatal = slog.Level(12) StackFrames = 5 // number of stack frames to skip in Handler.Handle )
Define our custom levels
const (
FrameMarker = "__skip_frames"
)
Variables ¶
var LevelColorsMap map[slog.Level]LevelColor = map[slog.Level]LevelColor{ LevelTrace: {Name: "TRACE", Color: color.FgGreen}, LevelFatal: {Name: "FATAL", Color: color.FgRed}, slog.LevelInfo: {Name: "INFO ", Color: color.FgBlue}, slog.LevelWarn: {Name: "WARN ", Color: color.FgYellow}, slog.LevelError: {Name: "ERROR", Color: color.FgRed}, slog.LevelDebug: {Name: "DEBUG", Color: color.FgMagenta}, }
LevelColorsMap is a map of log levels to their colors and the name of the level.
var LevelNames = map[slog.Leveler]string{ LevelTrace: "TRACE", LevelFatal: "FATAL", }
LevelNames defines additional log levels
var LevelStrings = map[string]slog.Level{ "TRACE": LevelTrace, "FATAL": LevelFatal, "INFO": slog.LevelInfo, "WARN": slog.LevelWarn, "ERROR": slog.LevelError, "DEBUG": slog.LevelDebug, }
LevelStrings is a map of level names to their slog.Level values.
Functions ¶
func NewConsole ¶
func NewConsole(w io.Writer, addSource bool, level slog.Leveler, color bool) (slog.Handler, *slog.LevelVar)
NewConsole creates a new slog.Handler for the ConsoleHandler, which wraps tint.NewHandler with some customizations.
func NewConsoleHandler ¶
ConsoleHandler is a slog.Handler that wraps tint.Handler
func NewJSONHandler ¶
Types ¶
type ConsoleHandler ¶
implement the slog.Handler interface via the tint.Handler
type FlexLogger ¶
type FlexLogger interface { // slog.Logger methods Debug(msg string, args ...any) DebugContext(ctx context.Context, msg string, args ...any) Enabled(ctx context.Context, level slog.Level) bool Error(msg string, args ...any) ErrorContext(ctx context.Context, msg string, args ...any) Handler() slog.Handler Info(msg string, args ...any) InfoContext(ctx context.Context, msg string, args ...any) Log(ctx context.Context, level slog.Level, msg string, args ...any) LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr) Warn(msg string, args ...any) WarnContext(ctx context.Context, msg string, args ...any) With(args ...any) *slog.Logger WithGroup(name string) *slog.Logger // custom methods Copy() FlexLogger GetLevel() slog.Level GetLogger() *slog.Logger SetLevel(level slog.Leveler) SetLevelString(level string) error SetLogger(logger *slog.Logger) SetReportCaller(reportCaller bool) Trace(msg string, args ...any) TraceContext(ctx context.Context, msg string, args ...any) Fatal(msg string, args ...any) FatalContext(ctx context.Context, msg string, args ...any) Writer() io.Writer AddSource() bool Level() *slog.LevelVar Color() bool }
type JsonHandler ¶
type LevelColor ¶
type LevelColor struct { // Name is the name of the log level Name string // Color is the color of the log level Color color.Attribute // contains filtered or unexported fields }
LevelColors defines the name as displayed to the user and color of a log level.
func (*LevelColor) Copy ¶
func (lc *LevelColor) Copy() *LevelColor
Copy returns a copy of the LevelColor.
func (*LevelColor) String ¶
func (lc *LevelColor) String(colored bool) string
String returns the level name, optionally with color applied.
type LevelColors ¶
type LevelColors struct {
// contains filtered or unexported fields
}
LevelColors is our internal representation of the user-defined LevelColorsMapping. We map the log levels via their slog.Level to their LevelColor using an offset to ensure we can map negative level values to our slice.
func (*LevelColors) Copy ¶
func (lc *LevelColors) Copy() *LevelColors
Copy returns a copy of the LevelColors.
func (*LevelColors) LevelColor ¶
func (lc *LevelColors) LevelColor(level slog.Level) *LevelColor
LevelColor returns the LevelColor for the given log level. Returns nil indicating if the log level was not found.
type LevelColorsMapping ¶
type LevelColorsMapping map[slog.Level]LevelColor
LevelColorsMapping is a map of log levels to their colors and is what the user defines in their configuration.
func (*LevelColorsMapping) LevelColors ¶
func (lm *LevelColorsMapping) LevelColors() *LevelColors
LevelColors returns the LevelColors for the LevelColorsMapping.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Our logger which wraps slog.Logger and implements CustomLogger
func NewLogger ¶
func NewLogger(f NewLoggerFunc, w io.Writer, addSource bool, level slog.Leveler, color bool) *Logger
NewLoggerFunc creates a new Logger
func (*Logger) AddSource ¶
AddSource returns whether the logger is configured to include the source file and line number in the log output
func (*Logger) Color ¶
Color returns whether the logger is configured to output colorized log messages
func (*Logger) Copy ¶
func (l *Logger) Copy() FlexLogger
Copy returns a copy of the Logger current Logger
func (*Logger) DebugContext ¶
DebugContext logs a message at the debug level with context
func (*Logger) ErrorContext ¶
ErrorContext logs a message at the error level with context
func (*Logger) FatalContext ¶
Log a message at the Fatal level with context and exit
func (*Logger) InfoContext ¶
InfoContext logs a message at the info level with context
func (*Logger) LogAttrs ¶
LogAttrs logs a message at the specified level with the included context and attributes
func (*Logger) LogWithSource ¶
func (l *Logger) LogWithSource(ctx context.Context, level slog.Level, frames int, msg string, args ...interface{})
LogWithSource sets the __source attribute so that our Handler knows to modify the r.PC value to include the original caller.
func (*Logger) SetLevelString ¶
SetLevelString sets the log level for the logger by string
func (*Logger) SetReportCaller ¶
SetReportCaller sets whether to include the source file and line number in the log output Doing so will replace the current logger with a new one that has the new setting
func (*Logger) TraceContext ¶
Log a message at the Trace level with context
func (*Logger) WarnContext ¶
WarnContext logs a message at the warn level with context