Documentation
¶
Overview ¶
Package logkit provides a structured logging wrapper over slog.Logger with enhanced context handling, custom levels, and flexible configuration.
Index ¶
- Constants
- Variables
- type Config
- type Logger
- func (logg Logger) Debug(ctx context.Context, msg string, args ...any)
- func (logg Logger) Error(ctx context.Context, msg string, args ...any)
- func (logg Logger) Fatal(ctx context.Context, msg string, args ...any)
- func (logg Logger) Info(ctx context.Context, msg string, args ...any)
- func (logg Logger) Trace(ctx context.Context, msg string, args ...any)
- func (logg Logger) Verbose(ctx context.Context, msg string, args ...any)
- func (logg Logger) Warn(ctx context.Context, msg string, args ...any)
- func (logg Logger) With(args ...any) *Logger
- type Option
Constants ¶
const ( // DefaultTimeTemplate is a default time format string. DefaultTimeTemplate = "02.01.2006 15:04:05.000" // DefaultLogType is a default log type. DefaultLogType = "json" // DefaultLevel is a default log level. DefaultLevel = "error" // DefaultLevelValue is a default log level value. DefaultLevelValue = slog.LevelError // DefaultWriter is a default writer to use for logging. DefaultWriter = "stdout" )
const ( // LevelTrace is a trace log level - the lowest possible level. LevelTrace = slog.Level(-8) // LevelDebug is an alias for slog.LevelDebug. LevelDebug = slog.LevelDebug // LevelVerbose is a verbose log level - the middleground between Debug and Info levels. LevelVerbose = slog.Level(-2) // LevelInfo is an alias for slog.LevelInfo. LevelInfo = slog.LevelInfo // LevelWarn is an alias for slog.LevelWarn. LevelWarn = slog.LevelWarn // LevelError is an alias for slog.LevelError. LevelError = slog.LevelError // LevelFatal is a fatal log level - the highest possible level. LevelFatal = slog.Level(16) )
Additional log levels.
Variables ¶
var DefaultWriterValue = os.Stdout
DefaultWriterValue is a default writer value.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config defines an inner logger configuration.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a wrapper around slog.Logger that supports:
- automatic context field injection.
- custom log levels (TRACE, VERBOSE, FATAL).
- configurable time format and output.
func NewLogger ¶
NewLogger returns a new Logger with the given log type and level. If no opts are provided, it returns a default logger.
The log type can be "text" or "json". The log level can be "debug", "info", "warn" or "error".
timeTemplate is a time format string. Any format which is valid for time.Time format is acceptable.
Empty log level corresponds to "error", as well as empty log type corresponds to "json". Empty time format is equal to the default value which is "02.01.2006 15:04:05.000". Empty writer option equals to using os.Stdout. Custom writer might be set using WithWriter option.
If the log type or level is unknown, it returns an error.
func (Logger) Fatal ¶
Fatal logs a message with level Error on the standard logger and then calls os.Exit(1).
type Option ¶
Option defines a function that allows to configure underlying logger on construction.
func WithConfig ¶
WithConfig allows to apply custom configuration. Expected following config structure:
{
format string, // "text" or "json"
level string, // "debug", "info", "warn", "error"
time_template string, // any valid time format
log_stream: string, // "stdout", "stderr"
}
func WithDefaults ¶
func WithDefaults() Option
WithDefaults applies default configuration to the logger. May be overwritten by WithConfig and/or WithWriter options.
func WithExtraContextFields ¶
WithExtraContextFields configures the logger to automatically include values from the context in every log record, using the provided keys.
The keys must be comparable (as required by context.Context) and are used directly in ctx.Value(key) to retrieve the corresponding values. Compatible key types are:
- string: used directly as the attribute key in logs.
- Any type implementing fmt.Stringer: its String() method is used as the attribute key.
This includes named string types (e.g., type MyString string) that implement fmt.Stringer. The logger will use these keys to extract values from the context and add them as log attributes:
- If a value is of type slog.Attr, it is added to the log as is - using internal key in slog.Attr pair.
- Otherwise, it is added as a regular attribute with the key as described above.
If no key is found in the context, it is safely ignored by the logger.
Example:
var RequestIDKey struct{}
func (RequestIDKey) String() string { return "request_id" }
logger := NewLogger(WithExtraContextFields(RequestIDKey))
ctx := context.WithValue(ctx, RequestIDKey, "abc-123")
logger.Info(ctx, "event") // → request_id=abc-123
If no fields are provided, the option does nothing and returns nil. If any key types are invalid, a single error listing all erroneous types is returned.
func WithWriter ¶
WithWriter allows to apply custom configuration.