Documentation
¶
Index ¶
- Variables
- func Channel(name string) *zap.SugaredLogger
- func Configure(cfg Config) error
- func Debug(msg string, keysAndValues ...any)
- func Debugf(template string, args ...any)
- func Error(msg string, keysAndValues ...any)
- func Errorf(template string, args ...any)
- func Fatal(msg string, keysAndValues ...any)
- func Fatalf(template string, args ...any)
- func ForRequest(c *http.Context) *zap.SugaredLogger
- func Info(msg string, keysAndValues ...any)
- func Infof(template string, args ...any)
- func Set(l *zap.Logger)
- func SetLevel(level string)
- func Sync()
- func TeeCore(extra zapcore.Core) error
- func Warn(msg string, keysAndValues ...any)
- func Warnf(template string, args ...any)
- func With(keysAndValues ...any) *zap.SugaredLogger
- func WithContext(c *http.Context, l *zap.SugaredLogger)
- func WithFields(fields map[string]any) *zap.SugaredLogger
- type ChannelConfig
- type Config
- type RotatingWriter
- type RotationConfig
Constants ¶
This section is empty.
Variables ¶
var Log *zap.SugaredLogger
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 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 TeeCore ¶
TeeCore wraps the global logger core with an additional core (e.g. Telescope). Safe to call once; subsequent calls are ignored.
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).
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.