logger

package
v0.44.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package logger implements a log/slog based logger in Zarf.

Index

Constants

This section is empty.

Variables

View Source
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

func Default() *slog.Logger

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 Enabled added in v0.42.1

func Enabled(ctx context.Context) bool

Enabled returns true if slog logging is enabled

func From added in v0.42.1

func From(ctx context.Context) *slog.Logger

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 New

func New(cfg Config) (*slog.Logger, error)

New takes a Config and returns a validated logger.

func SetDefault

func SetDefault(l *slog.Logger)

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

func WithContext(ctx context.Context, logger *slog.Logger) context.Context

WithContext takes a context.Context and a *slog.Logger, storing it on the key

func WithLoggingEnabled added in v0.42.1

func WithLoggingEnabled(ctx context.Context, enabled bool) context.Context

WithLoggingEnabled allows stores a value to determine whether or not slog logging is enabled

Types

type Config

type Config struct {
	// Level sets the log level. An empty value corresponds to Info aka 0.
	Level
	Format
	Destination
}

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.

func (Config) LogValue added in v0.42.2

func (c Config) LogValue() slog.Value

LogValue of config

type Destination

type Destination io.Writer

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 (
	// FormatConsoleNoColor uses the standard slog TextHandler
	FormatConsoleNoColor Format = "console-no-color"
	// 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"
)

func (Format) ToLower

func (f Format) ToLower() Format

ToLower takes a Format string and converts it to lowercase for case-agnostic validation. Users shouldn't have to care about "json" vs. "JSON" for example - they should both work.

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

func ParseLevel(s string) (Level, error)

ParseLevel takes a string representation of a Level, ensure it exists, and then converts it into a Level.

func (Level) String added in v0.42.2

func (l Level) String() string

String returns the string representation of the Level.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL