Skip to content

Logger manager

Defines a logger manager to manage logging configuration across the Gen AI application components.

Authors

Dimitrij Ray (dimitrij.ray@gdplabs.id) Henry Wicaksono (henry.wicaksono@gdplabs.id) Hermes Vincentius Gani (hermes.v.gani@gdplabs.id)

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.

ColoredFormatter

Bases: Formatter

Custom formatter to add colors based on log level.

format(record)

Format the log record with colors based on log level.

Parameters:

Name Type Description Default
record LogRecord

The log record to be formatted.

required

Returns:

Name Type Description
str str

The formatted log message with color codes.

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.

There are two logging modes: 1. TEXT (Default): Uses the ColoredFormatter to add colors based on log level. 2. JSON: Uses the JsonFormatter to format the log record as a JSON object.

Switch between the two by setting the environment variable LOG_FORMAT to json or text.

Get and use the logger:

manager = LoggerManager()

logger = manager.get_logger()

logger.info("This is an info message")

Set logging configuration:

manager = LoggerManager()

manager.set_level(logging.DEBUG)
manager.set_log_format(custom_log_format)
manager.set_date_format(custom_date_format)

Add a custom handler:

manager = LoggerManager()

handler = logging.FileHandler("app.log")
manager.add_handler(handler)

Log stack traces and exceptions:

try:
    1 / 0
except Exception as e:
    logger.error("Exception occurred", exc_info=True)

During errors, pass the error code using the extra parameter:

logger.error("I am dead!", extra={"error_code": "ERR_CONN_REFUSED"})

Output format in text mode:

[16/04/2025 15:08:18.323 Test INFO] Message

Output format in JSON mode:

{"timestamp": "2025-08-11T19:40:30+07:00", "name": "Test", "level": "INFO", "message": "Message"}

__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

setup_logger(name=None, level=logging.INFO, log_format=TEXT_LOG_FORMAT, date_format=DEFAULT_DATE_FORMAT)

Set up and configure a logger instance.

This function is a wrapper around the logging.getLogger function.

Parameters:

Name Type Description Default
name str | None

The name of the logger. Defaults to None.

None
level int

The logging level. Defaults to logging.INFO.

INFO
log_format str

Custom log format. Defaults to TEXT_LOG_FORMAT.

TEXT_LOG_FORMAT
date_format str

Custom date format. Defaults to DEFAULT_DATE_FORMAT.

DEFAULT_DATE_FORMAT

Returns:

Type Description
Logger

logging.Logger: Configured logger instance.