Documentation
¶
Overview ¶
Package logger provides structured logging (log/slog) with resources in unique.
Bootstrap ¶
Blank-import the use subpackage at the app composition root (not logger itself):
import _ "github.com/omcrgnt/logger/use" logger.Info(ctx, "started", "port", 8080)
logger/use registers DefaultLog and DefaultStdout via unique.MustAddReplaceable. Without logger/use, package funcs are no-op until the logger resource is wired (Inject sets active).
User override ¶
Build and register user resources with unique.Add (replaces system defaults of the same type):
logBuilt, err := cfg.Logger.Build() outBuilt, err := cfg.LogFile.Build() unique.Global().Add(outBuilt) unique.Global().Add(logBuilt)
Types Config, OutputStdoutConfig, and OutputFileConfig implement Build for override.
Usage ¶
Package funcs (Info, Debug, …) and Default delegate to the logger wired by Inject.
For DI, declare the port in Deps:
func (c *Controller) Deps() []any {
return []any{(*logger.Logger)(nil)}
}
Wiring ¶
Concrete logger implements Logger; Deps returns (*Output)(nil). DefaultLog and DefaultStdout are for logger/use system registration.
Index ¶
- func Debug(ctx context.Context, msg string, args ...any)
- func DefaultLog() any
- func DefaultStdout() any
- func Error(ctx context.Context, msg string, args ...any)
- func Info(ctx context.Context, msg string, args ...any)
- func Warn(ctx context.Context, msg string, args ...any)
- type Config
- type Logger
- type Output
- type OutputFileConfig
- type OutputStdoutConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultLog ¶
func DefaultLog() any
DefaultLog returns the system Logger resource for logger/use registration.
func DefaultStdout ¶ added in v0.1.1
func DefaultStdout() any
DefaultStdout returns the system Output resource for logger/use registration.
Types ¶
type Config ¶
Config configures the process logger (level, format). ecfg fills and protovalidates Level and Format before Build is called.
type Logger ¶
type Logger interface {
Debug(ctx context.Context, msg string, args ...any)
Info(ctx context.Context, msg string, args ...any)
Warn(ctx context.Context, msg string, args ...any)
Error(ctx context.Context, msg string, args ...any)
}
Logger is the logging API.
type OutputFileConfig ¶
type OutputFileConfig struct {
Path string `ecfg:"PATH"`
}
OutputFileConfig opens a file log sink (user resource via Build).
func (OutputFileConfig) Build ¶
func (c OutputFileConfig) Build() (any, error)
Build returns an Output writing to Path.
type OutputStdoutConfig ¶ added in v0.1.1
type OutputStdoutConfig struct{}
OutputStdoutConfig registers stdout as the logger Output (user resource via Build).
func (OutputStdoutConfig) Build ¶ added in v0.1.1
func (c OutputStdoutConfig) Build() (any, error)
Build returns an Output writing to os.Stdout.