Documentation
¶
Overview ¶
Package log provides a structured logger with context support.
Index ¶
- Variables
- func Default() *slog.Logger
- func New(opts ...Option) *slog.Logger
- func SetOutput(l *slog.Logger, w io.Writer)
- func StandardLog(opts ...StandardLogOption) *stdlog.Logger
- func WithContext(ctx context.Context, logger Logger) context.Context
- type CallerFormatter
- type Formatter
- type Level
- type LogOptions
- type Logger
- type Option
- func WithCallerFormatter(f CallerFormatter) Option
- func WithCallerOffset(offset int) Option
- func WithDefault() Option
- func WithFields(fields map[string]slog.Value) Option
- func WithFormatter(f Formatter) Option
- func WithLevel(l Level) Option
- func WithPrefix(p string) Option
- func WithReportCaller(r bool) Option
- func WithReportTimestamp(r bool) Option
- func WithStyles(s *Styles) Option
- func WithTimeFormat(f string) Option
- func WithTimeFunction(f func(time.Time) time.Time) Option
- func WithWriter(w io.Writer) Option
- type Options
- type StandardLogOption
- type StandardLogOptions
- type Styles
Constants ¶
This section is empty.
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 StandardLog ¶
func StandardLog(opts ...StandardLogOption) *stdlog.Logger
StandardLog creates a new standard logger with the given options.
Types ¶
type Formatter ¶
Type Aliases
const ( TextFormatter Formatter = log.TextFormatter JSONFormatter Formatter = log.JSONFormatter LogfmtFormatter Formatter = log.LogfmtFormatter )
Formatters
type Level ¶
Type Aliases
const ( DebugLevel Level = log.DebugLevel InfoLevel Level = log.InfoLevel WarnLevel Level = log.WarnLevel ErrorLevel Level = log.ErrorLevel FatalLevel Level = log.FatalLevel )
Log Levels
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 WithCallerFormatter ¶
func WithCallerFormatter(f CallerFormatter) Option
WithCallerFormatter sets the caller formatter option. Default is log.ShortCallerFormatter.
func WithCallerOffset ¶
WithCallerOffset sets the caller offset option. Default is 0.
func WithFields ¶
WithFields sets the fields option. Default is no fields.
func WithFormatter ¶
WithFormatter sets the formatter option. Default is TextFormatter.
func WithLevel ¶
WithLevel sets the level option. Default is log.InfoLevel.
func WithPrefix ¶
WithPrefix sets the prefix option. Default is no prefix.
func WithReportCaller ¶
WithReportCaller sets the report caller option. Default is false.
func WithReportTimestamp ¶
WithReportTimestamp sets the report timestamp option. Default is false.
func WithStyles ¶
WithStyles sets the styles option. Default is DefaultStyles.
func WithTimeFormat ¶
WithTimeFormat sets the time format option. Default is log.DefaultTimeFormat.
func WithTimeFunction ¶
WithTimeFunction 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.
