logger
Last updated
Last updated
The logger package provides a structured, context-aware logging solution for Go applications. It is built on top of the library and is designed to facilitate easy integration with your projects, offering features like:
JSON-formatted logs suitable for production environments.
Support for multiple log levels (DEBUG
, INFO
, WARN
, ERROR
, FATAL
).
Context propagation to include tracing information (e.g., trace_id
, span_id
).
Customizable log formatters and output destinations.
Integration with web frameworks like Gin.
Structured Logging: Outputs logs in JSON format, making them easy to parse and analyze.
Context-Aware: Supports logging with context.Context
, allowing you to include tracing information automatically.
Customizable Formatter: Use the default StructuredJSONFormatter
or provide your own formatter to customize the log output.
Environment and Service Name: Optionally include environment and service name in your logs for better traceability.
No-Op Logger: Provides a no-operation logger for testing purposes, which discards all log messages.
You can create a logger using the NewLogger
function, providing a Config
struct to customize its behavior:
Alternatively, you can use the default logger:
The 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.
You can update the default logger configuration using SetDefaultLoggerConfig:
The Config struct allows you to customize the logger:
The logger provides methods for different log levels:
Example:
For error and fatal logs, you can include an error object:
You can add persistent fields to the logger using WithFields, which returns a new logger instance:
The StructuredJSONFormatter
is a custom logrus.Formatter
designed to include contextual information in logs. It outputs logs in JSON format with a standardized structure, making it suitable for log aggregation and analysis tools.
Timestamp: Includes a timestamp formatted according to TimestampFormat
.
Severity: The log level (debug
, info
, warning
, error
, fatal
).
Message: The log message.
Error Handling: Automatically includes error messages if an error
is provided.
Tracing Information: Extracts trace_id
and span_id
from the context if available (e.g., when using OpenTelemetry).
Caller Information: Adds information about the function, file, and line number where the log was generated.
Stack Trace: Includes a stack trace for logs at the error
level or higher.
Custom Fields: Supports additional fields provided via logger.Fields
.
Field Key Customization: Allows custom formatting of field keys via FieldKeyFormatter
.
You can customize the StructuredJSONFormatter
when initializing the logger:
Example Log Entry (default FieldKeyFormatter
)
The StructuredJSONFormatter
can extract tracing information (trace_id
and span_id
) from the context.Context
if you are using a tracing system like OpenTelemetry. Ensure that spans are started and the context is propagated correctly.
Caller Information: The formatter includes the function name, file, and line number where the log was generated, aiding in debugging.
Stack Trace: For logs at the error
level or higher, a stack trace is included. This can be useful for diagnosing issues in production.
If you need a different format or additional customization, you can implement your own formatter by satisfying the logrus.Formatter
interface and providing it to the logger configuration.
Usage:
For testing purposes, you can use the no-operation logger, which implements the Logger
interface but discards all log messages:
This can be useful to avoid cluttering test output with logs or when you need a logger that does nothing.
You can find a complete working example in the repository under .
You can find a complete working example in the repository under .