Categorygithub.com/bruceesmith/logger
repositorypackage
1.1.0
Repository: https://github.com/bruceesmith/logger.git
Documentation: pkg.go.dev

# README

Go Reference Go Report Card

logger

import "github.com/bruceesmith/logger"

Package logger supports logging and tracing based on the standard library package log/slog.

Debug, Error, Info and Warn operate like their package slog equivalents, with the level of logging modifiable using SetLevel.

A custom logging level (LevelTrace) can be supplied to SetLevel to enable tracing. Tracing can be unconditional when calling Trace, or only enabled for pre-defined identifiers when calling TraceID. Identifiers for TraceID are registered by calling SetTraceIDs.

By default, all debug, error, info and warn messages go to Stdout, and traces go to Stderr; these destinations can be changed by calling RedirectNormal and RedirectTrace respectively.

A number of settings can be changed for one or both of the normal (non-trace) and trace loggers by calling Configure - the format of log records, their destination, and whether each record contains a timestamp.

When used in cli applications, a cli.Flag representing a LogLevel can be provided using the LogLevelFlag type.

Index

Constants

const (
    // LevelTrace can be set to enable tracing
    LevelTrace slog.Level = -10
)

func Configure

func Configure(setting ...ConfigSetting) error

Configure sets or changes attributes of either the normal or trace loggers

func Debug

func Debug(msg string, args ...any)

Debug emits a debug log

func Error

func Error(msg string, args ...any)

Error emits an error log

func Info

func Info(msg string, args ...any)

Info emits an info log

func Level

func Level() string

Level returns the current logging level as a string

func RedirectStandard

func RedirectStandard(w io.Writer)

RedirectStandard changes the destination for normal (non-trace) logsDestinationSetting argument

Deprecated: RedirectStandard() should be replaced by a call to Configure() with a DestinationSetting argument

func RedirectTrace

func RedirectTrace(w io.Writer)

RedirectTrace changes the destination for normal (non-trace) logs

Deprecated: RedirectTrace() should be replaced by a call to Configure() with a DestinationSetting argument

func SetFormat

func SetFormat(f Format)

SetFormat changes the format of log entries

Deprecated: SetFormat() should be replaced by calls to Configure() with a FormatSetting argument. An advantage of Configure() is that the format of the standard logger can be configured differently to that of the Trace logger

func SetLevel

func SetLevel(l slog.Level)

SetLevel sets the default level of logging

func SetTraceIds

func SetTraceIds(ids ...string)

SetTraceIds registers identifiers for future tracing

func Trace

func Trace(msg string, args ...any)

Trace emits one JSON-formatted log entry if trace level logging is enabled

func TraceID

func TraceID(id string, msg string, args ...any)

TraceID emits one JSON-formatted log entry if tracing is enabled for the requested ID

func Warn

func Warn(msg string, args ...any)

Warn emits a warning log

type ConfigSetting

ConfigSetting is an argument to Configure()

type ConfigSetting struct {
    AppliesTo LogID      // Logger whose setting is set/changed
    Key       SettingKey // Attribute of the logger that is srt/changed
    Value     any        // New value for this attribute
}

type Format

Format determines the format of each log entry

type Format string

const (
    Text Format = "text" // Text format logs
    JSON Format = "json" // JSON format logs
)

type LogID

LogID defines the identifier of a logger

type LogID int

const (
    Norm  LogID = iota // The normal logger
    Tracy              // The trace logger
)

func (LogID) String

func (i LogID) String() string

type LogLevel

LogLevel is the level of logging

type LogLevel int

func (*LogLevel) Set

func (ll *LogLevel) Set(ls string) (err error)

Set is a convenience method for pflag.Value

func (*LogLevel) String

func (ll *LogLevel) String() (s string)

String is a convenience method for pflag.Value

func (*LogLevel) Type

func (ll *LogLevel) Type() string

Type is a conveniene method for pflag.Value

func (*LogLevel) UnmarshalJSON

func (ll *LogLevel) UnmarshalJSON(jason []byte) (err error)

UnmarshalJSON is a convenience method for Kong

type LogLevelFlag

LogLevelFlag is useful for using a LogLevel as a command-line flag in CLI applications

type LogLevelFlag = cli.FlagBase[LogLevel, cli.NoConfig, logLevelValue]

type SettingKey

SettingKey defines a logger setting that can be set or changed via the Configure function

type SettingKey int

const (
    DestinationSetting SettingKey = iota // Output writer / destination for a logger
    FormatSetting                        // Format of log entries
    OmitTimeSetting                      // Whether a timestamp is included in log entries
)

func (SettingKey) String

func (i SettingKey) String() string

type Traces

Traces is the list of trace IDs enabled

type Traces []string

func (*Traces) Set

func (t *Traces) Set(ts string) (err error)

Set is a convenience method for pflag.Value

func (*Traces) String

func (t *Traces) String() (s string)

String is a convenience method for pflag.Value

func (*Traces) Type

func (t *Traces) Type() string

Type is a conveniene method for pflag.Value

Generated by gomarkdoc