Documentation
¶
Overview ¶
Example ¶
Example demonstrates basic usage of the logger package
// Initialize global logger with functional options
Init(
WithLevel(LevelInfo),
WithFormat(FormatJSON),
)
// Basic logging
Info("application started")
Debug("debug message", zap.String("key", "value"))
// Print functions (fmt replacement)
Print("simple message")
Println("message with newline")
Printf("formatted message: %s", "value")
// Level-specific printf functions
Debugf("debug: %d", 42)
Infof("info: %s", "information")
Warnf("warning: %s", "warning message")
Errorf("error: %s", "error message")
// Sugar logger
sugar := Sugar()
sugar.Infow("user action",
"action", "login",
"user_id", "123",
)
// Context with tracing
ctx := context.WithValue(context.Background(), traceIDKey, "12345")
ctxWithValue := context.WithValue(ctx, spanIDKey, "67890")
Trace(ctx).Info("processing request")
Trace(ctx).Warn("warning with trace", zap.Int("count", 5))
// Sugar with tracing
sugar.Trace(ctxWithValue).Infow("processing with trace",
"request_id", "req-123",
)
Index ¶
- Constants
- func Debug(msg string, fields ...zap.Field)
- func Debugf(format string, args ...interface{})
- func Error(msg string, fields ...zap.Field)
- func Errorf(format string, args ...interface{})
- func ExtractTraceFields(ctx context.Context) []zap.Field
- func Fatal(msg string, fields ...zap.Field)
- func Fatalf(format string, args ...interface{})
- func Info(msg string, fields ...zap.Field)
- func Infof(format string, args ...interface{})
- func Init(opts ...Option)
- func Panic(msg string, fields ...zap.Field)
- func Print(args ...interface{})
- func Printf(format string, args ...interface{})
- func Println(args ...interface{})
- func Warn(msg string, fields ...zap.Field)
- func Warnf(format string, args ...interface{})
- type Config
- type Logger
- func (l *Logger) Debugf(format string, args ...interface{})
- func (l *Logger) Errorf(format string, args ...interface{})
- func (l *Logger) Fatalf(format string, args ...interface{})
- func (l *Logger) Infof(format string, args ...interface{})
- func (l *Logger) Print(args ...interface{})
- func (l *Logger) Printf(format string, args ...interface{})
- func (l *Logger) Println(args ...interface{})
- func (l *Logger) Sugar() *SugaredLogger
- func (l *Logger) Trace(ctx context.Context) *Logger
- func (l *Logger) Warnf(format string, args ...interface{})
- type Option
- func WithCompress(compress bool) Option
- func WithConsoleOutput() Option
- func WithDebugLevel() Option
- func WithDev(isDev bool) Option
- func WithErrorLevel() Option
- func WithFileOutput(filePath string) Option
- func WithFilePath(filePath string) Option
- func WithFormat(format string) Option
- func WithInfoLevel() Option
- func WithLevel(level string) Option
- func WithMaxAge(maxAge int) Option
- func WithMaxBackups(maxBackups int) Option
- func WithMaxSize(maxSize int) Option
- func WithProductionDefaults() Option
- func WithTextOutput() Option
- func WithWarnLevel() Option
- type SugaredLogger
Examples ¶
Constants ¶
const ( FormatJSON = "json" FormatText = "text" FormatFile = "file" )
Log format constants
const ( LevelDebug = "debug" LevelInfo = "info" LevelWarn = "warn" LevelError = "error" )
Log level constants
Variables ¶
This section is empty.
Functions ¶
func ExtractTraceFields ¶
ExtractTraceFields extracts tracing information from context for both Datadog and OpenTelemetry.
Types ¶
type Config ¶
type Config struct {
Level string // debug, info, warn, error (default: info)
Format string // json, text, file (default: json)
FilePath string // file or directory path (only for file format)
MaxSize int // max size in MB before rotation (default: 100)
MaxAge int // max days to retain old logs (default: 30)
MaxBackups int // max number of old log files (default: 3)
Compress bool // compress rotated files (default: false)
IsDev *bool // use zap's development config for human-readable output (default: false)
AtomicLevel zap.AtomicLevel // atomic level for dynamic level changes
}
Config defines the logger configuration.
func (Config) IsDevelopment ¶
type Logger ¶
Logger embeds zap.Logger to inherit all standard methods.
func GetLogger ¶
func GetLogger() *Logger
GetLogger returns the global logger instance. It panics if the logger is not initialized.
func Trace ¶
Trace extracts tracing fields from context using the global logger.
Example ¶
ExampleTrace demonstrates tracing integration
// Context with OpenTelemetry-style trace
ctx := context.Background()
// In real usage, this would come from OpenTelemetry or Datadog
// Context with custom trace
ctx = context.WithValue(ctx, traceIDKey, "custom-trace-789")
ctx = context.WithValue(ctx, spanIDKey, "custom-span-012")
// All these will include trace information
Trace(ctx).Info("processing request")
Trace(ctx).Debug("debug with trace")
Trace(ctx).Error("error with trace")
func (*Logger) Print ¶
func (l *Logger) Print(args ...interface{})
Print functions (fmt replacement)
func (*Logger) Sugar ¶
func (l *Logger) Sugar() *SugaredLogger
Sugar returns a SugaredLogger with tracing support.
type Option ¶
type Option func(*Config)
Option is a functional option for configuring the logger.
func WithCompress ¶
WithCompress enables compression of rotated files.
func WithConsoleOutput ¶
func WithConsoleOutput() Option
WithConsoleOutput configures console output (JSON format).
func WithFileOutput ¶
WithFileOutput configures file output with rotation.
func WithFilePath ¶
WithFilePath sets the file path for file output.
func WithMaxAge ¶
WithMaxAge sets the maximum age in days to retain old logs.
func WithMaxBackups ¶
WithMaxBackups sets the maximum number of old log files to retain.
func WithMaxSize ¶
WithMaxSize sets the maximum size in MB before rotation.
func WithProductionDefaults ¶
func WithProductionDefaults() Option
WithProductionDefaults sets production-ready defaults.
func WithTextOutput ¶
func WithTextOutput() Option
WithTextOutput configures text output to console.
type SugaredLogger ¶
type SugaredLogger struct {
*zap.SugaredLogger
// contains filtered or unexported fields
}
SugaredLogger embeds zap.SugaredLogger with tracing support.
func (*SugaredLogger) Trace ¶
func (sl *SugaredLogger) Trace(ctx context.Context) *SugaredLogger
Trace extracts tracing fields from context and returns a sugared logger with those fields.