Documentation
¶
Overview ¶
Package log provides a logging adapter built on top of Uber's Zap library. It supports structured logging with configurable output, log levels, and hooks.
Index ¶
- Constants
- func CopyValues(ctxTo context.Context, ctxFrom context.Context) context.Context
- func RewriteContextField(ctx context.Context, field Field) context.Context
- func StdLoggerWithLevel(adapter Logger, level Level, withFields ...Field) *log.Logger
- func ToContext(ctx context.Context, kvs ...Field) context.Context
- func UpsertContextField(ctx context.Context, field Field) context.Context
- type Adapter
- func (a *Adapter) Config() Config
- func (a *Adapter) Debug(ctx context.Context, message any, fields ...Field)
- func (a *Adapter) Enabled(level Level) bool
- func (a *Adapter) Error(ctx context.Context, message any, fields ...Field)
- func (a *Adapter) Fatal(ctx context.Context, message any, fields ...Field)
- func (a *Adapter) Info(ctx context.Context, message any, fields ...Field)
- func (a *Adapter) Log(ctx context.Context, level Level, message any, fields ...Field)
- func (a *Adapter) SetLevel(level Level)
- func (a *Adapter) Sync() error
- func (a *Adapter) Warn(ctx context.Context, message any, fields ...Field)
- type Config
- type Field
- func Any(key string, value any) Field
- func Bool(key string, value bool) Field
- func ByteString(key string, value []byte) Field
- func ContextLogValues(ctx context.Context) []Field
- func FromContext(ctx context.Context, key string) (Field, bool)
- func Int(key string, value int) Field
- func Int32(key string, value int32) Field
- func Int64(key string, value int64) Field
- func String(key string, value string) Field
- type Level
- type Logger
- type Option
- type SamplingConfig
Constants ¶
const ( FatalLevel = zap.FatalLevel ErrorLevel = zap.ErrorLevel WarnLevel = zap.WarnLevel InfoLevel = zap.InfoLevel DebugLevel = zap.DebugLevel )
Variables ¶
This section is empty.
Functions ¶
func CopyValues ¶ added in v1.66.0
CopyValues copies logging fields from one context to another.
func RewriteContextField ¶ added in v1.33.0
RewriteContextField replaces a field in the context if it exists.
func StdLoggerWithLevel ¶
StdLoggerWithLevel wraps a Logger to provide a standard log.Logger interface.
Types ¶
type Adapter ¶
type Adapter struct {
// contains filtered or unexported fields
}
Adapter is the main logging implementation built on Zap.
func NewFromConfig ¶
NewFromConfig creates a new Adapter from a Config.
type Config ¶
type Config struct {
// IsInDevMode enables development mode with human-friendly output.
IsInDevMode bool
// OutputPaths specifies the destinations for log output.
OutputPaths []string
// DisableDefaultOutput prevents logging to the default output.
DisableDefaultOutput bool
// SamplingConfig configures log message sampling.
Sampling *SamplingConfig
// Hooks is a slice of functions called for each log entry.
Hooks []func(entry zapcore.Entry) error
// InitialLevel sets the minimum log level.
InitialLevel Level
}
Config holds the configuration for the logging adapter.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a Config with default settings.
type Field ¶
Field is an alias for zap.Field representing a structured log field.
func ByteString ¶
ByteString creates a byte slice field.
func ContextLogValues ¶
ContextLogValues extracts logging fields from a context.
func FromContext ¶ added in v1.66.0
FromContext retrieves a field from the context by key.
type Logger ¶
type Logger interface {
Error(ctx context.Context, message any, fields ...Field)
Warn(ctx context.Context, message any, fields ...Field)
Info(ctx context.Context, message any, fields ...Field)
Debug(ctx context.Context, message any, fields ...Field)
}
Logger defines the interface for structured logging.
type Option ¶
type Option func(cfg *Config)
Option is a function that configures a Config.
func WithDevelopmentMode ¶
func WithDevelopmentMode() Option
WithDevelopmentMode enables development mode for human-friendly output.
func WithDisableDefaultOutput ¶ added in v1.64.2
func WithDisableDefaultOutput() Option
WithDisableDefaultOutput disables the default output.
func WithFileOutput ¶
WithFileOutput adds a file output with rotation configuration.
type SamplingConfig ¶
type SamplingConfig = zap.SamplingConfig
SamplingConfig is an alias for zap.SamplingConfig.