Skip to main content
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
Custom path — any relative or absolute file path
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.
Each call appends one line to the log file and prints a colorized informational message to the console via chromolog. The file format is:
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).
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

basic_logging.py
The resulting ./logs/app.log will contain:
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.