Documentation
¶
Overview ¶
Package logx provides zerolog-based structured logging with context propagation, durable log fields, request logging middleware for echo, and GCP severity mapping
Index ¶
- Constants
- func BenignError(err error) bool
- func ErrorEvent(ctx context.Context, err error) *zerolog.Event
- func FromContext(ctx context.Context) *zerolog.Logger
- func LoggingMiddleware(config Config) echo.MiddlewareFunc
- func MatchEchoLevel(level log.Lvl) (zerolog.Level, log.Lvl)
- func Middleware(config MiddlewareConfig) echo.MiddlewareFunc
- func RequestErrorLevel(err error, status int) zerolog.Level
- func SeedContext(ctx context.Context) context.Context
- func SlogLogger(l zerolog.Logger) *slog.Logger
- func WithField(ctx context.Context, key string, value any) context.Context
- func WithFields(ctx context.Context, fields map[string]any) context.Context
- type Config
- type LogFields
- type Logger
- func (l *Logger) Debug(i ...any)
- func (l *Logger) Debugf(format string, i ...any)
- func (l *Logger) Debugj(j log.JSON)
- func (l *Logger) Error(err error)
- func (l *Logger) Errorf(format string, i ...any)
- func (l *Logger) Errorj(j log.JSON)
- func (l *Logger) Fatal(i ...any)
- func (l *Logger) Fatalf(format string, i ...any)
- func (l *Logger) Fatalj(j log.JSON)
- func (l *Logger) Info(i ...any)
- func (l *Logger) Infof(format string, i ...any)
- func (l *Logger) Infoj(j log.JSON)
- func (l *Logger) Level() log.Lvl
- func (l *Logger) Output() io.Writer
- func (l *Logger) Panic(i ...any)
- func (l *Logger) Panicf(format string, i ...any)
- func (l *Logger) Panicj(j log.JSON)
- func (l *Logger) Prefix() string
- func (l *Logger) Print(i ...any)
- func (l *Logger) Printf(format string, i ...any)
- func (l *Logger) Printj(j log.JSON)
- func (l *Logger) SetHeader(_ string)
- func (l *Logger) SetLevel(level log.Lvl)
- func (l *Logger) SetOutput(newOut io.Writer)
- func (l *Logger) SetPrefix(newPrefix string)
- func (l *Logger) Unwrap() zerolog.Logger
- func (l *Logger) Warn(i ...any)
- func (l *Logger) Warnf(format string, i ...any)
- func (l *Logger) Warnj(j log.JSON)
- func (l *Logger) WithContext(ctx context.Context) context.Context
- func (l *Logger) Write(p []byte) (n int, err error)
- type LoggerConfig
- type LoggerSet
- type MiddlewareConfig
Constants ¶
const ( // FieldRequestID is the log field for the request id FieldRequestID = "request_id" // FieldOrganizationID is the log field for the organization id FieldOrganizationID = "organization_id" // FieldSubjectID is the log field for the authenticated subject id FieldSubjectID = "subject_id" // FieldSubjectEmail is the log field for the authenticated subject email FieldSubjectEmail = "subject_email" // FieldCapabilities is the log field for the caller capability set FieldCapabilities = "capabilities" // FieldOperation is the log field for the operation being performed FieldOperation = "operation" // FieldRemoteIP is the log field for the client origin ip FieldRemoteIP = "remote_ip" // FieldUserAgent is the log field for the client user agent FieldUserAgent = "user_agent" // FieldRequestProtocol is the log field for the request protocol FieldRequestProtocol = "request_protocol" // FieldTrueClientIP is the log field for the True-Client-IP header FieldTrueClientIP = "true_client_ip" // FieldForwardedFor is the log field for the X-Forwarded-For header FieldForwardedFor = "x_forwarded_for" // FieldRealIP is the log field for the X-Real-IP header FieldRealIP = "x_real_ip" )
Canonical log field names shared by the middleware, caller enrichment, and consumers so the same concept always lands under the same key
Variables ¶
This section is empty.
Functions ¶
func BenignError ¶
BenignError reports whether err is an expected request-lifecycle failure rather than a server fault
func ErrorEvent ¶
ErrorEvent returns a context-logger event for err at error level, or warn level when the error is benign
func FromContext ¶
FromContext returns the logger stored on the context or falls back to the global logger
func LoggingMiddleware ¶
func LoggingMiddleware(config Config) echo.MiddlewareFunc
LoggingMiddleware is a middleware that logs requests using the provided logger
func MatchEchoLevel ¶
MatchEchoLevel returns a zerolog level and echo level for a given echo level
func Middleware ¶
func Middleware(config MiddlewareConfig) echo.MiddlewareFunc
Middleware returns echo v5 middleware that logs requests using the configured logger and seeds the request context with a request-scoped logger carrying durable fields
func RequestErrorLevel ¶
RequestErrorLevel returns the log level for a request that finished with err and HTTP status
func SeedContext ¶
SeedContext ensures the provided context carries a logger, returning a derived context when necessary
func SlogLogger ¶
SlogLogger wraps a zerolog logger in a *slog.Logger for consumers that require the standard library interface; level filtering is left to the underlying zerolog logger
Types ¶
type Config ¶
type Config struct {
// Logger is a custom instance of the logger to use
Logger *Logger
// Skipper defines a function to skip middleware
Skipper middleware.Skipper
// RequestIDHeader is the header name to use for the request ID in a log record
RequestIDHeader string
// RequestIDKey is the key name to use for the request ID in a log record
RequestIDKey string
// HandleError indicates whether to forward errors to the global error handler, so it can decide the appropriate status code
HandleError bool
// AttachRequestMetadata controls whether stable request metadata (client origin IP, user agent, and forwarding headers)
// is attached to the request-scoped logger context so downstream log entries can include it
AttachRequestMetadata bool
}
Config defines the config for the echolog middleware
type LogFields ¶
LogFields holds structured log fields that can be captured and restored
func FieldsFromContext ¶
FieldsFromContext returns the durable log fields stored on the context
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a wrapper around zerolog.Logger that provides an implementation of the echo.Logger interface
func (*Logger) SetOutput ¶
SetOutput implements echo.Logger interface; zerolog preserves the logger's context, hooks, and level across the output switch
func (*Logger) WithContext ¶
WithContext returns a new context with the provided logger
type LoggerConfig ¶
type LoggerConfig struct {
// Level sets the zerolog level. Use zerolog.NoLevel to keep the existing global default
Level zerolog.Level
// Pretty toggles human-readable console output instead of structured JSON
Pretty bool
// Writer controls where logs are written. Defaults to os.Stdout
Writer io.Writer
// IncludeCaller attaches caller information to each log entry
IncludeCaller bool
// IncludeStack wires zerolog's ErrorStackMarshaler to pkgerrors.MarshalStack so events
// carrying an error render its stack trace under the stack field
IncludeStack bool
// WithEcho instructs Configure to build an echo-compatible logger
WithEcho bool
// SetGlobal updates zerolog's global logger (`log.Logger`) using the configured settings
SetGlobal bool
}
LoggerConfig controls how loggers produced by Configure behave
type LoggerSet ¶
type LoggerSet struct {
// Logger is the configured root zerolog logger
Logger zerolog.Logger
// Echo is the echo-compatible logger, populated only when LoggerConfig.WithEcho is set
Echo *Logger
}
LoggerSet contains the loggers produced by Configure
func Configure ¶
func Configure(cfg LoggerConfig) LoggerSet
Configure builds loggers according to the supplied configuration
type MiddlewareConfig ¶
type MiddlewareConfig struct {
// Logger is a custom instance of the logger to use
Logger *Logger
// Skipper short-circuits the middleware when it returns true
Skipper func(c *echo.Context) bool
// RequestIDHeader is the header name to use for the request ID in a log record
RequestIDHeader string
// RequestIDKey is the key name to use for the request ID in a log record
RequestIDKey string
// HandleError indicates whether to forward errors to the global error handler, so it can decide the appropriate status code
HandleError bool
// AttachRequestMetadata controls whether stable request metadata (client origin IP, user agent, and forwarding headers)
// is attached to the request-scoped logger context so downstream log entries can include it
AttachRequestMetadata bool
}
MiddlewareConfig configures the echo v5 request logging middleware
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package consolelog is a zerolog consolewriter output formatter that can be used generically with any zerolog instantiation so that it's not specific to a particular application
|
Package consolelog is a zerolog consolewriter output formatter that can be used generically with any zerolog instantiation so that it's not specific to a particular application |