Documentation
¶
Index ¶
- Variables
- func Context(ctx context.Context, logger Logger) context.Context
- func InitializerDefaultLogger(sink io.Writer, opts Options) error
- type Format
- type Level
- type Logger
- type NoOpLogger
- type Options
- type SLogLogger
- func (s *SLogLogger) Debug(msg string, args ...any)
- func (s *SLogLogger) Error(msg string, args ...any)
- func (s *SLogLogger) Info(msg string, args ...any)
- func (s *SLogLogger) Warn(msg string, args ...any)
- func (s *SLogLogger) With(args ...any) Logger
- func (s *SLogLogger) WithContext(ctx context.Context) Logger
Constants ¶
This section is empty.
Variables ¶
var DefaultOptions = Options{ Format: FormatText, Level: slog.LevelInfo, }
DefaultOptions is the default options for the default logger.
var TraceIDKey = "traceID"
TraceIDKey is the key used by loggers for the trace ID field in key/value pairs. It is set as a variable rather than a constant so that it can be changed by users at startup.
Functions ¶
func Context ¶
Context returns a new context built from the provided context with the provided logger in it. The Logger added with Context() can be retrieved with FromContext()
func InitializerDefaultLogger ¶ added in v0.45.0
InitializerDefaultLogger sets the default logger for all SDK logging. It will use slog for logging messages and also set the klog logger to the same slog.Logger. This function is not thread-safe, and should only be called once during initialization.
Types ¶
type Format ¶ added in v0.45.0
type Format string
Format is a the output format of the logger. Supported formats are JSON and Text.
type Logger ¶
type Logger interface { // Debug logs a message at the DEBUG level, with optional arguments as a sequence of key/value pairs // (e.g. Debug("message", "key1", "val1", "key2", "val2")) Debug(msg string, args ...any) // Info logs a message at the INFO level, with optional arguments as a sequence of key/value pairs // (e.g. Info("message", "key1", "val1", "key2", "val2")) Info(msg string, args ...any) // Warn logs a message at the WARN level, with optional arguments as a sequence of key/value pairs // (e.g. Warn("message", "key1", "val1", "key2", "val2")) Warn(msg string, args ...any) // Error logs a message at the ERROR level, with optional arguments as a sequence of key/value pairs // (e.g. Error("message", "key1", "val1", "key2", "val2")) Error(msg string, args ...any) // With returns a Logger with the supplied key/value pair arguments attached to any messages it logs. // This is syntactically equivalent to adding args to every call to a log method on the logger. With(args ...any) Logger // WithContext returns a Logger with the provided context added, such that any subsequent // calls to log methods should pass the context to the underlying handler. WithContext(context.Context) Logger }
Logger is a simple logging interface that exposes methods got writing structured log messages at varying levels, and methods to return an altered logger (With and WithContext).
var ( // DefaultLogger is the default Logger for all SDK logging, if one hasn't been provided in the context. DefaultLogger Logger = &NoOpLogger{} )
func FromContext ¶
FromContext returns the Logger set in the context with Context(), or the DefaultLogger if no Logger is set in the context. If DefaultLogger is nil, it returns a *NoOpLogger so that the return is always valid to call methods on without nil-checking. So long as the Logger to return is not the NoOpLogger, it will carry the context provided in this call (by calling WithContext on the logger in the context).
type NoOpLogger ¶
type NoOpLogger struct{}
NoOpLogger is an implementation of Logger which does nothing when its methods are called
func (*NoOpLogger) Debug ¶
func (*NoOpLogger) Debug(string, ...any)
func (*NoOpLogger) Error ¶
func (*NoOpLogger) Error(string, ...any)
func (*NoOpLogger) Info ¶
func (*NoOpLogger) Info(string, ...any)
func (*NoOpLogger) Warn ¶
func (*NoOpLogger) Warn(string, ...any)
func (*NoOpLogger) With ¶
func (n *NoOpLogger) With(...any) Logger
func (*NoOpLogger) WithContext ¶
func (n *NoOpLogger) WithContext(context.Context) Logger
type Options ¶ added in v0.45.0
type Options struct { // Format is the output format of the logger. // If not set, the default is FormatText. Format Format // Level is the log level of the logger. // If not set, the default is LevelInfo. Level Level // HandlerOptions is the options for the slog.Handler. // If not set, the default is slog.HandlerOptions{Level: LevelInfo}. HandlerOptions slog.HandlerOptions }
Options is a set of options for the default logger.
type SLogLogger ¶
SLogLogger wraps slog.Logger both to override the With() method to return an *SLogLogger, and to have an embedded context.Context, which is passed to the slog.Logger's _Level_Context method when the _Level_ method is called.
func NewSLogLogger ¶
func NewSLogLogger(handler slog.Handler) *SLogLogger
NewSLogLogger creates a new SLogLogger which wraps an *slog.Logger that has a handler to always add a trace ID to the log messages if the context is provided in the log call (e.g. InfoContext())
func (*SLogLogger) Debug ¶
func (s *SLogLogger) Debug(msg string, args ...any)
Debug calls the slog.Logger's DebugContext method with the context provided by WithContext
func (*SLogLogger) Error ¶
func (s *SLogLogger) Error(msg string, args ...any)
Error calls the slog.Logger's ErrorContext method with the context provided by WithContext
func (*SLogLogger) Info ¶
func (s *SLogLogger) Info(msg string, args ...any)
Info calls the slog.Logger's InfoContext method with the context provided by WithContext
func (*SLogLogger) Warn ¶
func (s *SLogLogger) Warn(msg string, args ...any)
Warn calls the slog.Logger's WarnContext method with the context provided by WithContext
func (*SLogLogger) With ¶
func (s *SLogLogger) With(args ...any) Logger
With returns a new *SLogLogger with the provided key/value pairs attached
func (*SLogLogger) WithContext ¶
func (s *SLogLogger) WithContext(ctx context.Context) Logger
WithContext returns an *SLogLogger which still points to the same underlying *slog.Logger, but has the provided context attached for Debug, Info, Warn, and Error calls.