logr

package module
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: Unlicense Imports: 9 Imported by: 0

README

logr v2

A logging utility in Go. See the GoDoc

Documentation

Index

Examples

Constants

View Source
const (
	None Type = iota      // None
	P    Type = 1 << iota // Panic
	E                     // Error
	W                     // Warning
	I                     // Info
	D                     // Debug
	S                     // Success

	Critical = P | E
	Monitor  = Critical | W
	Verbose  = Monitor | I | S
	All      = Verbose | D
)

Available log types

View Source
const (
	// ColourReset code
	ColourReset = "\x1B[0m"
)

Variables

This section is empty.

Functions

func AddWriter

func AddWriter(w io.Writer, configs ...WriterConfigModifier) (stop func())

AddWriter add a io.Writer to the collection of writers that store the log messages.

Example
AddWriter(os.Stdout)
// which is equivalent to
// 		AddWriter(os.Stdout, WithFormatter(FormatDefault), WithFilter(All))

func Debug

func Debug(v ...any) string

Debug logs inputs as debug messages

func Debugf

func Debugf(msg string, v ...any) string

Debugf logs a formatted message as a debug message

func Error

func Error(v ...any) string

Error logs inputs as errors

func Errorf

func Errorf(msg string, v ...any) string

Errorf logs a formatted message as an error

func FormatDefault

func FormatDefault(m *Message) []byte

LogrFormat is a Formatter that converts a Message to a default format.

func FormatJSON

func FormatJSON(m *Message) []byte

FormatJSON is a Formatter that converts a Message to json

func FormatWithColours

func FormatWithColours(m *Message) []byte

FormatWithColours is a Formatter that converts a Message to a colourful default format.

Example
AddWriter(os.Stdout, WithFormatter(FormatWithColours))

func Info

func Info(v ...any) string

Info logs inputs as info messages

func Infof

func Infof(msg string, v ...any) string

Infof logs a formatted message as an info message

func Panic

func Panic(v ...any)

Panic logs inputs as panics and panics

func Panicf

func Panicf(msg string, v ...any)

Panicf logs a formatted message as a panic and panics

func SetBufferSize

func SetBufferSize(size int)

SetBufferSize updates the message queue buffer size. Default size is 10,000 (10K)

func SetMeta

func SetMeta(data map[string]any)

SetMeta sets the global meta data attached to every log message

func Success

func Success(v ...any) string

Success logs inputs as success messages

func Successf

func Successf(msg string, v ...any) string

Successf logs a formatted message as a success message

func Wait

func Wait(opts ...WaitOption) bool

Wait for log messages to be processed. Returns true when all messages are processed. Optional WaitOption can be provided to set a timeout or context. Without options, Wait blocks until all pending messages are processed. Returns false if the timeout or context was cancelled before all messages were processed.

func Warn

func Warn(v ...any) string

Warn logs inputs as warnings

func Warnf

func Warnf(msg string, v ...any) string

Warnf logs a formatted message as a warning

Types

type Formatter

type Formatter func(m *Message) []byte

Formatter is a function that returns a formatted byte array representation of the given Message.

type Interfaces

type Interfaces []any

func (Interfaces) SSV

func (i Interfaces) SSV() string

SSV creates a space separated values string

func (Interfaces) Strings

func (i Interfaces) Strings() []string

type Logger

type Logger interface {
	Panic(v ...any)
	Panicf(msg string, v ...any)
	Error(v ...any) string
	Errorf(msg string, v ...any) string
	Warn(v ...any) string
	Warnf(msg string, v ...any) string
	Info(v ...any) string
	Infof(msg string, v ...any) string
	Debug(v ...any) string
	Debugf(msg string, v ...any) string
	Success(v ...any) string
	Successf(msg string, v ...any) string
	With(data Meta) Logger
}

Logger defines the methods available both by the logr package and Logr containing additional meta data.

func With

func With(data Meta) Logger

With metadata in the log messages

type Logr

type Logr struct {
	// contains filtered or unexported fields
}

Logr implements the Logger interface

func (*Logr) Debug

func (l *Logr) Debug(v ...any) string

Debug logs inputs as debug messages

func (*Logr) Debugf

func (l *Logr) Debugf(msg string, v ...any) string

Debugf logs a formatted message as a debug message

func (*Logr) Error

func (l *Logr) Error(v ...any) string

Error logs inputs as errors

func (*Logr) Errorf

func (l *Logr) Errorf(msg string, v ...any) string

Errorf logs a formatted message as an error

func (*Logr) Info

func (l *Logr) Info(v ...any) string

Info logs inputs as info messages

func (*Logr) Infof

func (l *Logr) Infof(msg string, v ...any) string

Infof logs a formatted message as an info message

func (*Logr) Panic

func (l *Logr) Panic(v ...any)

Panic logs inputs as panics and panics

func (*Logr) Panicf

func (l *Logr) Panicf(msg string, v ...any)

Panicf logs a formatted message as a panic and panics

func (*Logr) Success

func (l *Logr) Success(v ...any) string

Success logs inputs as success messages

func (*Logr) Successf

func (l *Logr) Successf(msg string, v ...any) string

Successf logs a formatted message as a success message

func (*Logr) Warn

func (l *Logr) Warn(v ...any) string

Warn logs inputs as warnings

func (*Logr) Warnf

func (l *Logr) Warnf(msg string, v ...any) string

Warnf logs a formatted message as a warning

func (*Logr) With

func (l *Logr) With(data Meta) Logger

With metadata in the log messages

type Message

type Message struct {
	Type Type     `json:"type"`
	Time string   `json:"time"`
	Code string   `json:"code"`
	Desc string   `json:"description"`
	Meta MetaData `json:"metadata,omitempty"`
	// contains filtered or unexported fields
}

Message used to send log message to logger goroutine

func (*Message) Reset

func (m *Message) Reset()

Reset the message object for later reuse

type Meta

type Meta map[string]any

func M

func M(key string, value any) Meta

func (Meta) Copy

func (m Meta) Copy() Meta

func (Meta) With

func (m Meta) With(key string, value any) Meta

type MetaData

type MetaData = Meta

type Type

type Type int

Type of log item

func IntToType

func IntToType(i int) Type

IntToType converts and integer to a log type/level

func LabelToType

func LabelToType(l string) Type

LabelToType converts a label to a log type/level Available labels are one of: none, panic, error, warning, info, success, debug, critical, monitor, verbose, all If the label is not found, it defaults to error and logs a message explaining the cause

Example
// available labels:
// 	none     // -
// 	panic    // Panic
// 	error    // Panic, Error
// 	warning  // Panic, Error, Warning
// 	info     // Panic, Error, Warning, Info
// 	success  // Panic, Error, Warning, Info, Success
// 	debug    // Panic, Error, Warning, Info, Success, and Debug
// 	critical // Panic, Error
// 	monitor  // Panic, Error, Warning
// 	verbose  // Panic, Error, Warning, Info, Success
// 	all      // Panic, Error, Warning, Info, Success, and Debug
AddWriter(os.Stdout, WithFormatter(FormatWithColours), WithFilter(LabelToType("success")))
// which is equivalent to
// 		AddWriter(os.Stdout, WithFilter(P | E | W | I | S)) // debug logs are filtered out

func RuneToType

func RuneToType(r rune) Type

RuneToType converts a code to a log type/level

func StringToType

func StringToType(s string) Type

StringToType converts a string of codes to a log type/level

Example
AddWriter(os.Stdout, WithFormatter(FormatWithColours), WithFilter(StringToType("PEW")))
// which is equivalent to
// 		AddWriter(os.Stdout, WithFilter(P | E | W))

func (Type) Colour

func (t Type) Colour() string

Colour returns the bash colour code for the log Type

func (Type) MarshalJSON

func (t Type) MarshalJSON() ([]byte, error)

MashalJSON implements json.Marshaller

func (Type) Rune

func (t Type) Rune() string

Rune returns the rune of the log Type

func (Type) String

func (t Type) String() string

String returns a descriptive string of the log Type

type WaitOption added in v2.0.3

type WaitOption func(*waitConfig)

WaitOption configures the behavior of Wait

func WithContext added in v2.0.3

func WithContext(ctx context.Context) WaitOption

WithContext sets a context for Wait to observe for cancellation

func WithTimeout added in v2.0.3

func WithTimeout(d time.Duration) WaitOption

WithTimeout sets a maximum duration for Wait to block

type WriterConfig

type WriterConfig struct {
	// contains filtered or unexported fields
}

type WriterConfigModifier

type WriterConfigModifier func(c WriterConfig) WriterConfig

func WithFilter

func WithFilter(f Type) WriterConfigModifier

WithFilter creates a WriterConfigModifier that sets a filter on the configuration for a Writer

Example
// None Type = iota      // None
// P    Type = 1 << iota // Panic
// E                     // Error
// W                     // Warning
// I                     // Info
// D                     // Debug
// S                     // Success
// Critical = P | E           // Panic and Error
// Monitor  = Critical | W    // Panic, Error, and Warning
// Verbose  = Monitor | I | S // Panic, Error, Warning, Info, and Success
// All      = Verbose | D     // Panic, Error, Warning, Info, Success, and Debug
AddWriter(os.Stdout, WithFilter(Monitor))
// which is equivalent to
// 		AddWriter(os.Stdout, WithFilter(P | E | W))

func WithFormatter

func WithFormatter(f Formatter) WriterConfigModifier

WithFormatter creates a WriterConfigModifier that defines how a log message is formatted on the configuration for a Writer. A custom formatter may be provided to convert the Message to any desired format before it is passed to the Writer.

Jump to

Keyboard shortcuts

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