Documentation ¶
Index ¶
- Constants
- Variables
- func NewContext(ctx context.Context, logger Logger) context.Context
- func NewRequest(r *http.Request, logger Logger) *http.Request
- func NoopFieldKeyFormatter(defaultKey string) string
- func SetDefaultLoggerConfig(config Config) error
- type Config
- type FieldKeyFormatter
- type Fields
- type LogLevel
- type Logger
- type StructuredJSONFormatter
Constants ¶
const ( // DefaultEnvironmentKey is the default key used for the environment field in logs. DefaultEnvironmentKey = "environment" // DefaultServiceNameKey is the default key used for the service name field in logs. DefaultServiceNameKey = "service_name" // DefaultErrorKey is the default key used for the error field in logs. DefaultErrorKey = "error" )
const ( DefaultSJsonFmtTimestampKey = "timestamp" DefaultSJsonFmtSeverityKey = "severity" DefaultSJsonFmtMessageKey = "message" DefaultSJsonFmtErrorKey = "error" DefaultSJsonFmtTraceIDKey = "trace_id" DefaultSJsonFmtSpanIDKey = "span_id" DefaultSJsonFmtCallerKey = "caller" DefaultSJsonFmtCallerFuncKey = "function" DefaultSJsonFmtCallerFileKey = "file" DefaultSJsonFmtStackTraceKey = "stack_trace" )
Variables ¶
var (
ErrInvalidLogLevel = errors.New("invalid log level")
)
Functions ¶
func NewContext ¶
NewContext returns a new Context that carries the provided Logger.
func NewRequest ¶
NewRequest returns a new *http.Request that carries the provided Logger.
func NoopFieldKeyFormatter ¶
NoopFieldKeyFormatter is the default implementation of FieldKeyFormatter. It returns the key unchanged, effectively performing no operation on the key.
func SetDefaultLoggerConfig ¶
SetDefaultLoggerConfig tries to set a custom configuration for the default logger. If creating a logger with the provided config fails, the default configuration remains unchanged.
Types ¶
type Config ¶
type Config struct { // Level determines the minimum log level that will be processed by the logger. // Logs with a level lower than this will be ignored. Level LogLevel // Formatter is an optional field for specifying a custom logrus formatter. // If not provided, the logger will use the StructuredJSONFormatter by default. Formatter logrus.Formatter // Environment is an optional field for specifying the running environment (e.g., "production", "staging"). // This field is used for adding environment-specific fields to logs. Environment string // ServiceName is an optional field for specifying the name of the service. // This field is used for adding service-specific fields to logs. ServiceName string // Output is an optional field for specifying the output destination for logs (e.g., os.Stdout, file). // If not provided, logs will be written to stdout by default. Output io.Writer }
Config holds the logger configuration.
type FieldKeyFormatter ¶
FieldKeyFormatter is a function type that allows users to customize the keys of log fields.
Example usage:
customFieldKeyFormatter := func(key string) string { return strings.ToUpper(key) }
type Fields ¶
type Fields map[string]interface{}
Fields represents a key-value pair for structured logging.
type Logger ¶
type Logger interface { WithFields(fields Fields) Logger Debug(ctx context.Context, msg string, fields Fields) Info(ctx context.Context, msg string, fields Fields) Warn(ctx context.Context, msg string, fields Fields) Error(ctx context.Context, msg string, err error, fields Fields) Fatal(ctx context.Context, msg string, err error, fields Fields) }
func FromContext ¶
FromContext retrieves the Logger from the context. It returns a default logger if the context doesn't have one.
func FromRequest ¶
FromRequest retrieves the Logger from the HTTP request's context.
func NewDefaultLogger ¶
func NewDefaultLogger() Logger
NewDefaultLogger returns a logger instance with the default or user-defined configuration. If SetDefaultLoggerConfig has been called, it uses the user-defined configuration; otherwise, it uses the package's default configuration. The default logger configuration uses the StructuredJSONFormatter, which outputs logs in JSON format with the following fields:
- timestamp: formatted in RFC3339 format.
- severity: the severity level of the log (e.g., info, debug, error).
- message: the log message.
- error: the error message for logs with error-level severity or higher.
- trace_id: the trace identifier for correlating logs with distributed traces (if available).
- span_id: the span identifier for correlating logs within specific spans of a trace (if available).
- caller: the function, file, and line number where the log was generated.
- stack_trace: included for logs with error-level severity or higher, providing additional debugging context.
func NewNoopLogger ¶
func NewNoopLogger() Logger
NewNoopLogger returns a no-op logger that discards all log messages.
type StructuredJSONFormatter ¶
type StructuredJSONFormatter struct { // TimestampFormat sets the format used for marshaling timestamps. TimestampFormat string // PrettyPrint will indent all JSON logs. PrettyPrint bool // SkipPackages is a list of packages to skip when searching for the caller. SkipPackages []string // FieldKeyFormatter is a function type that allows users to customize log field keys. FieldKeyFormatter FieldKeyFormatter }
StructuredJSONFormatter is a custom logrus formatter for structured JSON logs. It includes the following fields:
- timestamp: The log timestamp in the specified format.
- severity: The log severity level (e.g., info, debug, error).
- message: The log message.
- error: The error message if present.
- trace_id: The trace ID if available.
- span_id: The span ID if available.
- caller: The caller's function name, file, and line number.
- stack_trace: The stack trace for error levels.
Directories ¶
Path | Synopsis |
---|---|
example
|
|
Package logger_mocks is a generated GoMock package.
|
Package logger_mocks is a generated GoMock package. |