Documentation ¶
Overview ¶
Package slog provides structured logging for our Go services It is a wrapper for the https://pkg.golang.ir/golang.org/x/exp/slog With some extra functionality to configure levels and formatters.
Index ¶
- Constants
- func Configure(cfg Config) error
- func Debug(msg string, args ...any)
- func DebugCtx(ctx context.Context, msg string, args ...any)
- func Error(msg string, args ...any)
- func ErrorCtx(ctx context.Context, msg string, args ...any)
- func Fatal(msg string, args ...any)
- func FatalCtx(ctx context.Context, msg string, args ...any)
- func FromCtx(ctx context.Context) *slog.Logger
- func Info(msg string, args ...any)
- func InfoCtx(ctx context.Context, msg string, args ...any)
- func NewContext(ctx context.Context, log *slog.Logger) context.Context
- func Warn(msg string, args ...any)
- func WarnCtx(ctx context.Context, msg string, args ...any)
- type Config
- type Format
- type Level
- type Logger
Examples ¶
Constants ¶
const ( FormatText = "text" FormatGcloud = "gcloud" )
All available log formats
const ( DefaultLevel = slog.LevelInfo DefaultFormat = FormatGcloud )
Default configurations
Variables ¶
This section is empty.
Functions ¶
func Configure ¶
Configure will change the default logger configuration. It should be called as soon as possible, usually on the main of your program.
func FatalCtx ¶ added in v0.0.5
FatalCtx is equivalent to ErrorCtx() followed by a call to os.Exit(1).
func FromCtx ¶ added in v0.0.2
FromCtx gets the Logger associated with the given context. A default Logger is returned if the context has no Logger associated with it.
func NewContext ¶ added in v0.0.2
NewContext creates a new context.Context with the given Logger associated with it. Call FromCtx to retrieve the Logger.
Types ¶
type Config ¶
Config represents log configuration.
func LoadConfig ¶
LoadConfig will load the log Config of the service from environment variables. The service name is used as a prefix for the environment variables. So a service "TEST" will load the log level from "TEST_LOG_LEVEL".
Available log levels are: "debug", "info", "warn", "error" Available log fmts are: "gcloud", "text"
If the environment variables are not found it will use default values.
Example ¶
package main import ( "github.com/birdie-ai/golibs/slog" ) func main() { _ = slog.Configure(slog.Config{ Level: slog.LevelDebug, Format: slog.FormatText, }) slog.Info("info msg", "key", "val", "key2", 666) _ = slog.Configure(slog.Config{ Level: slog.LevelDebug, Format: slog.FormatGcloud, }) slog.Debug("debug msg", "key", "val", "key2", 666) }
Output: