Documentation
¶
Index ¶
- type Config
- type Field
- type Level
- type Logger
- func (l *Logger) Debug(traceId string, msg string, metadata any, fields ...Field)
- func (l *Logger) Error(traceId string, msg string, metadata any, fields ...Field)
- func (l *Logger) Fatal(traceId string, msg string, metadata any, fields ...Field)
- func (l *Logger) Info(traceId string, msg string, metadata any, fields ...Field)
- func (l *Logger) Sync() error
- func (l *Logger) Warn(traceId string, msg string, metadata any, fields ...Field)
- func (l *Logger) With(fields ...Field) *Logger
- type OutputType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Service is the name of the service (required).
Service string
// Env is the environment: dev, development, staging, prod, or production (required).
Env string
// Level is the minimum log level (required).
// Use log.DebugLevel, log.InfoLevel, log.WarnLevel, log.ErrorLevel, or log.FatalLevel.
Level Level
// Output specifies where to write logs: OutputStdout or OutputFile (required).
Output OutputType
// FilePath is the path to the log file (required if Output is OutputFile).
FilePath string
// MaxSizeMB is the maximum size in megabytes before log rotation (default: 100).
// Only used when Output is OutputFile.
MaxSizeMB int
// MaxBackups is the maximum number of old log files to retain (default: 3).
// Only used when Output is OutputFile.
MaxBackups int
// MaxAgeDays is the maximum number of days to retain old log files (default: 28).
// Only used when Output is OutputFile.
MaxAgeDays int
// EnableCaller enables automatic caller and function extraction for each log entry.
// When enabled, 'caller' (file:line) and 'function' fields are added to logs.
// Performance note: Uses runtime.Caller which has ~200-500ns overhead per log call.
// Recommended: Enable in dev/staging for debugging, disable in production for performance.
// Default: false (disabled)
EnableCaller bool
}
Config holds logger configuration. All fields except file rotation settings (MaxSizeMB, MaxBackups, MaxAgeDays) are required. File rotation settings have defaults and are only used when Output is OutputFile.
type Field ¶
type Field struct {
// contains filtered or unexported fields
}
Field represents a structured log field (key-value pair). It is an opaque type that wraps the underlying logging implementation. Use the provided helper functions (String, Int, etc.) to create fields.
func Any ¶
Any creates a field with any type of value. The value will be JSON-marshaled in the log output. Use this for complex types like maps, structs, and slices.
func Error ¶
Error creates an error field with the key "error". The error message and type will be included in the log output.
type Level ¶
type Level string
Level represents the severity level of a log entry.
const ( // DebugLevel is for verbose debugging information. // Typically disabled in production environments. DebugLevel Level = "debug" // InfoLevel is for general informational messages. // This is the default and recommended level for production. InfoLevel Level = "info" // WarnLevel is for warning messages about potentially harmful situations. // More important than Info but doesn't require immediate action. WarnLevel Level = "warn" // ErrorLevel is for error messages about failures. // Applications running smoothly should not generate error-level logs. ErrorLevel Level = "error" // FatalLevel is for critical errors that cause the application to exit. // After logging, the application will call os.Exit(1). FatalLevel Level = "fatal" )
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger provides structured logging with required traceId and metadata fields. All log methods require a traceId for request traceability and accept optional metadata for contextual information.
func New ¶
New creates a new Logger instance with the provided configuration. Returns an error if the configuration is invalid.
Example:
logger, err := log.New(log.Config{
Service: "my-service",
Env: "production",
Level: log.InfoLevel,
Output: log.OutputStdout,
})
func (*Logger) Debug ¶
Debug logs a message at debug level.
Parameters:
- traceId: Trace identifier for request traceability (required, panics if empty)
- msg: Human-readable log message (required)
- metadata: Contextual information (can be nil, always included in output)
- fields: Additional structured fields (optional)
Panics if traceId is empty.
func (*Logger) Error ¶
Error logs a message at error level.
Parameters:
- traceId: Trace identifier for request traceability (required, panics if empty)
- msg: Human-readable log message (required)
- metadata: Contextual information (can be nil, always included in output)
- fields: Additional structured fields (optional)
Panics if traceId is empty.
func (*Logger) Fatal ¶
Fatal logs a message at fatal level, then calls os.Exit(1).
Parameters:
- traceId: Trace identifier for request traceability (required, panics if empty)
- msg: Human-readable log message (required)
- metadata: Contextual information (can be nil, always included in output)
- fields: Additional structured fields (optional)
Panics if traceId is empty. After logging, this method calls os.Exit(1).
func (*Logger) Info ¶
Info logs a message at info level.
Parameters:
- traceId: Trace identifier for request traceability (required, panics if empty)
- msg: Human-readable log message (required)
- metadata: Contextual information (can be nil, always included in output)
- fields: Additional structured fields (optional)
Panics if traceId is empty.
func (*Logger) Sync ¶
Sync flushes any buffered log entries. Applications should call Sync before exiting to ensure all logs are written.
Example:
func main() {
logger, _ := log.New(log.Config{...})
defer logger.Sync()
// ... application code
}
func (*Logger) Warn ¶
Warn logs a message at warn level.
Parameters:
- traceId: Trace identifier for request traceability (required, panics if empty)
- msg: Human-readable log message (required)
- metadata: Contextual information (can be nil, always included in output)
- fields: Additional structured fields (optional)
Panics if traceId is empty.
func (*Logger) With ¶
With creates a child logger with pre-bound fields. The pre-bound fields will be included in all subsequent log calls from the child logger. The parent logger remains unchanged (immutable pattern).
Example:
userLogger := logger.With(log.String("user_id", "user-456"))
userLogger.Info("req-123", "user action", nil) // includes user_id field
logger.Info("req-456", "other action", nil) // does not include user_id field
Multiple levels of nesting are supported:
serviceLogger := logger.With(log.String("layer", "api"))
userLogger := serviceLogger.With(log.String("user_id", "user-456"))
userLogger.Info("req-123", "action", nil) // includes both layer and user_id
type OutputType ¶
type OutputType string
OutputType specifies the destination for log output.
const ( // OutputStdout writes logs to standard output. // This is the default and recommended for containerized applications. // Logs are written as JSON, one entry per line. OutputStdout OutputType = "stdout" // OutputFile writes logs to a file with automatic rotation. // Rotation is handled by lumberjack based on MaxSizeMB, MaxBackups, and MaxAgeDays settings. OutputFile OutputType = "file" )
func (OutputType) String ¶
func (o OutputType) String() string
String returns the string representation of the OutputType.