Logger manager
Defines a logger manager to manage logging configuration across the Gen AI application components.
References
NONE
AppJSONFormatter
Bases: JsonFormatter
JSON formatter that groups error-related fields under an 'error' key.
This formatter renames the following fields when present: 1. exc_info -> error.message 2. stack_info -> error.stacktrace 3. error_code -> error.code
process_log_record(log_record)
Process log record to group and rename error-related fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_record |
LogRecord
|
The original log record. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
LogRecord |
LogRecord
|
The processed log record. |
LoggerManager
A singleton class to manage logging configuration.
This class ensures that the root logger is initialized only once and is used across the application.
Basic usage
The LoggerManager can be used to get a logger instance as follows:
logger = LoggerManager().get_logger()
logger.info("This is an info message")
Set logging configuration
The LoggerManager also provides capabilities to set the logging configuration:
manager = LoggerManager()
manager.set_level(logging.DEBUG)
manager.set_log_format(custom_log_format)
manager.set_date_format(custom_date_format)
Add custom handlers
The LoggerManager also provides capabilities to add custom handlers to the root logger:
manager = LoggerManager()
handler = logging.FileHandler("app.log")
manager.add_handler(handler)
Extra error information
When logging errors, extra error information can be added as follows:
logger.error("I am dead!", extra={"error_code": "ERR_CONN_REFUSED"})
Logging modes
The LoggerManager supports three logging modes:
-
Text: Logs in a human-readable format with RichHandler column-based formatting. Used when the
LOG_FORMATenvironment variable is set to "text". Output example:log 2025-10-08T09:26:16 DEBUG [LoggerName] This is a debug message. 2025-10-08T09:26:17 INFO [LoggerName] This is an info message. 2025-10-08T09:26:18 WARNING [LoggerName] This is a warning message. 2025-10-08T09:26:19 ERROR [LoggerName] This is an error message. 2025-10-08T09:26:20 CRITICAL [LoggerName] This is a critical message. -
Simple: Logs in a human-readable format with Rich colors but without columns-based formatting. Used when the
LOG_FORMATenvironment variable is set to "simple". Output example:log [2025-10-08T09:26:16.123 LoggerName DEBUG] This is a debug message. [2025-10-08T09:26:17.456 LoggerName INFO] This is an info message. [2025-10-08T09:26:18.789 LoggerName WARNING] This is a warning message. [2025-10-08T09:26:19.012 LoggerName ERROR] This is an error message. [2025-10-08T09:26:20.345 LoggerName CRITICAL] This is a critical message. -
JSON: Logs in a JSON format, recommended for easy parsing due to the structured nature of the log records. Used when the
LOG_FORMATenvironment variable is set to "json". Output example:log {"timestamp": "2025-10-08T11:23:43+0700", "name": "LoggerName", "level": "DEBUG", "message": "..."} {"timestamp": "2025-10-08T11:23:44+0700", "name": "LoggerName", "level": "INFO", "message": "..."} {"timestamp": "2025-10-08T11:23:45+0700", "name": "LoggerName", "level": "WARNING", "message": "..."} {"timestamp": "2025-10-08T11:23:46+0700", "name": "LoggerName", "level": "ERROR", "message": "..."} {"timestamp": "2025-10-08T11:23:47+0700", "name": "LoggerName", "level": "CRITICAL", "message": "..."}
When the LOG_FORMAT environment is not set, the LoggerManager defaults to "text" mode.
__new__()
Initialize the singleton instance.
Returns:
| Name | Type | Description |
|---|---|---|
LoggerManager |
LoggerManager
|
The singleton instance. |
add_handler(handler)
Add a custom handler to the root logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handler |
Handler
|
The handler to add to the root logger. |
required |
get_logger(name=None)
Get a logger instance.
This method returns a logger instance that is a child of the root logger. If name is not provided, the root logger will be returned instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str | None
|
The name of the child logger. If None, the root logger will be returned. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
Logger
|
logging.Logger: Configured logger instance. |
set_date_format(date_format)
Set date format for all loggers in the hierarchy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_format |
str
|
The date format to set. |
required |
set_level(level)
Set logging level for all loggers in the hierarchy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level |
int
|
The logging level to set (e.g., logging.INFO, logging.DEBUG). |
required |
set_log_format(log_format)
Set logging format for all loggers in the hierarchy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_format |
str
|
The log format to set. |
required |
SimpleRichHandler(*args, **kwargs)
Bases: StreamHandler
Custom StreamHandler that utilizes Rich only to apply colors, without columns or Rich formatting.
Attributes:
| Name | Type | Description |
|---|---|---|
console |
Console
|
The Rich Console object to use for printing. |
Initializes a new instance of the SimpleRichHandler class.
emit(record)
Emits a log record with simple Rich coloring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record |
LogRecord
|
The log record to emit. |
required |
TextRichHandler
Bases: RichHandler
Custom RichHandler that applies specific colors and log format.
emit(record)
Emits a log record with custom coloring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record |
LogRecord
|
The log record to emit. |
required |