Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Dev2Forge/chromologger/llms.txt

Use this file to discover all available pages before exploring further.

log_e() is Chromologger’s dedicated method for exceptions. Instead of manually formatting an error message, you pass the caught exception directly and Chromologger extracts the exception class name, the source file where it was raised, the line number, and the full error message — all automatically, using Python’s built-in traceback module.

How it works

When you call logger.log_e(e), Chromologger calls traceback.extract_tb(e.__traceback__) internally and picks the last frame of the stack — the location where the exception actually occurred. It then writes a single structured line at the [ERROR] level:
[ERROR][2025-01-06 19:52:08.636560] - Exception: FileNotFoundError - File: /app/main.py - ErrorLine: 35 - Message: [Errno 2] No such file or directory: './config.json'
The fields in the output are:
FieldDescription
ExceptionThe exception class name (e.g. FileNotFoundError, ZeroDivisionError)
FileAbsolute path to the Python file where the exception was raised
ErrorLineThe line number within that file
MessageThe exception’s string representation

Basic exception logging

from chromologger import Logger

logger = Logger('./logs/errors.log')

try:
    result = 10 / 0
except ZeroDivisionError as e:
    logger.log_e(e)

logger.close()
Resulting log line:
[ERROR][2025-01-06 19:52:08.636560] - Exception: ZeroDivisionError - File: /app/main.py - ErrorLine: 6 - Message: division by zero

File not found

try:
    with open('config.json', 'r') as f:
        data = f.read()
except FileNotFoundError as e:
    logger.log_e(e)
[ERROR][2025-01-06 19:52:08.636560] - Exception: FileNotFoundError - File: /app/main.py - ErrorLine: 3 - Message: [Errno 2] No such file or directory: 'config.json'

Catching broad exceptions

You can combine log_e() with a follow-up log() call to record both the full traceback and a human-readable summary:
try:
    # risky operation
    process_data()
except Exception as e:
    logger.log_e(e)
    logger.log(f'Operation failed: {type(e).__name__}')
This produces two lines in the log file: one [ERROR] entry with the complete traceback detail, and one [INFO] entry acting as a plain-text summary that is easy to grep for.
log_e() extracts traceback information only when the exception has been raised — that is, when it carries a __traceback__ attribute. If you construct an exception object manually without raising it (e.g. e = ValueError("oops")) and pass it to log_e(), the traceback will be empty and Chromologger will not be able to populate the File or ErrorLine fields. Always call log_e() from inside an except block.

Next steps:

Build docs developers (and LLMs) love