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 guide walks you through the fundamentals of Chromologger: creating a Logger instance, recording informational events with log(), and properly releasing file resources when you’re done. By the end you’ll have a clear picture of the [INFO][timestamp] format written to your log files and the recommended patterns for managing the logger lifecycle.
Creating a Logger
Import Logger from chromologger and call the constructor. The single optional parameter is the path to the log file.
Default — log.log in the caller’s directory
from chromologger import Logger
logger = Logger()
# Creates log.log next to the script that runs this code
Custom path — any relative or absolute file path
from chromologger import Logger
logger = Logger('./logs/app.log')
The parent directory of the log file must already exist before you call
Logger() with a custom path. Chromologger opens the file immediately on
construction and will raise a FileNotFoundError if the directory is missing.
Create it first with os.makedirs('./logs', exist_ok=True).
Logging messages
Call logger.log() with any value. Chromologger converts the argument to a string automatically, so strings, dictionaries, numbers, and any other type all work.
# String message
logger.log('Service started')
# Dictionary — auto-converted to its string representation
logger.log({'key': 'value'})
# Number
logger.log(42)
Each call appends one line to the log file and prints a colorized informational message to the console via chromolog. The file format is:
[INFO][2025-01-06 19:52:08.636560] - Service started
[INFO][2025-01-06 19:52:08.636561] - {'key': 'value'}
[INFO][2025-01-06 19:52:08.636562] - 42
The timestamp is recorded at microsecond precision using the system clock at the moment log() is called.
Closing the logger
logger.close() flushes and closes the underlying file handle. It returns True when the file was closed successfully, or False if no valid file was open (for example, because the constructor failed to open the file).
success = logger.close()
# True → file closed and resources released
# False → no valid file was open
Always close the logger when you are finished — either explicitly at the end of a script or inside a finally block so the file is released even if an exception is raised.
Full example
from chromologger import Logger
import os
os.makedirs('./logs', exist_ok=True)
logger = Logger('./logs/app.log')
logger.log('Service started')
logger.log({'event': 'user_login', 'user': 'alice'})
logger.log(f'Processed {150} records')
logger.close()
The resulting ./logs/app.log will contain:
[INFO][2025-01-06 19:52:08.636560] - Service started
[INFO][2025-01-06 19:52:08.636561] - {'event': 'user_login', 'user': 'alice'}
[INFO][2025-01-06 19:52:08.636562] - Processed 150 records
Ready to record errors and exceptions? Head over to the
Exception Logging guide to learn how log_e()
captures the full traceback — including the source file, line number, and
exception type — in a single call.