logger

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Logger wraps zap.SugaredLogger for structured logging. Nimbus uses this logger in its own middleware and internals, and applications are free to replace it at startup (or in tests) via Set with their own zap.Logger.

Functions

func Channel

func Channel(name string) *zap.SugaredLogger

Channel returns a named channel logger. Falls back to the global logger.

func Configure

func Configure(cfg Config) error

Configure sets up the global logger from Config.

func Debug

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

Debug logs at debug level.

func Debugf

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

Debugf logs a formatted debug message.

func Error

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

Error logs at error level.

func Errorf

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

Errorf logs a formatted error message.

func Fatal

func Fatal(msg string, keysAndValues ...any)

Fatal logs and exits.

func Fatalf

func Fatalf(template string, args ...any)

Fatalf logs a formatted fatal message and exits.

func ForRequest

func ForRequest(c *http.Context) *zap.SugaredLogger

ForRequest returns a *zap.SugaredLogger scoped to the current HTTP request. It carries the request_id (set by the RequestID middleware) and any other fields previously attached via WithContext.

If no scoped logger exists yet, one is created from the global logger with the request_id field.

Usage:

log := logger.ForRequest(c)
log.Info("processing payment", "amount", 42.50)

func Info

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

Info logs at info level.

func Infof

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

Infof logs a formatted info message.

func Set

func Set(l *zap.Logger)

Set replaces the global logger (e.g. for testing or custom config).

func SetLevel

func SetLevel(level string)

SetLevel dynamically changes the log level.

func Sync

func Sync()

Sync flushes any buffered log entries.

func TeeCore

func TeeCore(extra zapcore.Core) error

TeeCore wraps the global logger core with an additional core (e.g. Telescope). Safe to call once; subsequent calls are ignored.

func Warn

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

Warn logs at warn level.

func Warnf

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

Warnf logs a formatted warning message.

func With

func With(keysAndValues ...any) *zap.SugaredLogger

With returns a logger with additional fields.

func WithContext

func WithContext(c *http.Context, l *zap.SugaredLogger)

WithContext attaches a request-scoped logger to the context. This is typically called by middleware to enrich the logger with additional fields.

logger.WithContext(c, logger.Log.With("user_id", userID))

func WithFields

func WithFields(fields map[string]any) *zap.SugaredLogger

WithFields returns a logger with structured fields.

Types

type ChannelConfig

type ChannelConfig struct {
	// Driver: "stdout", "stderr", "file" (default: stdout)
	Driver string

	// Path is required for "file" driver.
	Path string

	// Level overrides the global level for this channel.
	Level string

	// Format overrides the global format for this channel.
	Format string
}

ChannelConfig configures a single logging channel.

type Config

type Config struct {
	// Level: debug, info, warn, error, fatal (default: info)
	Level string

	// Format: "console" or "json" (default: console)
	Format string

	// Channels: named output channels.
	// Each channel can write to stdout, stderr, or a file path.
	Channels map[string]ChannelConfig
}

Config configures the logger.

type RotatingWriter

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

RotatingWriter is an io.Writer that automatically rotates log files when they exceed a configured maximum size. Old log files are kept up to a configurable count.

Usage:

w, _ := logger.NewRotatingWriter(logger.RotationConfig{
    Path:       "storage/logs/app.log",
    MaxSizeMB:  50,
    MaxBackups: 7,
})
// Use w as a zap WriteSyncer or io.Writer.

func NewRotatingWriter

func NewRotatingWriter(cfg RotationConfig) (*RotatingWriter, error)

NewRotatingWriter creates a new rotating log file writer.

func (*RotatingWriter) Close

func (w *RotatingWriter) Close() error

Close closes the underlying file.

func (*RotatingWriter) Sync

func (w *RotatingWriter) Sync() error

Sync flushes the file to disk (implements zapcore.WriteSyncer).

func (*RotatingWriter) Write

func (w *RotatingWriter) Write(p []byte) (n int, err error)

Write implements io.Writer. Rotates the file if the write would exceed MaxSizeMB.

type RotationConfig

type RotationConfig struct {
	// Path is the log file path (e.g. "storage/logs/app.log").
	Path string

	// MaxSizeMB is the maximum file size in megabytes before rotation (default: 100).
	MaxSizeMB int

	// MaxBackups is the number of rotated files to keep (default: 5).
	MaxBackups int
}

RotationConfig configures log file rotation.

Jump to

Keyboard shortcuts

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