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.

This page walks through the complete basic workflow for using Chromologger: install the package, create a Logger instance, write informational log entries, capture exceptions with full traceback detail, and close the log file to flush and release resources. By the end you will have a working script and a clear picture of what ends up in your log file.
1

Install Chromologger

Install the package from PyPI. The chromolog==0.2.5 dependency is pulled in automatically.
pip install chromologger
2

Import and create a Logger

Import the Logger class and create an instance. You can use the zero-config default or supply an explicit path.Default — places log.log in the same directory as the calling script:
from chromologger import Logger

logger = Logger()
Custom path — relative or absolute:
from chromologger import Logger
import os

os.makedirs('./logs', exist_ok=True)
logger = Logger('./logs/app.log')
If the directory in your path does not already exist, Chromologger will raise a FileNotFoundError and log it internally. Always create the target directory with os.makedirs(..., exist_ok=True) before passing a nested path to Logger.
3

Log an info message

Call logger.log(msg) with any value. Chromologger converts it to a string, prepends an [INFO] level tag and a microsecond-precision timestamp, and appends the line to the log file.
logger.log('Application started')
The line written to the file looks like:
[INFO][2025-01-06 19:52:08.636560] - Application started
4

Log an exception

Wrap risky code in a try/except block and pass the caught exception to logger.log_e(e). Chromologger automatically extracts the exception type, source file, line number, and message from the live traceback.
try:
    result = 10 / 0
except Exception as e:
    logger.log_e(e)
The line written to the file looks like:
[ERROR][2025-01-06 19:52:08.636560] - Exception: ZeroDivisionError - File: /app/app.py - ErrorLine: 9 - Message: division by zero
5

Close the logger

Call logger.close() when you are done. It flushes and closes the underlying file handle and returns True on success.
result = logger.close()
print(result)   # True
Always close the logger before your program exits to ensure all buffered data is written to disk.

Complete working script

Putting all the steps together:
app.py
from chromologger import Logger
import os

os.makedirs('./logs', exist_ok=True)
logger = Logger('./logs/app.log')

logger.log('Application started')

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

logger.log('Shutting down')
logger.close()

What’s in the log file

After running the script above, ./logs/app.log contains:
[INFO][2025-01-06 19:52:08.636560] - Application started
[ERROR][2025-01-06 19:52:08.636560] - Exception: ZeroDivisionError - File: /app/app.py - ErrorLine: 9 - Message: division by zero
[INFO][2025-01-06 19:52:08.636561] - Shutting down
Because Chromologger always opens files in append mode, re-running the script adds new entries below the existing ones — nothing is ever overwritten.
In addition to writing to the log file, Chromologger prints colorized messages to your terminal via chromolog each time log() is called. The console output includes the full path of the active log file so you always know where to find your records, even across multiple Logger instances.

Next steps

Basic logging guide

Explore logging patterns, multiple Logger instances, and organizing log files by date or component.

Logger API reference

Full reference for the Logger constructor and all public methods: log(), log_e(), and close().

Build docs developers (and LLMs) love