Documentation
¶
Index ¶
- Constants
- Variables
- func Config(opts Options) error
- func Debug(msg string, args ...any)
- func DebugContext(ctx context.Context, msg string, args ...any)
- func Enabled(ctx context.Context, level slog.Level) bool
- func Error(msg string, args ...any)
- func ErrorContext(ctx context.Context, msg string, args ...any)
- func GetLevel() slog.Level
- func Handler() slog.Handler
- 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 New(pkg string) *slog.Logger
- func SetBackend(h slog.Handler)
- func SetDefaultState()
- func SetLevel(l slog.Level)
- func SetLevelStr(l string) error
- func Warn(msg string, args ...any)
- func WarnContext(ctx context.Context, msg string, args ...any)
- func With(args ...any) *slog.Logger
- func WithGroup(name string) *slog.Logger
- type Options
- type OutputFormat
- type ProxyHandler
Constants ¶
const ( DefaultLogLevel = slog.LevelInfo DefaultFormat = FormatPretty )
Variables ¶
var ( DefaultWriter = os.Stderr DefaultTimeFormat = time.StampMilli DefaultPrettyReplaceAttr = func(groups []string, a slog.Attr) slog.Attr { if a.Key == "pkg" { return tint.Attr(243, a) } if a.Key == "status" { code := a.Value.Int64() switch { case code >= 200 && code < 300: return tint.Attr(2, a) case code >= 400 && code < 500: return tint.Attr(3, a) case code >= 500 && code < 600: return tint.Attr(1, a) } } return a } )
Functions ¶
func Config ¶ added in v0.11.0
Config initializes and applies the global logger configuration.
It sets up the global slog.Logger according to the provided Options, controlling:
- output destination (stdout, file, or any io.Writer);
- log format (plain text, JSON, or colorized "pretty");
- optional attribute transformation via ReplaceAttr.
Behavior by format:
FormatText Writes human-readable logs using slog.TextHandler. Suitable for local development or text log files.
FormatJSON Writes structured JSON logs using slog.JSONHandler. Recommended for production environments and log aggregation systems.
FormatPretty Writes developer-friendly, colorized logs using tint.Handler. Colors are automatically disabled if the output is not a terminal.
ReplaceAttr:
- If not nil, it is applied to each attribute before output. Can be used to mask sensitive data, rename fields, or modify messages.
- If nil, attributes are written as-is.
Global effect:
- Config replaces the backend of the global logger.
- All loggers created via log.New, log.With, or log.WithGroup immediately start using the new configuration.
Example:
log.Config(log.Options{
Writer: os.Stdout,
Format: log.FormatPretty,
})
func Debug ¶ added in v0.11.0
Debug logs a message at Debug level. Example: log.Debug("debug message", "key", 123)
func DebugContext ¶ added in v0.11.0
DebugContext logs a Debug-level message with context. Example: log.DebugContext(ctx, "debug with ctx")
func Error ¶ added in v0.11.0
Error logs a message at Error level. Example: log.Error("failed to save file", "file", filename)
func ErrorContext ¶ added in v0.11.0
ErrorContext logs an Error-level message with context. Example: log.ErrorContext(ctx, "request failed")
func Handler ¶ added in v0.11.0
Handler returns the current global slog.Handler. Useful for integration or testing.
func Info ¶ added in v0.11.0
Info logs a message at Info level. Example: log.Info("user logged in", "user", "alice")
func InfoContext ¶ added in v0.11.0
InfoContext logs an Info-level message with context. Example: log.InfoContext(ctx, "operation completed")
func Log ¶ added in v0.11.0
Log outputs a message at the specified level with optional arguments. Example: log.Log(ctx, slog.LevelInfo, "message", "key", 123)
func LogAttrs ¶ added in v0.11.0
LogAttrs outputs a message with structured slog attributes. Example: log.LogAttrs(ctx, slog.LevelInfo, "user login",
slog.String("user", "alice"), slog.Int("id", 42))
func New ¶
New creates a logger scoped to a specific package or component. Example: log.New("users").Info("user created")
func SetBackend ¶ added in v0.11.0
SetBackend replaces the current backend for the global logger. Usually, Config() is sufficient.
func SetDefaultState ¶ added in v0.11.0
func SetDefaultState()
SetDefaultState resets the global logger to its default configuration. Typically used at startup or in tests.
func SetLevel ¶ added in v0.4.0
SetLevel sets the global log level. Example: log.SetLevel(slog.LevelDebug)
func SetLevelStr ¶ added in v0.4.0
SetLevelStr sets the global log level from a string. Example: log.SetLevelStr("INFO")
func WarnContext ¶ added in v0.11.0
WarnContext logs a Warn-level message with context. Example: log.WarnContext(ctx, "rate limit approaching")
Types ¶
type OutputFormat ¶ added in v0.10.0
type OutputFormat string
const ( // FormatText writes plain text logs, human-readable, suitable for local or file output. FormatText OutputFormat = "text" // FormatJSON writes structured JSON logs, recommended for production and log aggregation. FormatJSON OutputFormat = "json" // FormatPretty writes colorized, developer-friendly logs, automatically disables // color if output is not a terminal. FormatPretty OutputFormat = "pretty" )
type ProxyHandler ¶
type ProxyHandler struct {
// contains filtered or unexported fields
}
func NewProxyHandler ¶
func NewProxyHandler() *ProxyHandler