logger

package module
v1.3.6 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 11 Imported by: 1

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 TraceIDs

func TraceIDs() []string

TraceIDs returns the list of enabled trace IDs

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

Documentation

Overview

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

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

Variables

This section is empty.

Functions

func Configure added in v1.1.0

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 deprecated

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 deprecated

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 deprecated

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 TraceIDs added in v1.3.0

func TraceIDs() []string

TraceIDs returns the list of enabled trace IDs

func Warn

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

Warn emits a warning log

Types

type ConfigSetting added in v1.1.0

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
}

ConfigSetting is an argument to Configure()

type Format

type Format string

Format determines the format of each log entry

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

type LogID added in v1.1.0

type LogID int

LogID defines the identifier of a logger

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

func (LogID) String added in v1.1.0

func (i LogID) String() string

type LogLevel

type LogLevel int

LogLevel is the level of logging

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

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

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

type SettingKey added in v1.1.0

type SettingKey int

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

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 added in v1.1.0

func (i SettingKey) String() string

type Traces

type Traces []string

Traces is the list of trace IDs enabled

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

Jump to

Keyboard shortcuts

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