logger
Logger Package
The logger package provides a structured, context-aware logging solution for Go applications. It is built on top of the logrus 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.
Installation
go get github.com/kittipat1413/go-common/framework/loggerDocumentation
For detailed API documentation, examples, and usage patterns, visit the Go Package Documentation.
Features
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
StructuredJSONFormatteror 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.
Usage
Creating a Logger
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
NewDefaultLoggerreturns a logger instance with the default or user-defined configuration.If
SetDefaultLoggerConfighas been called, it uses the user-defined configuration; otherwise, it uses the package's default configuration.
Updating the Default Logger Configuration
You can update the default logger configuration using SetDefaultLoggerConfig:
Configuration
The Config struct allows you to customize the logger:
Logging Messages
The logger provides methods for different log levels:
Example:
Including Errors
For error and fatal logs, you can include an error object:
Adding Persistent Fields
You can add persistent fields to the logger using WithFields, which returns a new logger instance:
You can find a complete working example in the repository under framework/logger/example.
StructuredJSONFormatter
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.
Features
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
erroris provided.Tracing Information: Extracts
trace_idandspan_idfrom 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
errorlevel or higher.Custom Fields: Supports additional fields provided via
logger.Fields.Field Key Customization: Allows custom formatting of field keys via
FieldKeyFormatter.
Configuration
You can customize the StructuredJSONFormatter when initializing the logger:
Example Log Entry (default FieldKeyFormatter)
Tracing Integration
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 and Stack Trace
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
errorlevel or higher, a stack trace is included. This can be useful for diagnosing issues in production.
Custom Formatter
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:
No-Op Logger
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.
Example
You can find a complete working example in the repository under framework/logger/example.
Last updated