Documentation
¶
Index ¶
- Constants
- Variables
- type Attr
- type AttrKind
- type AttrReplaceFunc
- type AttrValue
- type Color
- type ColoredHandler
- func (h *ColoredHandler) Dispose() error
- func (h *ColoredHandler) Enabled(ctx context.Context, level Level) bool
- func (h *ColoredHandler) Flags() RecordFlag
- func (h *ColoredHandler) Handle(ctx context.Context, r Record) error
- func (h *ColoredHandler) WithAttrs(attrs ...Attr) Handler
- func (h *ColoredHandler) WithGroup(group string) Handler
- type Config
- type DefaultHandler
- func (h *DefaultHandler) Dispose() error
- func (h *DefaultHandler) Enabled(ctx context.Context, level Level) bool
- func (h *DefaultHandler) Flags() RecordFlag
- func (h *DefaultHandler) Handle(ctx context.Context, r Record) error
- func (h *DefaultHandler) WithAttrs(attrs ...Attr) Handler
- func (h *DefaultHandler) WithGroup(group string) Handler
- type DefaultLogger
- func (l *DefaultLogger[LVL]) BUG(msg string, args ...any)
- func (l *DefaultLogger[LVL]) Debug(msg string, args ...any)
- func (l *DefaultLogger[LVL]) Deprecated(msg string, args ...any)
- func (l *DefaultLogger[LVL]) Error(msg string, err error, args ...any)
- func (l *DefaultLogger[LVL]) HTTP(status int, path string, args ...any)
- func (l *DefaultLogger[LVL]) Info(msg string, args ...any)
- func (l *DefaultLogger[LVL]) Issue(msg string, args ...any)
- func (l *DefaultLogger[LVL]) Level() LevelIface
- func (l *DefaultLogger[LVL]) Log(level LevelIface, msg string, args ...any)
- func (l *DefaultLogger[LVL]) LogDepth(ctx context.Context, calldepth int, lvl LevelIface, msg string, err error, ...)
- func (l *DefaultLogger[LVL]) Msg(msg string, args ...any)
- func (l *DefaultLogger[LVL]) Msgf(format string, args ...any)
- func (l *DefaultLogger[LVL]) NotImplemented(msg string, args ...any)
- func (l *DefaultLogger[LVL]) Notice(msg string, args ...any)
- func (l *DefaultLogger[LVL]) Ok(msg string, args ...any)
- func (l *DefaultLogger[LVL]) SetLevel(level LevelIface)
- func (l *DefaultLogger[LVL]) SystemDebug(msg string, args ...any)
- func (l *DefaultLogger[LVL]) Task(name, description string) TaskInfo
- func (l *DefaultLogger[LVL]) Warn(msg string, args ...any)
- func (l *DefaultLogger[LVL]) With(args ...any) Logger
- func (l *DefaultLogger[LVL]) WithGroup(group string) Logger
- type Handler
- type HandlerKind
- type JSONHandler
- func (h *JSONHandler) Dispose() error
- func (h *JSONHandler) Enabled(ctx context.Context, level Level) bool
- func (h *JSONHandler) Flags() RecordFlag
- func (h *JSONHandler) Handle(ctx context.Context, r Record) error
- func (h *JSONHandler) WithAttrs(attrs ...Attr) Handler
- func (h *JSONHandler) WithGroup(group string) Handler
- type Level
- func (l Level) Int() int
- func (l Level) MarshalJSON() ([]byte, error)
- func (l Level) MarshalSetting() ([]byte, error)
- func (l Level) MarshalText() ([]byte, error)
- func (l Level) SettingKind() settings.Kind
- func (l Level) String() string
- func (l *Level) UnmarshalJSON(data []byte) error
- func (l *Level) UnmarshalSetting(data []byte) error
- func (l *Level) UnmarshalText(data []byte) error
- type LevelIface
- type Logger
- type LoggerIface
- type Record
- type RecordFlag
- type RecordTimestampKind
- type Settings
- type TaskInfo
- type TextHandler
- func (h *TextHandler) Dispose() error
- func (h *TextHandler) Enabled(ctx context.Context, level Level) bool
- func (h *TextHandler) Flags() RecordFlag
- func (h *TextHandler) Handle(ctx context.Context, r Record) error
- func (h *TextHandler) WithAttrs(attrs ...Attr) Handler
- func (h *TextHandler) WithGroup(group string) Handler
Constants ¶
const ( // TimeKey is the key used by the built-in handlers for the time // when the log method is called. The associated Value is a [time.Time]. TimeKey = "time" // LevelKey is the key used by the built-in handlers for the level // of the log call. The associated value is a [Level]. LevelKey = "level" // MessageKey is the key used by the built-in handlers for the // message of the log call. The associated value is a string. MessageKey = "message" // SourceKey is the key used by the built-in handlers for the source file // and line of the log call. The associated value is a string. SourceKey = "source" // ErrorKey is the key used for errors by Logger.Error. // The associated value is an [error]. ErrorKey = "error" // DataKey is the key used for data constructed from logger arguments. DataKey = "data" )
Keys for "built-in" attributes.
Variables ¶
var ( ErrAttrReplace = errors.New("replace attr error") ErrUnknownLevelName = errors.New("unknown level name") )
Functions ¶
This section is empty.
Types ¶
type ColoredHandler ¶
type ColoredHandler struct {
// contains filtered or unexported fields
}
func (*ColoredHandler) Dispose ¶
func (h *ColoredHandler) Dispose() error
func (*ColoredHandler) Enabled ¶
func (h *ColoredHandler) Enabled(ctx context.Context, level Level) bool
func (*ColoredHandler) Flags ¶
func (h *ColoredHandler) Flags() RecordFlag
func (*ColoredHandler) Handle ¶
func (h *ColoredHandler) Handle(ctx context.Context, r Record) error
func (*ColoredHandler) WithAttrs ¶
func (h *ColoredHandler) WithAttrs(attrs ...Attr) Handler
func (*ColoredHandler) WithGroup ¶
func (h *ColoredHandler) WithGroup(group string) Handler
type Config ¶
type Config struct { // AddSource is set to true then the handler adds a ("source", "file:line") // attribute to the output indicating the source code position of the log // statement. AddSource is false by default to skip the cost of computing // this information. AddSource bool Colors bool // Level reports the minimum record level that will be logged. // The handler discards records with lower levels. // Default: LevelInfo Level Level // Secrets is a collection of log attr keys whose values are masked in logs. Secrets []string ReplaceAttr []AttrReplaceFunc TimeLoc string TimeLayout string DefaultHandler HandlerKind }
type DefaultHandler ¶
type DefaultHandler struct {
// contains filtered or unexported fields
}
func (*DefaultHandler) Dispose ¶
func (h *DefaultHandler) Dispose() error
func (*DefaultHandler) Enabled ¶
func (h *DefaultHandler) Enabled(ctx context.Context, level Level) bool
func (*DefaultHandler) Flags ¶
func (h *DefaultHandler) Flags() RecordFlag
func (*DefaultHandler) Handle ¶
func (h *DefaultHandler) Handle(ctx context.Context, r Record) error
func (*DefaultHandler) WithAttrs ¶
func (h *DefaultHandler) WithAttrs(attrs ...Attr) Handler
func (*DefaultHandler) WithGroup ¶
func (h *DefaultHandler) WithGroup(group string) Handler
type DefaultLogger ¶
type DefaultLogger[LVL LevelIface] struct { // contains filtered or unexported fields }
func New ¶
func New(config Config) (*DefaultLogger[Level], error)
New creates and returns new logger.
func Simple ¶
func Simple(level Level) *DefaultLogger[Level]
func (*DefaultLogger[LVL]) BUG ¶
func (l *DefaultLogger[LVL]) BUG(msg string, args ...any)
BUG logs at logging.LevelBUG.
func (*DefaultLogger[LVL]) Debug ¶
func (l *DefaultLogger[LVL]) Debug(msg string, args ...any)
Debug logs at logging.LevelDebug.
func (*DefaultLogger[LVL]) Deprecated ¶
func (l *DefaultLogger[LVL]) Deprecated(msg string, args ...any)
Deprecated logs at logging.LevelDeprecated.
func (*DefaultLogger[LVL]) Error ¶
func (l *DefaultLogger[LVL]) Error(msg string, err error, args ...any)
Error logs at logging.LevelError.
func (*DefaultLogger[LVL]) HTTP ¶
func (l *DefaultLogger[LVL]) HTTP(status int, path string, args ...any)
func (*DefaultLogger[LVL]) Info ¶
func (l *DefaultLogger[LVL]) Info(msg string, args ...any)
Info logs at logging.LevelInfo.
func (*DefaultLogger[LVL]) Issue ¶
func (l *DefaultLogger[LVL]) Issue(msg string, args ...any)
Issue logs at logging.LevelIssue.
func (*DefaultLogger[LVL]) Level ¶
func (l *DefaultLogger[LVL]) Level() LevelIface
Level returns current log level.
func (*DefaultLogger[LVL]) Log ¶
func (l *DefaultLogger[LVL]) Log(level LevelIface, msg string, args ...any)
Log logs at given log level.
func (*DefaultLogger[LVL]) LogDepth ¶
func (l *DefaultLogger[LVL]) LogDepth(ctx context.Context, calldepth int, lvl LevelIface, msg string, err error, args ...any)
LogDepth is like Logger.Log, but accepts a call depth to adjust the file and line number in the log record. 1 refers to the caller of LogDepth; 2 refers to the caller's caller; and so on. Level argument ensures that logger ignores records whose level is lower however registered handlerer may ignore records if record level does not meet minimum level required by the handler.
func (*DefaultLogger[LVL]) Msg ¶
func (l *DefaultLogger[LVL]) Msg(msg string, args ...any)
Msg logs at logging.LevelAlways.
func (*DefaultLogger[LVL]) Msgf ¶
func (l *DefaultLogger[LVL]) Msgf(format string, args ...any)
Msg logs at logging.LevelAlways.
func (*DefaultLogger[LVL]) NotImplemented ¶
func (l *DefaultLogger[LVL]) NotImplemented(msg string, args ...any)
NotImplemented logs at logging.LevelNotImplemented.
func (*DefaultLogger[LVL]) Notice ¶
func (l *DefaultLogger[LVL]) Notice(msg string, args ...any)
Notice logs at logging.LevelNotice.
func (*DefaultLogger[LVL]) Ok ¶
func (l *DefaultLogger[LVL]) Ok(msg string, args ...any)
Ok logs at logging.LevelOk.
func (*DefaultLogger[LVL]) SetLevel ¶
func (l *DefaultLogger[LVL]) SetLevel(level LevelIface)
SetLevel sets level for logger.
func (*DefaultLogger[LVL]) SystemDebug ¶
func (l *DefaultLogger[LVL]) SystemDebug(msg string, args ...any)
SystemDebug logs at logging.LevelSystemDebug.
func (*DefaultLogger[LVL]) Task ¶
func (l *DefaultLogger[LVL]) Task(name, description string) TaskInfo
Task logs at logging.LevelTask and returns slog group with task info which can be used as logging attribute argument when logging task related events.
func (*DefaultLogger[LVL]) Warn ¶
func (l *DefaultLogger[LVL]) Warn(msg string, args ...any)
Warn logs at logging.LevelWarn.
func (*DefaultLogger[LVL]) With ¶
func (l *DefaultLogger[LVL]) With(args ...any) Logger
func (*DefaultLogger[LVL]) WithGroup ¶
func (l *DefaultLogger[LVL]) WithGroup(group string) Logger
type Handler ¶
type Handler interface { // Enabled reports whether the handler handles records at the given level. // The handler ignores records whose level is lower. // It is called early, before any arguments are processed, // to save effort if the log event should be discarded. // The Logger's context is passed so Enabled can use its values // to make a decision. The context may be nil. Enabled(ctx context.Context, level Level) bool // Handle handles the Record. // It will only be called if Enabled returns true. // // The first argument is the context of the Logger that created the Record, // which may be nil. // It is present solely to provide Handlers access to the context's values. // Canceling the context should not affect record processing. // (Among other things, log messages may be necessary to debug a // cancellation-related problem.) // // Handle methods that produce output should observe the following rules: // - If r.Time is the zero time, ignore the time. // - If an Attr's key is the empty string and the value is not a group, // ignore the Attr. // - If a group's key is empty, inline the group's Attrs. // - If a group has no Attrs (even if it has a non-empty key), // ignore it. Handle(ctx context.Context, r Record) error // WithAttrs returns a new Handler whose attributes consist of // both the receiver's attributes and the arguments. // The Handler owns the slice: it may retain, modify or discard it. // [Logger.With] will resolve the Attrs. WithAttrs(attrs ...Attr) Handler // WithGroup returns a new Handler with the given group appended to // the receiver's existing groups. // The keys of all subsequent attributes, whether added by With or in a // Record, should be qualified by the sequence of group names. // // How this qualification happens is up to the Handler, so long as // this Handler's attribute keys differ from those of another Handler // with a different sequence of group names. // // A Handler should treat WithGroup as starting a Group of Attrs that ends // at the end of the log event. That is, // // logger.WithGroup("s").LogAttrs(level, msg, slog.Int("a", 1), slog.Int("b", 2)) // // should behave like // // logger.LogAttrs(level, msg, slog.Group("s", slog.Int("a", 1), slog.Int("b", 2))) // // If the name is empty, WithGroup returns the receiver. WithGroup(name string) Handler // Dispose discards handlerer and frees resources used by handler Dispose() error // Flags returns record flags for handler. Flags() RecordFlag }
Handler handles log records produced by a Logger.
A typical handler may print log records to standard error, or write them to a file or database, or perhaps augment them with additional attributes and pass them on to another handler.
Any of the Handler's methods may be called concurrently with itself or with other methods. It is the responsibility of the Handler to manage this concurrency.
Users of the slog package should not invoke Handler methods directly. They should use the methods of Logger instead.
type HandlerKind ¶
type HandlerKind uint8
const ( WithDefaultHandler HandlerKind = 1 << iota WithTextHandler WithColoredHandler WithJSONHandler )
func (HandlerKind) MarshalSetting ¶
func (hk HandlerKind) MarshalSetting() ([]byte, error)
func (HandlerKind) SettingKind ¶
func (hk HandlerKind) SettingKind() settings.Kind
func (HandlerKind) String ¶
func (hk HandlerKind) String() string
func (*HandlerKind) UnmarshalSetting ¶
func (hk *HandlerKind) UnmarshalSetting(data []byte) error
func (*HandlerKind) UnmarshalText ¶
func (hk *HandlerKind) UnmarshalText(data []byte) error
type JSONHandler ¶
type JSONHandler struct {
// contains filtered or unexported fields
}
func (*JSONHandler) Dispose ¶
func (h *JSONHandler) Dispose() error
func (*JSONHandler) Flags ¶
func (h *JSONHandler) Flags() RecordFlag
func (*JSONHandler) WithAttrs ¶
func (h *JSONHandler) WithAttrs(attrs ...Attr) Handler
func (*JSONHandler) WithGroup ¶
func (h *JSONHandler) WithGroup(group string) Handler
type Level ¶
type Level int32
const ( LevelSystemDebug Level = -10 // Level(slog.LevelDebug - 1) LevelDebug Level = -4 // Level(slog.LevelDebug) LevelInfo Level = 0 // Level(slog.LevelInfo) LevelTask Level = 1 // Level(slog.LevelInfo + 1) LevelOk Level = 2 // Level(slog.LevelInfo + 2) LevelNotice Level = 3 // Level(slog.LevelInfo + 3) LevelWarn Level = 4 // Level(slog.LevelWarn) LevelNotImplemented Level = 5 // Level(slog.LevelWarn + 1) LevelDeprecated Level = 6 // Level(slog.LevelWarn + 2) LevelIssue Level = 7 // Level(slog.LevelWarn + 3) LevelError Level = 8 // Level(slog.LevelError) LevelBUG Level = math.MaxInt32 - 2 LevelAlways Level = math.MaxInt32 - 1 LevelQuiet Level = math.MaxInt32 )
func (Level) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler by quoting the output of Level.String.
func (Level) MarshalSetting ¶
func (Level) MarshalText ¶
MarshalText implements encoding.TextMarshaler by calling Level.String.
func (Level) SettingKind ¶
func (*Level) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json.Unmarshaler It accepts any string produced by Level.MarshalJSON, ignoring case. It also accepts numeric offsets that would result in a different string on output. For example, "Error-8" would marshal as "INFO".
func (*Level) UnmarshalSetting ¶
func (*Level) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler. It accepts any string produced by Level.MarshalText, ignoring case. It also accepts numeric offsets that would result in a different string on output. For example, "Error-8" would marshal as "INFO".
type LevelIface ¶
type Logger ¶
type Logger interface { Level() LevelIface SetLevel(LevelIface) SystemDebug(msg string, args ...any) Debug(msg string, args ...any) Info(msg string, args ...any) Ok(msg string, args ...any) Notice(msg string, args ...any) Warn(msg string, args ...any) NotImplemented(msg string, args ...any) Deprecated(msg string, args ...any) Issue(msg string, args ...any) Error(msg string, err error, args ...any) BUG(msg string, args ...any) Msg(msg string, args ...any) Msgf(format string, args ...any) Log(level LevelIface, msg string, args ...any) HTTP(status int, path string, args ...any) LogDepth(ctx context.Context, calldepth int, level LevelIface, msg string, err error, args ...any) With(args ...any) Logger WithGroup(group string) Logger }
type LoggerIface ¶
type LoggerIface[LVL LevelIface] interface { Level() LevelIface SetLevel(LevelIface) SystemDebug(msg string, args ...any) Debug(msg string, args ...any) Info(msg string, args ...any) Ok(msg string, args ...any) Notice(msg string, args ...any) Warn(msg string, args ...any) NotImplemented(msg string, args ...any) Deprecated(msg string, args ...any) Issue(msg string, args ...any) Error(msg string, err error, args ...any) BUG(msg string, args ...any) Msg(msg string, args ...any) Msgf(format string, args ...any) Log(level LevelIface, msg string, args ...any) HTTP(status int, path string, args ...any) LogDepth(ctx context.Context, calldepth int, level LevelIface, msg string, err error, args ...any) With(args ...any) Logger WithGroup(group string) Logger }
type RecordFlag ¶
type RecordFlag uint8
const ( WithRecordTimestamp RecordFlag = 1 << iota WithRecordLevel WithRecordMessage WithRecordError WithRecordData WithRecordSource WithAllFields = WithRecordTimestamp | WithRecordLevel | WithRecordMessage | WithRecordError | WithRecordData | WithRecordSource )
func (RecordFlag) Has ¶
func (rf RecordFlag) Has(flag RecordFlag) bool
type RecordTimestampKind ¶
type RecordTimestampKind int
const ( RecordTimestampLayout RecordTimestampKind = iota RecordTimestampUnix RecordTimestampUnixMilli RecordTimestampUnixMicro RecordTimestampUnixNano )
type Settings ¶
type Settings struct { Level Level `key:"level" default:"ok" mutation:"mutable"` Secrets settings.String `key:"secrets" mutation:"once"` Source settings.Bool `key:"source" default:"false" mutation:"once"` Handler HandlerKind `key:"default.handler" default:"colored" mutation:"once"` TimestampEnabled settings.Bool `key:"timestamp.enabled" default:"true" mutation:"once"` TimestampFormat settings.String `key:"timestamp.format" default:"15:04:05.999" mutation:"once"` TimestampLocation settings.String `key:"timestamp.location" default:"Local" mutation:"once"` SlogGlobal settings.Bool `key:"slog.global" default:"false" mutation:"once"` }
type TaskInfo ¶
type TaskInfo struct { // Name of the task Name string // Description of the task Description string // contains filtered or unexported fields }
TaskInfo is used to log task info
type TextHandler ¶
type TextHandler struct {
// contains filtered or unexported fields
}
func (*TextHandler) Dispose ¶
func (h *TextHandler) Dispose() error
func (*TextHandler) Flags ¶
func (h *TextHandler) Flags() RecordFlag
func (*TextHandler) WithAttrs ¶
func (h *TextHandler) WithAttrs(attrs ...Attr) Handler
func (*TextHandler) WithGroup ¶
func (h *TextHandler) WithGroup(group string) Handler