Documentation
¶
Overview ¶
Package rlog provides a structured logging library for the ROR project.
rlog is a wrapper around the uber-go/zap logging package that provides: - Structured logging with strongly typed fields - Context-aware logging with automatic extraction of request IDs and trace data - OpenTelemetry integration for distributed tracing - Configurable outputs via environment variables - Support for both development (human-readable) and production (JSON) formats - HTTP middleware for Gin web framework
The package is configured via environment variables: - LOG_LEVEL: Sets the minimum log level (debug, info, warn, error) - LOG_OUTPUT: Specifies where logs are written (stderr by default, can be files or multiple targets) - LOG_OUTPUT_ERROR: Specifies where error logs are written - LOG_DEVELOP: When "true", outputs human-readable logs instead of JSON
Basic usage:
rlog.Info("This is an informational message", rlog.String("key", "value"))
rlog.Error("An error occurred", err, rlog.Int("status", 500))
// With context (includes trace IDs and context values automatically)
rlog.Infoc(ctx, "Processing request", rlog.String("user", "admin"))
Index ¶
- Constants
- func AddContextKeyField(key string) error
- func Debug(msg string, fields ...Field)
- func Debugc(ctx context.Context, msg string, fields ...Field)
- func Error(msg string, err error, fields ...Field)
- func Errorc(ctx context.Context, msg string, err error, fields ...Field)
- func Fatal(msg string, err error, fields ...Field)
- func Fatalc(ctx context.Context, msg string, err error, fields ...Field)
- func GetLogLevel() string
- func Info(msg string, fields ...Field)
- func Infoc(ctx context.Context, msg string, fields ...Field)
- func Infof(format string, v ...any)
- func InitializeRlog()
- func LogMiddleware() gin.HandlerFunc
- func Warn(msg string, fields ...Field)
- func Warnc(ctx context.Context, msg string, fields ...Field)
- type CorrelationIdType
- type Field
- type Logger
Constants ¶
const ( RequestIdKey CorrelationIdType = iota SessionIdKey LOG_LEVEL = "LOG_LEVEL" LOG_OUTPUT = "LOG_OUTPUT" LOG_OUTPUT_ERROR = "LOG_OUTPUT_ERROR" LOG_DEVELOP = "LOG_DEVELOP" )
Variables ¶
This section is empty.
Functions ¶
func AddContextKeyField ¶ added in v0.0.11
AddContextKeyField adds a key to look for in contexts so that we can add the context value as a persistent field to all logs. This allows automatic inclusion of specified context values in log entries.
Parameters:
- key: The context key to extract values from. Must be non-empty.
Returns:
- An error if the key is empty, nil otherwise.
func Debug ¶
Debug logs a message at DebugLevel. The message includes any fields passed at the log site.
Parameters:
- msg: The log message
- fields: Optional fields to add context to the log entry
func Debugc ¶
Debugc logs a message at DebugLevel with context. The message includes any fields passed at the log site and any tracing fields in the attached context, if the context contains known fields these are also added.
Parameters:
- ctx: The context which may contain trace information
- msg: The log message
- fields: Optional fields to add context to the log entry
func Error ¶
Error logs a message at ErrorLevel. The message includes any fields passed at the log site and the error.
Parameters:
- msg: The log message
- err: The error to log
- fields: Optional fields to add context to the log entry
func Errorc ¶
Errorc logs a message at ErrorLevel with context. The message includes any fields passed at the log site and any tracing fields in the attached context, if the context contains known fields these are also added.
Parameters:
- ctx: The context which may contain trace information
- msg: The log message
- err: The error to log
- fields: Optional fields to add context to the log entry
func Fatal ¶
Fatal logs a message at FatalLevel. The message includes any fields passed at the log site and the error. The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.
Parameters:
- msg: The log message
- err: The error to log
- fields: Optional fields to add context to the log entry
func Fatalc ¶
Fatalc logs a message at FatalLevel with context. The message includes any fields passed at the log site and any tracing fields in the attached context, if the context contains known fields these are also added. The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.
Parameters:
- ctx: The context which may contain trace information
- msg: The log message
- err: The error to log
- fields: Optional fields to add context to the log entry
func GetLogLevel ¶ added in v1.0.0
func GetLogLevel() string
GetLogLevel returns the current log level as a string. The log level indicates the minimum severity of messages that will be logged.
func Info ¶
Info logs a message at InfoLevel. The message includes any fields passed at the log site.
Parameters:
- msg: The log message
- fields: Optional fields to add context to the log entry
func Infoc ¶
Infoc logs a message at InfoLevel with context. The message includes any fields passed at the log site and any tracing fields in the attached context, if the context contains known fields these are also added.
Parameters:
- ctx: The context which may contain trace information
- msg: The log message
- fields: Optional fields to add context to the log entry
func Infof ¶
Infof logs a message at InfoLevel with context. The message is formated with sprintf.
Parameters:
- format: A format string for the message
- v: Values to be formatted into the message
func InitializeRlog ¶
func InitializeRlog()
InitializeRlog initializes the global logger based on configuration. It creates either a default or development logger configuration depending on environment settings and initializes a global logger instance. If initialization fails, it will panic with an error message.
func LogMiddleware ¶
func LogMiddleware() gin.HandlerFunc
LogMiddleware is a Gin middleware function for logging HTTP requests. It logs detailed information about any requests that result in error status codes (3xx, 4xx, 5xx), including method, status code, path, latency, user agent, client IP, forwarded address, response size, and any errors that occurred during request processing.
Usage:
router := gin.New() router.Use(rlog.LogMiddleware())
Returns:
- A Gin HandlerFunc that can be used in middleware chains
func Warn ¶
Warn logs a message at WarnLevel. The message includes any fields passed at the log site.
Parameters:
- msg: The log message
- fields: Optional fields to add context to the log entry
func Warnc ¶
Warnc logs a message at WarnLevel with context. The message includes any fields passed at the log site and any tracing fields in the attached context, if the context contains known fields these are also added.
Parameters:
- ctx: The context which may contain trace information
- msg: The log message
- fields: Optional fields to add context to the log entry
Types ¶
type CorrelationIdType ¶
type CorrelationIdType int
type Field ¶
func Any ¶
Any creates a field with the given key and arbitrary value. Any handles JSON marshaling for arbitrary objects.
Parameters:
- key: The field key
- value: The value to be logged (will be marshaled to JSON)
Returns:
- A Field object that can be used in logging functions
func ByteString ¶
ByteString creates a field with the given key and byte slice value.
Parameters:
- key: The field key
- value: The byte slice value
Returns:
- A Field object that can be used in logging functions
func Int ¶
Int creates a field with the given key and integer value.
Parameters:
- key: The field key
- value: The integer value
Returns:
- A Field object that can be used in logging functions
func Int64 ¶
Int64 creates a field with the given key and int64 value.
Parameters:
- key: The field key
- value: The int64 value
Returns:
- A Field object that can be used in logging functions
func String ¶
String creates a field with the given key and string value.
Parameters:
- key: The field key
- value: The string value
Returns:
- A Field object that can be used in logging functions