Documentation
¶
Overview ¶
Package logze implements a zerolog wrapper providing a convenient and short interface for structural logging.
Index ¶
- Constants
- Variables
- func Debug(msg string, fields ...any)
- func Debugf(msg string, args ...any)
- func Err(err error, fields ...any)
- func Error(err error, msg string, fields ...any)
- func Errorf(err error, msg string, args ...any)
- func Fatal(v ...any)
- func Fatalf(format string, args ...any)
- func Fatalln(v ...any)
- func Info(msg string, fields ...any)
- func Infof(msg string, args ...any)
- func Init(cfg Config, fields ...any)
- func Panic(v ...any)
- func Panicf(format string, args ...any)
- func Panicln(v ...any)
- func Print(v ...any)
- func Printf(format string, args ...any)
- func Println(v ...any)
- func Raw() *zerolog.Logger
- func SetLoggerForDefault(l Logger, fields ...any)
- func Trace(msg string, fields ...any)
- func Tracef(msg string, args ...any)
- func Update(cfg Config, fields ...any)
- func Warn(msg string, fields ...any)
- func Warnf(msg string, args ...any)
- func Write(p []byte) (n int, err error)
- type Config
- func (c Config) WithConsole() Config
- func (c Config) WithConsoleJSON() Config
- func (c Config) WithConsoleNoColor() Config
- func (c Config) WithDiodeAlert(foo func(int)) Config
- func (c Config) WithDiodePollingInterval(interval time.Duration) Config
- func (c Config) WithDiodeSize(size int) Config
- func (c Config) WithDisabledDiode() Config
- func (c Config) WithErrorCounter(ec ErrorCounter) Config
- func (c Config) WithHook(hook zerolog.Hook) Config
- func (c Config) WithLevel(level string) Config
- func (c Config) WithSimpleErrorCounter(name string) Config
- func (c Config) WithStackTrace() Config
- func (c Config) WithTimeFieldFormat(format string) Config
- func (c Config) WithToIgnore(toIgnore ...string) Config
- func (c Config) WithWriter(w io.Writer) Config
- type ErrorCounter
- type Logger
- func New(cfg Config, fields ...any) Logger
- func NewDefault(fields ...any) Logger
- func NewFromZerolog(l zerolog.Logger) Logger
- func Nop() Logger
- func With(fields ...any) Logger
- func WithErrorCounter(ec ErrorCounter) Logger
- func WithFields(fields ...any) Logger
- func WithLevel(level string) Logger
- func WithSimpleErrorCounter(name string) Logger
- func WithToIgnore(toIgnore ...string) Logger
- func (l Logger) Debug(msg string, fields ...any)
- func (l Logger) Debugf(msg string, args ...any)
- func (l Logger) Err(err error, fields ...any)
- func (l Logger) Error(err error, msg string, fields ...any)
- func (l Logger) Errorf(err error, msg string, args ...any)
- func (l Logger) Fatal(v ...any)
- func (l Logger) Fatalf(format string, args ...any)
- func (l Logger) Fatalln(v ...any)
- func (l Logger) GetErrorCounter() ErrorCounter
- func (l Logger) Info(msg string, fields ...any)
- func (l Logger) Infof(msg string, args ...any)
- func (l Logger) NotInited() bool
- func (l Logger) Panic(v ...any)
- func (l Logger) Panicf(format string, args ...any)
- func (l Logger) Panicln(v ...any)
- func (l Logger) Print(v ...any)
- func (l Logger) Printf(format string, args ...any)
- func (l Logger) Println(v ...any)
- func (l Logger) Raw() *zerolog.Logger
- func (l Logger) Trace(msg string, fields ...any)
- func (l Logger) Tracef(msg string, args ...any)
- func (l *Logger) Update(cfg Config, fields ...any)
- func (l Logger) Warn(msg string, fields ...any)
- func (l Logger) Warnf(msg string, args ...any)
- func (l Logger) With(fields ...any) Logger
- func (l Logger) WithErrorCounter(ec ErrorCounter) Logger
- func (l Logger) WithFields(fields ...any) Logger
- func (l Logger) WithLevel(level string) Logger
- func (l Logger) WithSimpleErrorCounter(name string) Logger
- func (l Logger) WithStack(stackTrace bool) Logger
- func (l Logger) WithToIgnore(toIgnore ...string) Logger
- func (l Logger) Write(p []byte) (n int, err error)
- type SLogger
Constants ¶
const ( TraceLevel = "trace" DebugLevel = "debug" InfoLevel = "info" WarnLevel = "warn" ErrorLevel = "error" FatalLevel = "fatal" DisabledLevel = "disabled" )
Enumerating string representations of all supported levels.
const DefaultDiodeSize = 1000
DefaultDiodeSize is a default size of a diode writer. Logs will be lost if there will be more logs than that value in a small period of time (of time less that Config.DiodePollingInterval).
Variables ¶
var Levels = []string{ TraceLevel, DebugLevel, InfoLevel, WarnLevel, ErrorLevel, FatalLevel, DisabledLevel, }
Levels is a list of all supported levels in string format.
var LevelsAny = []any{ TraceLevel, DebugLevel, InfoLevel, WarnLevel, ErrorLevel, FatalLevel, DisabledLevel, }
LevelsAny is a list of all supported levels in any format.
var Log = NewDefault()
Log is a global logger.
Functions ¶
func Debugf ¶
Debugf logs a formatted message in debug level adding provided fields after formatting args using a global logger.
func Error ¶
Error logs a provided error and message in error level adding provided fields using a global logger.
func Errorf ¶
Errorf logs a provided error and formatted message in error level adding provided fields after formatting args using a global logger.
func Fatal ¶
func Fatal(v ...any)
Fatal logs a message in fatal level using fmt.Sprint to interpret args sing a global logger, then calls os.Exit(1).
func Fatalf ¶
Fatalf logs a formatted message in fatal level using a global logger, then calls os.Exit(1).
func Fatalln ¶
func Fatalln(v ...any)
Fatalln logs a message in fatal level using fmt.Sprintln to interpret args using a global logger, then calls os.Exit(1).
func Infof ¶
Infof logs a formatted message in info level adding provided fields after formatting args using a global logger.
func Init ¶
Init calls New function and assigns the result to global Log variable. It also calls SetLoggerForDefault with this new logger.
func Panic ¶
func Panic(v ...any)
Panic logs a message in fatal level using fmt.Sprint to interpret args using a global logger, then calls panic().
func Panicf ¶
Panicf logs a formatted message in fatal level using a global logger, then calls panic().
func Panicln ¶
func Panicln(v ...any)
Panicln logs a message in fatal level using fmt.Sprintln to interpret args using a global logger, then calls panic().
func Print ¶
func Print(v ...any)
Print logs a message without level using fmt.Sprint to interpret args using a global logger.
func Println ¶
func Println(v ...any)
Println writes a message without level using fmt.Sprintln to interpret args using a global logger.
func Raw ¶
Raw returns Logger's underlying zerolog.Logger from global logger.
func SetLoggerForDefault ¶
SetLoggerForDefault sets priovded Logger with (key, value) pairs as writer for default Go logger and also calls stdlog.SetFlags(0).
func Trace ¶
Trace logs a message in trace level adding provided fields and information about method caller using a global logger.
func Tracef ¶
Tracef logs a formatted message in trace level adding provided fields after formatting args and information about method caller using a global logger.
func Update ¶ added in v1.3.0
Update calls Logger.Update method for global Log. It also calls SetLoggerForDefault with this new logger. It is not safe for concurrent use!
Types ¶
type Config ¶
type Config struct { // Writers is a list of writers where logger will log its data. // Default value is [io.Discard]. Writers []io.Writer // Level is a log level in string format. Supported levels are: // trace, debug, info, warn, error, fatal, disabled. Level string // TimeFieldFormat is a format for time field. Default value is RFC3339. // You can use values from zerolog like [zerolog.TimeFormatUnix], [zerolog.TimeFormatUnixMs], // [zerolog.TimeFormatUnixMicro], [zerolog.TimeFormatUnixNano], [time.RFC3339], [time.RFC3339Nano] or custom. // UNIX Time is faster and smaller than most timestamps TimeFieldFormat string // Hook is a zerolog.Hook that will be used when creating logger. // Default value is nil. Hook zerolog.Hook // ToIgnore is a list of messages that will be ignored. // Default value is nil. ToIgnore []string // ErrorCounter is a counter of logged errors. Use WithSimpleErrorCounter method to use a simple counter. // Default value is nil. ErrorCounter ErrorCounter // DiodeSize is a size of a diode writer. Logs will be lost if there will be more logs than that value // in a small period of time (of time less that Config.DiodePollingInterval). // Default value is 1000. DiodeSize int // DiodePollingInterval is a time after which diode writer will flush its buffer. // Default value is 100ms. DiodePollingInterval time.Duration // DiodeAlertFunc is a function that will be called when diode writer will flush its buffer. // Default value is a function that logs a message in warn level. DiodeAlertFunc func(int) // DisableDiode if true, will disable diode writer. // Default value is false. DisableDiode bool // StackTrace if true, will enable stack trace for Error and Errorf methods. // Default value is false. StackTrace bool }
Config is using for initializing Logger. You should use NewConfig and With* methods instead of creating a Config struct directly.
func C ¶ added in v1.5.0
C is a shortcut for NewConfig that returns Config with provided list of io.Writer, where Logger should logs its data.
func NewConfig ¶
NewConfig returns Config with provided list of io.Writer, where Logger should logs its data.
func (Config) WithConsole ¶
WithConsole returns Config with a configurated output to stderr in a pretty console format with colors. This format may significantly slow down logging in an application compared to a default JSON format.
func (Config) WithConsoleJSON ¶
WithConsoleJSON returns Config with a configurated output to stderr in a JSON format.
func (Config) WithConsoleNoColor ¶
WithConsoleNoColor returns Config a with configurated output to stderr in a pretty console format without colors. This format may significantly slow down logging in an application compared to a default JSON format.
func (Config) WithDiodeAlert ¶
WithDiodeAlert returns Config with provided diode alert func.
func (Config) WithDiodePollingInterval ¶
WithDiodePollingInterval returns Config with enabled diode polling with provided interval.
func (Config) WithDiodeSize ¶
WithDiodeSize returns Config with a new size of diode writer.
func (Config) WithDisabledDiode ¶
WithDisabledDiode returns Config with disabled diode writer.
func (Config) WithErrorCounter ¶
func (c Config) WithErrorCounter(ec ErrorCounter) Config
WithErrorCounter returns Config with the provided ErrorCounter.
func (Config) WithHook ¶
WithHook returns Config with initialized zerolog.Hook provided as argument.
func (Config) WithLevel ¶
WithLevel returns Config with initialized level (in string format) provided as argument.
func (Config) WithSimpleErrorCounter ¶
WithErrorCounter returns Config with a simple ErrorCounter inited with the provided name.
func (Config) WithStackTrace ¶
WithStackTrace returns Config with an enabled stack trace for Error and Errorf methods.
func (Config) WithTimeFieldFormat ¶ added in v1.5.0
WithTimeFieldFormat returns Config with a new format for time field. TimeFieldFormat is a format for time field. Default value is RFC3339. You can use values from zerolog like zerolog.TimeFormatUnix, zerolog.TimeFormatUnixMs, zerolog.TimeFormatUnixMicro, zerolog.TimeFormatUnixNano, time.RFC3339, time.RFC3339Nano or custom. UNIX Time is faster and smaller than most timestamps
func (Config) WithToIgnore ¶ added in v1.2.0
WithToIgnore returns Config with a list of messages that will be ignored.
type ErrorCounter ¶
type ErrorCounter interface {
Inc(err ...error)
}
ErrorCounter provides an interface to count logged errors. Use WithSimpleErrorCounter in Config creation to use a simple error counter and GetErrorCounter method in Logger to retrieve it.
func GetErrorCounter ¶
func GetErrorCounter() ErrorCounter
GetErrorCounter returns Logger's underlying ErrorCounter from global logger.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger represents an initialized logger. Default value behaves as default zerolog.Logger.
func New ¶
New returns a new Logger with provided config and fields.
- Default output is io.Discard, so you should provide at least one io.Writer in Config when creating a logger.
- Default level is info.
- Fields should be passed as (key, value) pairs, its will be applied to all messages.
For example, if you use Logger like that:
lg := New(C().WithConsoleJSON(), "foo", "bar") lg.Info("some message", "key", "value") lg.Error(errors.New("some error"), "cannot handle")
You will have output:
{"level":"info","time":"2023-11-20T18:48:14+03:00","message":"some message","foo":"bar","key":"value"} {"level":"error","time":"2023-11-20T18:48:14+03:00","error":"some error","message":"cannot handle","foo":"bar"}
func NewDefault ¶
NewDefault returns a new Logger with logging to stderr.
func NewFromZerolog ¶ added in v1.5.0
NewFromZerolog returns a new Logger based on provided zerolog.Logger.
func WithErrorCounter ¶
func WithErrorCounter(ec ErrorCounter) Logger
WithErrorCounter returns Logger with the provided ErrorCounter, based on a global logger.
func WithFields ¶
WithFields returns Logger with applied fields, provided as (key, value) pairs, based on a global logger.
func WithSimpleErrorCounter ¶
WithSimpleErrorCounter returns Logger with a simple ErrorCounter inited with the provided name, based on a global logger.
func WithToIgnore ¶ added in v1.5.0
WithToIgnore returns Logger with the provided list of messages to ignore based on a global logger.
func (Logger) Debugf ¶
Debugf logs a formatted message in debug level adding provided fields after formatting args.
func (Logger) Error ¶
Error logs a provided error and message in error level adding provided fields.
func (Logger) Errorf ¶
Errorf logs a provided error and formatted message in error level adding provided fields after formatting args.
func (Logger) Fatal ¶
Fatal logs a message in fatal level using fmt.Sprint to interpret args, then calls os.Exit(1).
func (Logger) Fatalln ¶
Fatalln logs a message in fatal level using fmt.Sprintln to interpret args, then calls os.Exit(1).
func (Logger) GetErrorCounter ¶
func (l Logger) GetErrorCounter() ErrorCounter
GetErrorCounter returns Logger's underlying ErrorCounter.
func (Logger) Infof ¶
Infof logs a formatted message in info level adding provided fields after formatting args.
func (Logger) NotInited ¶ added in v1.1.0
NotInited returns true if Logger is not inited (struct with default values).
func (Logger) Panic ¶
Panic logs a message in fatal level using fmt.Sprint to interpret args, then calls panic().
func (Logger) Panicln ¶
Panicln logs a message in fatal level using fmt.Sprintln to interpret args, then calls panic().
func (Logger) Println ¶
Println writes a message without level using fmt.Sprintln to interpret args.
func (Logger) Raw ¶
Raw returns Logger's underlying zerolog.Logger.
func (Logger) Trace ¶
Trace logs a message in trace level adding provided fields and information about method caller.
func (Logger) Tracef ¶
Tracef logs a formatted message in trace level adding provided fields after formatting args and information about method caller.
func (*Logger) Update ¶ added in v1.3.0
Update replaces underlying logger with a new one created using provided config and fields. It is not safe for concurrent use!
func (Logger) Warnf ¶
Warnf logs a formatted message in warn level adding provided fields after formatting args.
func (Logger) With ¶ added in v1.3.0
With is a shortcut for Logger.WithFields.
func (Logger) WithErrorCounter ¶
func (l Logger) WithErrorCounter(ec ErrorCounter) Logger
WithErrorCounter returns Logger with the provided ErrorCounter.
func (Logger) WithFields ¶
WithFields returns Logger with applied fields to all messages, provided as (key, value) pairs.
func (Logger) WithSimpleErrorCounter ¶
WithSimpleErrorCounter returns Logger with a simple ErrorCounter inited with the provided name.
func (Logger) WithToIgnore ¶ added in v1.5.0
WithToIgnore returns Logger with the provided list of messages to ignore.
type SLogger ¶ added in v1.1.0
type SLogger struct {
Logger
}
SLogger is a wrapper for Logger. It provides the same methods as Logger but with another Error interface (slog style).
func ConvertToS ¶ added in v1.1.0
func S ¶ added in v1.5.0
S is a shortcut to ConvertToS that converts Logger to SLogger.