Documentation ¶
Index ¶
- Variables
- func AddContextAttrs(attrs ...ContextAttr)
- func Debug(msg string, args ...any)
- func DebugContext(ctx context.Context, msg string, args ...any)
- func Error(msg string, args ...any)
- func ErrorContext(ctx context.Context, msg string, args ...any)
- func GetFn[T any](ctxKey any) func(ctx context.Context) (value any, ok bool)
- func Info(msg string, args ...any)
- func InfoContext(ctx context.Context, msg string, args ...any)
- func Log(ctx context.Context, level slog.Level, msg string, args ...any)
- func LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
- func P[T any](v T) *T
- func SetFn() func(key string, value any) (attr slog.Attr, ok bool)
- func SetInnerHandler(handler slog.Handler)
- func SetJSONHandler(w io.Writer, opts *slog.HandlerOptions)
- func SetLogID(ctx context.Context, logID LogID) context.Context
- func SetLogIdGenerator(gen IDGenerator)
- func SetParentLogID(ctx context.Context, parentLogID LogID) context.Context
- func SetTextHandler(w io.Writer, opts *slog.HandlerOptions)
- func Warn(msg string, args ...any)
- func WarnContext(ctx context.Context, msg string, args ...any)
- func WithChildLogContext(ctx context.Context) context.Context
- func WithLogContext(ctx context.Context) context.Context
- type ByteLogID
- type ContextAttr
- type ContextHandler
- func (h *ContextHandler) Enabled(ctx context.Context, l slog.Level) bool
- func (h *ContextHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *ContextHandler) SetContextAttrs(attrs []ContextAttr) *ContextHandler
- func (h *ContextHandler) SetInnerHandler(ih slog.Handler)
- func (h *ContextHandler) WithAttrs(as []slog.Attr) slog.Handler
- func (h *ContextHandler) WithContextAttrs(attrs ...ContextAttr) *ContextHandler
- func (h *ContextHandler) WithGroup(name string) slog.Handler
- type IDGenerator
- type LogID
- type Logger
- 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) HandleLog(ctx context.Context, level slog.Level, callDepth int, msg string, args ...any)
- func (l *Logger) HandleLogAttrs(ctx context.Context, level slog.Level, callDepth int, msg string, ...)
- 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) 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) Warn(msg string, args ...any)
- func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) With(args ...any) *Logger
- func (l *Logger) WithChildContext(ctx context.Context) (context.Context, *Logger)
- func (l *Logger) WithContext(ctx context.Context) (context.Context, *Logger)
- func (l *Logger) WithContextAttrs(attrs ...ContextAttr) *Logger
- func (l *Logger) WithGroup(name string) *Logger
- type LoggerProvider
- func (p *LoggerProvider) AddContextAttrs(attrs ...ContextAttr)
- func (p *LoggerProvider) NewLogger() *Logger
- func (p *LoggerProvider) NewLoggerWithChildContext(ctx context.Context) (context.Context, *Logger)
- func (p *LoggerProvider) NewLoggerWithContext(ctx context.Context) (context.Context, *Logger)
- func (p *LoggerProvider) NewLoggerWithContextAttrs(attrs ...ContextAttr) *Logger
- func (p *LoggerProvider) SetInnerHandler(handler slog.Handler)
- func (p *LoggerProvider) SetJSONHandler(w io.Writer, opts *slog.HandlerOptions)
- func (p *LoggerProvider) SetTextHandler(w io.Writer, opts *slog.HandlerOptions)
- type StringLogID
Constants ¶
This section is empty.
Variables ¶
var ( // NowFunc returns current time. // This function is exported for testing. NowFunc = func() time.Time { return time.Now() } )
Functions ¶
func AddContextAttrs ¶
func AddContextAttrs(attrs ...ContextAttr)
AddContextAttrs calls LoggerProvider.AddContextAttrs on the default provider.
func DebugContext ¶
DebugContext calls Logger.DebugContext on the default logger.
func ErrorContext ¶
ErrorContext calls Logger.ErrorContext on the default logger.
func GetFn ¶
GetFn returns a ContextAttr's getFn for a value with a given key.
func InfoContext ¶
InfoContext calls Logger.InfoContext on the default logger.
func SetFn ¶
SetFn returns a ContextAttr's setFn.
func SetInnerHandler ¶
SetInnerHandler sets the default logger provider's handler.
func SetJSONHandler ¶
func SetJSONHandler(w io.Writer, opts *slog.HandlerOptions)
SetJSONHandler sets the slog.JSONHandler as the default logger provider's handler.
func SetLogIdGenerator ¶
func SetLogIdGenerator(gen IDGenerator)
SetLogIdGenerator sets the logIdGenerator which generates logId and parentLogId.
func SetTextHandler ¶
func SetTextHandler(w io.Writer, opts *slog.HandlerOptions)
SetTextHandler sets the slog.TextHandler as the default logger provider's handler.
func WarnContext ¶
WarnContext calls Logger.WarnContext on the default logger.
func WithChildLogContext ¶
WithChildLogContext returns a new context with a newly generated logId. If the given context already contains a logId, it is set as the parentLogId.
Types ¶
type ByteLogID ¶
type ByteLogID [8]byte
type ContextAttr ¶
type ContextAttr struct {
// contains filtered or unexported fields
}
ContextAttr represents an attribute obtained from the context.
- key: The key to be included in the log output. If key is empty, the key-value pari is omitted from the log.
- defaultValue: If not nil, defaultValue is used in the log when the valuecannot be obtained from the context using getFn.
- getFn: A function to retrieve the value from the context. The returned value is included in the log. If ok is false, the defaultValue is used. If defaultValue is nil and ok is false, the key-value pair is omitted from the log by default, or the defaultValue is passed to setFn if setFn is provided.
- setFn: A function to create slog.Attr. If setFn is nil, slog.Attr is created with key and value (not nil) as-is.
func Context ¶
func Context( key string, defaultValue any, getFn func(ctx context.Context) (value any, ok bool), setFn func(key string, value any) (attr slog.Attr, ok bool), ) ContextAttr
Context returns an ContextAttr.
func (ContextAttr) Attr ¶
Attr retrieves the attribute from the context and returns it as a slog.Attr. If getFn is provided, it attempts to get the value from the context; otherwise, it uses the defaultValue. If setFn is provided, it uses setFn to create the slog.Attr with the obtained or default value.
type ContextHandler ¶
type ContextHandler struct {
// contains filtered or unexported fields
}
func NewContextHandler ¶
func NewContextHandler(sHandler slog.Handler) *ContextHandler
func (*ContextHandler) Handle ¶
Handle processes the given slog.Record within the context. It enhances the Record's attributes with the context attributes obtained from the context.
func (*ContextHandler) SetContextAttrs ¶
func (h *ContextHandler) SetContextAttrs(attrs []ContextAttr) *ContextHandler
SetContextAttrs returns a new Handler with the given context attributes. The receiver's existing context attributes are replaced.
func (*ContextHandler) SetInnerHandler ¶
func (h *ContextHandler) SetInnerHandler(ih slog.Handler)
func (*ContextHandler) WithContextAttrs ¶
func (h *ContextHandler) WithContextAttrs(attrs ...ContextAttr) *ContextHandler
WithContextAttrs returns a new Handler with the given context attributes appended to the receiver's existing context attributes.
type IDGenerator ¶
type IDGenerator interface {
NewID() LogID
}
type LogID ¶
func GetParentLogID ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func DefaultLogger ¶
func DefaultLogger() *Logger
DefaultProvider returns the logger provided by the default logger provider.
func NewLoggerWithChildContext ¶
NewContextLogger calls LoggerProvider.NewLoggerWithChildContext on the default provider.
func NewLoggerWithContext ¶
NewLoggerWithContext calls LoggerProvider.NewLoggerWithContext on the default provider.
func NewLoggerWithContextAttrs ¶
func NewLoggerWithContextAttrs(attrs ...ContextAttr) *Logger
NewLoggerWithContextAttrs calls LoggerProvider.NewLoggerWithContextAttrs on the default provider.
func (*Logger) DebugContext ¶
func (*Logger) ErrorContext ¶
func (*Logger) HandleLog ¶
func (l *Logger) HandleLog(ctx context.Context, level slog.Level, callDepth int, msg string, args ...any)
HandleLog is the low-level logging method for methods that take ...any. When it is called directly by a logging method or function, set callDepth is 0. If you create a wrapped logging function and want to include source code locations in the log, set the appropriate callDepth.
func (*Logger) HandleLogAttrs ¶
func (l *Logger) HandleLogAttrs(ctx context.Context, level slog.Level, callDepth int, msg string, attrs ...slog.Attr)
HandleLogAttrs is like [Logger.log], but for methods that take ...Attr.
func (*Logger) InfoContext ¶
func (*Logger) WarnContext ¶
func (*Logger) WithChildContext ¶
NewLoggerWithChildContext creates a new child context and a corresponding logger. If the provided context (ctx) has an existing logId, the parentLogId is set to the logId, and it generates a new logId. The child context includes both parentLogId and logId, and a new logger is created based on this child context.
func (*Logger) WithContext ¶
NewLoggerWithContext creates a new context and a corresponding logger. If the provided context (ctx) does not have a logId, a new logId is generated, and it is set to the context. The created logger includes the logId, parentLogId, and other context attributes set in the provider based on the context. The context attributes' default values are set to the values found in the given context, if they exist.
func (*Logger) WithContextAttrs ¶
func (l *Logger) WithContextAttrs(attrs ...ContextAttr) *Logger
WithContextAttrs returns a Logger that includes the given context attributes in each output operation.
type LoggerProvider ¶
type LoggerProvider struct {
// contains filtered or unexported fields
}
func DefaultProvider ¶
func DefaultProvider() *LoggerProvider
DefaultProvider returns the default logger provider.
func NewLoggerProvider ¶
func NewLoggerProvider(innerHandler slog.Handler) *LoggerProvider
NewLoggerProvider returns LoggerProvider.
func (*LoggerProvider) AddContextAttrs ¶
func (p *LoggerProvider) AddContextAttrs(attrs ...ContextAttr)
AddContextAttrs sets the attr (key-value pair) obtained from context to be output to the log. See also ContextAttr.
func (*LoggerProvider) NewLogger ¶
func (p *LoggerProvider) NewLogger() *Logger
NewLogger returns Logger.
func (*LoggerProvider) NewLoggerWithChildContext ¶
NewLoggerWithChildContext returns a context and a logger by Logger.WithChildContext
func (*LoggerProvider) NewLoggerWithContext ¶
NewLoggerWithContextAttrs returns a context and a logger by Logger.WithContextAttrs
func (*LoggerProvider) NewLoggerWithContextAttrs ¶
func (p *LoggerProvider) NewLoggerWithContextAttrs(attrs ...ContextAttr) *Logger
NewLoggerWithContext returns a context and a logger by Logger.WithContext
func (*LoggerProvider) SetInnerHandler ¶
func (p *LoggerProvider) SetInnerHandler(handler slog.Handler)
SetInnerHandler sets the inner handler.
func (*LoggerProvider) SetJSONHandler ¶
func (p *LoggerProvider) SetJSONHandler(w io.Writer, opts *slog.HandlerOptions)
SetJSONHandler sets the slog.JSONHandler as the inner handler.
func (*LoggerProvider) SetTextHandler ¶
func (p *LoggerProvider) SetTextHandler(w io.Writer, opts *slog.HandlerOptions)
SetTextHandler sets the slog.TextHandler as the inner handler.
type StringLogID ¶
type StringLogID string
func (StringLogID) IsZero ¶
func (s StringLogID) IsZero() bool
func (StringLogID) String ¶
func (s StringLogID) String() string