logger

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LevelError = iota*10 + 10
	LevelWarning
	LevelInfo
	LevelDebug
	LevelTrace

	DefaultLogLevel = math.MaxInt
)

Variables

This section is empty.

Functions

func ToCtx

func ToCtx(ctx context.Context, logger Logger) context.Context

ToCtx stores logger in the context, it can be extracted with FromCtx or FromCtxOrNop.

Types

type Adapter

type Adapter interface {
	// Log logs a message with the provided level, message, optional error, and an
	// arbitrary number of fields.
	Log(level int, msg string, err error, fs ...fields.Field)

	// WithFields returns a logger adapter with the given fields attached to it.
	WithFields(fs ...fields.Field) Adapter

	// WithName returns a logger adapter with the given name attached to it.
	WithName(name string) Adapter

	// WithStackTrace returns a logger adapter with the stack trace attached to it.
	WithStackTrace(trace string) Adapter

	// Flush all logs to the output. It is a no-op for non-buffered loggers.
	//
	// It is application's responsibility to call this method before it exits.
	Flush() error
}

Adapter is an interface that allows to encapsulate any logger inside Logger.

type CtxKey

type CtxKey struct{}

type Logger

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

func FromCtx

func FromCtx(ctx context.Context) (Logger, bool)

FromCtx attempts to extract logger from the context. In case logger is not present, or it is not of the correct type, it returns empty logger and false.

func FromCtxOrNop

func FromCtxOrNop(ctx context.Context) Logger

FromCtxOrNop extracts logger from the context. In case logger is not present, or it is not of the correct type, it returns a nop logger.

func New

func New(adapter Adapter, maxLevel int) Logger

New creates new Logger. Consult Logger docs for more info about parameters.

func NewNop

func NewNop() Logger

NewNop creates a new logger that does nothing.

func (Logger) Debug

func (l Logger) Debug(msg string, fs ...fields.Field)

Debug logs a message with the LevelDebug log-level.

Use Debug to log detailed information that is useful during development and debugging.

func (Logger) DebugE

func (l Logger) DebugE(msg string, err error, fs ...fields.Field)

DebugE logs a message with the LevelDebug log-level and the provided error.

Use DebugE to log detailed information that is useful during development and debugging along with an error.

func (Logger) Error

func (l Logger) Error(msg string, err error, fs ...fields.Field)

Error logs a message with the LevelError log-level.

Use Error to log any unrecoverable error, such as a database query failure where the application cannot continue. It's OK to pass nil as the error. To attach a stack trace, use Logger.WithStackTrace.

func (Logger) Flush

func (l Logger) Flush() error

Flush flushes the underlying logger adapter, allowing buffered adapters to write logs to the output.

It is the application's responsibility to call Logger.Flush before exiting.

func (Logger) Info

func (l Logger) Info(msg string, fs ...fields.Field)

Info logs a message with the LevelInfo log-level.

Use Info to log informational messages that highlight the progress of the application.

func (Logger) InfoE

func (l Logger) InfoE(msg string, err error, fs ...fields.Field)

InfoE logs a message with the LevelInfo log-level and the provided error.

Use InfoE to log informational messages that highlight the progress of the application along with an error.

func (Logger) IsNop

func (l Logger) IsNop() bool

IsNop returns true if the logger's adapter is a NopAdapter.

func (Logger) IsZero

func (l Logger) IsZero() bool

IsZero returns true if the logger is a zero-value structure.

func (Logger) Log

func (l Logger) Log(level int, msg string, err error, fs ...fields.Field)

Log logs a message with given log-level, optional error and fields.

func (Logger) Trace

func (l Logger) Trace(msg string, fs ...fields.Field)

Trace logs a message with the LevelTrace log-level.

Use Trace to log very detailed information, typically of interest only when diagnosing problems.

func (Logger) TraceE

func (l Logger) TraceE(msg string, err error, fs ...fields.Field)

TraceE logs a message with the LevelTrace log-level and the provided error.

Use TraceE to log very detailed information, typically of interest only when diagnosing problems along with an error.

func (Logger) Warning

func (l Logger) Warning(msg string, fs ...fields.Field)

Warning logs a message with the LevelWarning log-level.

Use WarningE to log any recoverable error, such as an error during a remote API call where the service did not respond and the application will retry.

func (Logger) WarningE

func (l Logger) WarningE(msg string, err error, fs ...fields.Field)

WarningE logs a message with the LevelWarning log-level and the provided error.

Use WarningE to log any recoverable error, such as an error during a remote API call where the service did not respond and the application will retry.

func (Logger) WithFields

func (l Logger) WithFields(fs ...fields.Field) Logger

WithFields returns a new child-logger with the given fields attached to it.

func (Logger) WithName

func (l Logger) WithName(name string) Logger

WithName returns a new child-logger with the given name assigned to it.

func (Logger) WithStackTrace

func (l Logger) WithStackTrace(_ uint) Logger

WithStackTrace returns a new child-logger with the stack trace attached to it.

type NopAdapter

type NopAdapter struct{}

NopAdapter is a no-op logger adapter, it does nothing even if any of its methods called.

func (NopAdapter) Flush

func (NopAdapter) Flush() error

func (NopAdapter) Log

func (NopAdapter) Log(int, string, error, ...fields.Field)

func (NopAdapter) WithFields

func (a NopAdapter) WithFields(...fields.Field) Adapter

func (NopAdapter) WithName

func (a NopAdapter) WithName(string) Adapter

func (NopAdapter) WithStackTrace

func (a NopAdapter) WithStackTrace(string) Adapter

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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