Documentation
¶
Overview ¶
Package log provides a structured logger with context support.
Index ¶
- Constants
- Variables
- func Debug(msg string, args ...slog.Attr)
- func Debugf(format string, args ...any)
- func Default() *slog.Logger
- func Error(msg string, args ...slog.Attr)
- func Errorf(format string, args ...any)
- func Fatal(msg string, args ...slog.Attr)
- func Fatalf(format string, args ...any)
- func Info(msg string, args ...slog.Attr)
- func Infof(format string, args ...any)
- func Log(level Level, msg string, args ...slog.Attr)
- func Logf(level Level, format string, args ...any)
- func New(opts ...Option) *slog.Logger
- func Print(msg string, args ...slog.Attr)
- func SetCallerFormatter(f CallerFormatter, loggers ...*slog.Logger)
- func SetCallerOffset(offset int, loggers ...*slog.Logger)
- func SetFormatter(f Formatter, loggers ...*slog.Logger)
- func SetLevel(level Level, loggers ...*slog.Logger)
- func SetOutput(w io.Writer, loggers ...*slog.Logger)
- func SetPrefix(prefix string, loggers ...*slog.Logger)
- func SetReportCaller(report bool, loggers ...*slog.Logger)
- func SetReportTimestamp(report bool, loggers ...*slog.Logger)
- func SetStyles(s *Styles, loggers ...*slog.Logger)
- func SetTimeFormat(format string, loggers ...*slog.Logger)
- func SetTimeFunction(f TimeFunction, loggers ...*slog.Logger)
- func StandardLog(opts ...StandardLogOption) *stdlog.Logger
- func Warn(msg string, args ...slog.Attr)
- func Warnf(format string, args ...any)
- func WithContext(ctx context.Context, logger Logger) context.Context
- func WithPrefix(l *slog.Logger, prefix string) *slog.Logger
- type CallerFormatter
- type Formatter
- type Level
- type LogOptions
- type Logger
- type Option
- func AsDefault() Option
- func UseCallerFormatter(f CallerFormatter) Option
- func UseCallerOffset(offset int) Option
- func UseFields(fields map[string]slog.Value) Option
- func UseFormatter(f Formatter) Option
- func UseLevel(l Level) Option
- func UseOutput(w io.Writer) Option
- func UsePrefix(p string) Option
- func UseReportCaller(r bool) Option
- func UseReportTimestamp(r bool) Option
- func UseStyles(s *Styles) Option
- func UseTimeFormat(f string) Option
- func UseTimeFunction(f TimeFunction) Option
- type Options
- type StandardLogOption
- type StandardLogOptions
- type Styles
- type TimeFunction
Constants ¶
const ( DebugLevel = log.DebugLevel InfoLevel = log.InfoLevel WarnLevel = log.WarnLevel ErrorLevel = log.ErrorLevel FatalLevel = log.FatalLevel )
Levels
const ( TextFormatter = log.TextFormatter JSONFormatter = log.JSONFormatter LogfmtFormatter = log.LogfmtFormatter )
Formatters
Variables ¶
var ( ShortCallerFormatter = log.ShortCallerFormatter LongCallerFormatter = log.LongCallerFormatter )
Caller Formatters
var ContextKey = contextKey{"log"}
ContextKey is the key used to store the logger in context.
Functions ¶
func Fatalf ¶ added in v0.2.0
Fatalf logs a formatted message with level Fatal and exits with status code 1.
func SetCallerFormatter ¶ added in v0.2.0
func SetCallerFormatter(f CallerFormatter, loggers ...*slog.Logger)
SetCallerFormatter sets the caller formatter.
func SetCallerOffset ¶ added in v0.2.0
SetCallerOffset sets the caller offset.
func SetFormatter ¶ added in v0.2.0
SetFormatter sets the formatter.
func SetReportCaller ¶ added in v0.2.0
SetReportCaller sets whether to report caller location.
func SetReportTimestamp ¶ added in v0.2.0
SetReportTimestamp sets whether to report timestamp.
func SetTimeFormat ¶ added in v0.2.0
SetTimeFormat sets the time format.
func SetTimeFunction ¶ added in v0.2.0
func SetTimeFunction(f TimeFunction, loggers ...*slog.Logger)
SetTimeFunction sets the time function.
func StandardLog ¶
func StandardLog(opts ...StandardLogOption) *stdlog.Logger
StandardLog creates a new standard logger with the given options.
func WithContext ¶
WithContext wraps the given logger in context.
Types ¶
type Logger ¶
type Logger interface {
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
}
Logger defines the slog.Logger interface.
func FromContext ¶
FromContext returns the logger from the given context. This will return the default package logger if no logger found in context.
type Option ¶
type Option func(*Options)
Option is a logger option.
func AsDefault ¶ added in v0.2.0
func AsDefault() Option
AsDefault sets the logger as the default logger. Default is false.
func UseCallerFormatter ¶ added in v0.2.0
func UseCallerFormatter(f CallerFormatter) Option
UseCallerFormatter sets the caller formatter option. Default is log.ShortCallerFormatter.
func UseCallerOffset ¶ added in v0.2.0
UseCallerOffset sets the caller offset option. Default is 0.
func UseFormatter ¶ added in v0.2.0
UseFormatter sets the formatter option. Default is TextFormatter.
func UseLevel ¶ added in v0.2.0
UseLevel sets the level option. Default is log.InfoLevel.
func UseReportCaller ¶ added in v0.2.0
UseReportCaller sets the report caller option. Default is false.
func UseReportTimestamp ¶ added in v0.2.0
UseReportTimestamp sets the report timestamp option. Default is false.
func UseStyles ¶ added in v0.2.0
UseStyles sets the styles option. Default is DefaultStyles.
func UseTimeFormat ¶ added in v0.2.0
UseTimeFormat sets the time format option. Default is log.DefaultTimeFormat.
func UseTimeFunction ¶ added in v0.2.0
func UseTimeFunction(f TimeFunction) Option
UseTimeFunction sets the time function option. Default is time.Now.
type Options ¶
type Options struct {
*LogOptions
Writer io.Writer // Writer is the writer for the logger. Default is [os.Stderr].
Styles *log.Styles // Styles is the styles for the logger. Default is [DefaultStyles].
Default bool // Default is whether the logger is the default logger. Default is false.
}
Options is the logger options.
type StandardLogOption ¶
type StandardLogOption func(*StandardLogOptions)
StandardLogOption is a standard logger option.
type StandardLogOptions ¶
type StandardLogOptions struct {
log.StandardLogOptions
Logger *slog.Logger // Log is the logger to use. Default is the default logger.
}
StandardLogOptions can be used to configure the standard log adapter.
type Styles ¶
Type aliases
func DefaultStyles ¶
func DefaultStyles() *Styles
DefaultStyles returns the default styles. It applies custom styles to the log.DefaultStyles.
