Documentation ¶
Overview ¶
Package logger implements a log/slog based logger in Zarf.
Index ¶
- Variables
- func Default() *slog.Logger
- func Enabled(ctx context.Context) bool
- func From(ctx context.Context) *slog.Logger
- func New(cfg Config) (*slog.Logger, error)
- func SetDefault(l *slog.Logger)
- func WithContext(ctx context.Context, logger *slog.Logger) context.Context
- func WithLoggingEnabled(ctx context.Context, enabled bool) context.Context
- type Color
- type Config
- type Destination
- type Format
- type Level
Constants ¶
This section is empty.
Variables ¶
var ( Debug = Level(slog.LevelDebug) // -4 Info = Level(slog.LevelInfo) // 0 Warn = Level(slog.LevelWarn) // 4 Error = Level(slog.LevelError) // 8 )
Store names for Levels
Functions ¶
func Default ¶
Default retrieves a logger from the package default. This is intended as a fallback when a logger cannot easily be passed in as a dependency, like when developing a new function. Use it like you would use context.TODO().
func From ¶ added in v0.42.1
From takes a context and reads out a *slog.Logger. If From does not find a value it will return a discarding logger similar to log-format "none".
func SetDefault ¶
SetDefault takes a logger and atomically stores it as the package default. This is intended to be called when the application starts to override the default config with application-specific config. See Default() for more usage details.
func WithContext ¶ added in v0.42.1
WithContext takes a context.Context and a *slog.Logger, storing it on the key
Types ¶
type Color ¶ added in v0.45.0
type Color bool
Color is a type that represents whether or not to use color in the logger.
type Config ¶
type Config struct { // Level sets the log level. An empty value corresponds to Info aka 0. Level Format Destination Color }
Config is configuration for a logger.
func ConfigDefault ¶
func ConfigDefault() Config
ConfigDefault returns a Config with defaults like Text formatting at Info level writing to Stderr.
type Destination ¶
Destination declares an io.Writer to send logs to.
var ( // DestinationDefault points to Stderr DestinationDefault Destination = os.Stderr // DestinationNone discards logs as they are received DestinationNone Destination = io.Discard )
type Format ¶
type Format string
Format declares the kind of logging handler to use. NOTE(mkcp): An empty Format defaults to "none" while logger is being worked on, but this is intended to use "text" on release.
var ( // FormatJSON uses the standard slog JSONHandler FormatJSON Format = "json" // FormatConsole uses console-slog to provide prettier colorful messages FormatConsole Format = "console" // FormatDev uses a verbose and pretty printing devslog handler FormatDev Format = "dev" // FormatNone sends log writes to DestinationNone / io.Discard FormatNone Format = "none" )
type Level ¶
type Level int
Level declares each supported log level. These are 1:1 what log/slog supports by default. Info is the default level.
func ParseLevel ¶
ParseLevel takes a string representation of a Level, ensure it exists, and then converts it into a Level.