logger

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2025 License: MIT Imports: 10 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.

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

Index

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

func RedirectStandard

func RedirectStandard(w io.Writer)

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

func RedirectTrace

func RedirectTrace(w io.Writer)

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

func SetFormat

func SetFormat(f Format)

SetFormat changes the format of log entries

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 Format

Format determines the format of each log entry

type Format string

const (
    // LevelTrace can be set to enable tracing
    LevelTrace slog.Level = -10
    // Text format
    Text Format = "text"
    // JSON format
    JSON Format = "json"
)

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 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.

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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

func RedirectStandard

func RedirectStandard(w io.Writer)

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

func RedirectTrace

func RedirectTrace(w io.Writer)

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

func SetFormat

func SetFormat(f Format)

SetFormat changes the format of log entries

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

Types

type Format

type Format string

Format determines the format of each log entry

const (
	// LevelTrace can be set to enable tracing
	LevelTrace slog.Level = -10
	// Text format
	Text Format = "text"
	// JSON format
	JSON Format = "json"
)

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