log

package
v0.0.2-beta.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, attrs ...any)

Debug logs a debug-level message with optional structured attributes using the global logger.

func Debugf

func Debugf(format string, args ...any)

Debugf logs a debug-level message using printf-style formatting with the global logger.

func Error

func Error(msg string, attrs ...any)

Error logs an error-level message with optional structured attributes using the global logger.

func Errorf

func Errorf(format string, args ...any)

Errorf logs an error-level message using printf-style formatting with the global logger.

func Info

func Info(msg string, attrs ...any)

Info logs an info-level message with optional structured attributes using the global logger.

func Infof

func Infof(format string, args ...any)

Infof logs an info-level message using printf-style formatting with the global logger.

func Init

func Init(config *Config, attrs map[string]any)

Init initializes the global logger with the provided configuration and attributes. The configuration determines output destinations and formatting, while attrs are added to all log messages. This should be called once during application startup.

func NewSlogLogger

func NewSlogLogger(config *Config, options ...Option) *slog.Logger

NewSlogLogger creates a new structured logger using the standard library's slog interface. This provides compatibility with Go's standard structured logging while using zap as the backend.

func Sync

func Sync() error

Sync flushes any buffered log entries to their destinations. This should typically be called before application shutdown to ensure all logs are written.

func Warn

func Warn(msg string, attrs ...any)

Warn logs a warning-level message with optional structured attributes using the global logger.

func Warnf

func Warnf(format string, args ...any)

Warnf logs a warning-level message using printf-style formatting with the global logger.

Types

type AddCallerStatus

type AddCallerStatus int

AddCallerStatus represents the state of caller information in log entries.

const (
	// AddCallerStatusKeep maintains the current caller setting without changes.
	AddCallerStatusKeep AddCallerStatus = iota
	// AddCallerStatusEnable adds caller information to log entries.
	AddCallerStatusEnable
	// AddCallerStatusDisable removes caller information from log entries.
	AddCallerStatusDisable
)

type BaseLogger

type BaseLogger interface {
	// Debug logs a debug-level message with optional structured attributes.
	Debug(msg string, args ...any)
	// Info logs an info-level message with optional structured attributes.
	Info(msg string, args ...any)
	// Warn logs a warning-level message with optional structured attributes.
	Warn(msg string, args ...any)
	// Error logs an error-level message with optional structured attributes.
	Error(msg string, args ...any)
}

BaseLogger defines the core logging interface with structured logging capabilities. It provides the four standard log levels with support for structured attributes.

type Config

type Config struct {
	File    *FileConfig    `json:"file" yaml:"file"`
	Console *ConsoleConfig `json:"console" yaml:"console"`
	Level   string         `json:"level" yaml:"level"`
}

Config defines the complete logging configuration including output destinations, log level, and formatting options. It supports both console and file outputs.

func NewDefaultConfig

func NewDefaultConfig() *Config

NewDefaultConfig creates a new Config with sensible defaults. Returns a configuration with info level logging and no file output.

type ConsoleConfig

type ConsoleConfig struct {
	Disable bool `json:"disable" yaml:"disable"`
}

ConsoleConfig configures console logging output. When Disable is true, console logging is completely turned off.

type Field

type Field = zap.Field

Field is an alias for zap.Field providing structured logging fields.

func Any

func Any(key string, value interface{}) Field

Any creates a structured field with an arbitrary value. The value will be serialized using the best available method.

func Bool

func Bool(key string, value bool) Field

Bool creates a structured field with a boolean value.

func Duration

func Duration(key string, value time.Duration) Field

Duration creates a structured field with a duration value.

func Err

func Err(err error) Field

Err creates a structured field for error values using the standard "error" key.

func Float64

func Float64(key string, value float64) Field

Float64 creates a structured field with a floating-point value.

func Group

func Group(key string, values ...any) Field

Group creates a structured field that groups multiple values under a single key.

func Int

func Int(key string, value int) Field

Int creates a structured field with an integer value.

func Int64

func Int64(key string, value int64) Field

Int64 creates a structured field with a 64-bit integer value.

func String

func String(key, value string) Field

String creates a structured field with a string value.

func Time

func Time(key string, value time.Time) Field

Time creates a structured field with a time value.

func Uint64

func Uint64(key string, value uint64) Field

Uint64 creates a structured field with an unsigned 64-bit integer value.

type FileConfig

type FileConfig struct {
	FileName   string `json:"file_name" yaml:"file_name"`
	MaxSize    int    `json:"max_size" yaml:"max_size"`
	MaxBackups int    `json:"max_backups" yaml:"max_backups"`
	MaxAge     int    `json:"max_age" yaml:"max_age"`
}

FileConfig configures file-based logging output with rotation settings. It supports automatic log rotation based on size, age, and backup count.

type Logger

type Logger interface {
	BaseLogger
	// Debugf logs a debug-level message using printf-style formatting.
	Debugf(format string, args ...any)
	// Infof logs an info-level message using printf-style formatting.
	Infof(format string, args ...any)
	// Warnf logs a warning-level message using printf-style formatting.
	Warnf(format string, args ...any)
	// Errorf logs an error-level message using printf-style formatting.
	Errorf(format string, args ...any)
}

Logger extends BaseLogger with formatted logging capabilities. It combines structured logging with traditional printf-style formatting.

func With

func With(options ...Option) Logger

With creates a new logger instance with additional options applied to the global logger. The returned logger includes the caller skip adjustment for proper call site reporting.

type Option

type Option = func(*options)

Option is a function type for configuring logger options.

func AddCaller

func AddCaller() Option

AddCaller enables caller information in log entries. This includes file names and line numbers where the log call was made.

func AddCallerSkip

func AddCallerSkip(skip int) Option

AddCallerSkip adjusts the caller skip count for accurate call site reporting. This is useful when wrapping the logger to ensure the correct caller is reported.

func DisableCaller

func DisableCaller() Option

DisableCaller removes caller information from log entries. This can improve performance when caller information is not needed.

func WithAttrs

func WithAttrs(attrs map[string]any) Option

WithAttrs adds structured attributes to all log messages from this logger. These attributes provide consistent context across all log entries.

func WithName

func WithName(name string) Option

WithName sets the logger name for identification purposes. The name appears in log output to help distinguish between different loggers.

func WithStackAt

func WithStackAt(level zapcore.Level) Option

WithStackAt enables stack trace logging at the specified level and above. Stack traces help debug issues by showing the full call chain.

Jump to

Keyboard shortcuts

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