logger

package
v0.42.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2024 License: Apache-2.0 Imports: 6 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 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.

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.

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. An empty Format defaults to text.

var (
	// FormatText uses the standard slog TextHandler
	FormatText Format = "text"
	// FormatJSON uses the standard slog JSONHandler
	FormatJSON Format = "json"
	// FormatNone sends log writes to DestinationNone / io.Discard
	FormatNone Format = "none"
)

TODO(mkcp): Add dev format

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.

Jump to

Keyboard shortcuts

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