logger

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2025 License: MIT Imports: 7 Imported by: 1

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

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

View Source
var ErrInvalidLogFormat = errors.New("invalid log format")

ErrInvalidLogFormat is returned when an invalid log format is provided.

View Source
var ErrInvalidLogLevel = errors.New("invalid log level")

ErrInvalidLogLevel is returned when an invalid log level is provided.

Functions

func WithLevel

func WithLevel(level string) option

WithLevel sets the initial log level.

func WithLogFormat

func WithLogFormat(format string) option

WithLogFormat sets the initial log format (json or console).

func WithOutput

func WithOutput(w io.Writer) option

WithOutput sets the initial output writer.

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.

func New

func New(options ...option) Logger

New creates a new logger with the given options. Options override environment variables.

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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