Documentation
¶
Overview ¶
Package logger provides a production-ready logging interface backed by zerolog. It supports configurable log levels, formats (JSON or console), and output writers. Configuration can be influenced by environment variables LOG_LEVEL, LOG_FORMAT, and LOG_FILE. The logger is thread-safe and follows best practices for structured logging in Go.
Index ¶
- Constants
- Variables
- func WithLevel(level string) option
- func WithLogFormat(format string) option
- func WithOutput(w io.Writer) option
- func WithTimeFormat(format string) option
- type Logger
- type ZeroLogger
- func (z *ZeroLogger) Debug(msg string, fields ...any)
- func (z *ZeroLogger) Error(err error, msg string, fields ...any)
- func (z *ZeroLogger) Fatal(err error, msg string, fields ...any)
- func (z *ZeroLogger) FromContext(ctx context.Context) Logger
- func (z *ZeroLogger) Info(msg string, fields ...any)
- func (z *ZeroLogger) Panic(err error, msg string, fields ...any)
- func (z *ZeroLogger) SetLevel(level string) error
- func (z *ZeroLogger) SetLogFormat(format string) error
- func (z *ZeroLogger) Trace(msg string, fields ...any)
- func (z *ZeroLogger) Warn(msg string, fields ...any)
- func (z *ZeroLogger) WithContext(ctx context.Context) context.Context
- func (z *ZeroLogger) WithFields(fields ...any) Logger
Constants ¶
const ( // LogFormatJSON specifies JSON output format. LogFormatJSON = "json" // LogFormatConsole specifies pretty-printed console output format. LogFormatConsole = "console" // LevelTrace sets the logger to trace level. LevelTrace = "trace" // LevelDebug sets the logger to debug level. LevelDebug = "debug" // LevelInfo sets the logger to info level. LevelInfo = "info" // LevelWarn sets the logger to warn level. LevelWarn = "warn" // LevelError sets the logger to error level. LevelError = "error" // LevelFatal sets the logger to fatal level. LevelFatal = "fatal" // LevelPanic sets the logger to panic level. LevelPanic = "panic" // LevelNo sets the logger to no logging. LevelNo = "no" // LevelDisabled disables the logger entirely. LevelDisabled = "disabled" )
Variables ¶
var ErrInvalidLogFormat = errors.New("invalid log format")
ErrInvalidLogFormat is returned when an invalid log format is provided.
var ErrInvalidLogLevel = errors.New("invalid log level")
ErrInvalidLogLevel is returned when an invalid log level is provided.
Functions ¶
func WithLogFormat ¶
func WithLogFormat(format string) option
WithLogFormat sets the initial log format (json or console).
func WithTimeFormat ¶
func WithTimeFormat(format string) option
WithTimeFormat sets the time format for log timestamps.
Types ¶
type Logger ¶
type Logger interface {
Trace(msg string, fields ...any)
Debug(msg string, fields ...any)
Info(msg string, fields ...any)
Warn(msg string, fields ...any)
Error(err error, msg string, fields ...any)
Fatal(err error, msg string, fields ...any)
Panic(err error, msg string, fields ...any)
WithFields(fields ...any) Logger
WithContext(ctx context.Context) context.Context
FromContext(ctx context.Context) Logger
SetLevel(level string) error
SetLogFormat(format string) error
}
Logger defines the interface for logging operations.
type ZeroLogger ¶
type ZeroLogger struct {
// contains filtered or unexported fields
}
ZeroLogger is the zerolog implementation of the Logger interface.
func (*ZeroLogger) Debug ¶
func (z *ZeroLogger) Debug(msg string, fields ...any)
Debug logs a debug-level message with optional fields.
func (*ZeroLogger) Error ¶
func (z *ZeroLogger) Error(err error, msg string, fields ...any)
Error logs an error-level message with an error and optional fields.
func (*ZeroLogger) Fatal ¶
func (z *ZeroLogger) Fatal(err error, msg string, fields ...any)
Fatal logs a fatal-level message with an error and optional fields, then exits the program.
func (*ZeroLogger) FromContext ¶
func (z *ZeroLogger) FromContext(ctx context.Context) Logger
FromContext retrieves the logger from the context. If none is found, returns a logger based on the current instance.
func (*ZeroLogger) Info ¶
func (z *ZeroLogger) Info(msg string, fields ...any)
Info logs an info-level message with optional fields.
func (*ZeroLogger) Panic ¶
func (z *ZeroLogger) Panic(err error, msg string, fields ...any)
Panic logs a panic-level message with an error and optional fields, then panics.
func (*ZeroLogger) SetLevel ¶
func (z *ZeroLogger) SetLevel(level string) error
SetLevel sets the minimum log level for the logger.
func (*ZeroLogger) SetLogFormat ¶
func (z *ZeroLogger) SetLogFormat(format string) error
SetLogFormat sets the output format (json or console) for the logger.
func (*ZeroLogger) Trace ¶
func (z *ZeroLogger) Trace(msg string, fields ...any)
Trace logs a trace-level message with optional fields.
func (*ZeroLogger) Warn ¶
func (z *ZeroLogger) Warn(msg string, fields ...any)
Warn logs a warn-level message with optional fields.
func (*ZeroLogger) WithContext ¶
func (z *ZeroLogger) WithContext(ctx context.Context) context.Context
WithContext attaches the logger to the provided context.
func (*ZeroLogger) WithFields ¶
func (z *ZeroLogger) WithFields(fields ...any) Logger
WithFields returns a new logger with additional structured fields.