Skip to main content
Logger is the primary class in Chromologger. It manages a log file and provides three public methods for writing records — log() for general informational messages, log_e() for structured exception entries, and close() to release the file handle when you are done.

Import


Constructor

Creates a new Logger instance, resolves the target log file path, and opens the file in append mode.
str
default:"'log.log'"
Name or path of the log file.
  • When the default 'log.log' is used, the file is created in the same directory as the calling script, detected automatically via Python’s inspect module.
  • Any other value is resolved to an absolute path using pathlib.Path.resolve(), so both relative paths (e.g. './logs/app.log') and absolute paths (e.g. '/var/log/app.log') are accepted.
Examples
The parent directory of the log file must already exist. Chromologger will not create missing directories — if the directory is absent, __open() will catch a FileNotFoundError, store -1 in self.file, and write the failure to the module’s internal log.

Instance Attributes

After construction, the following attributes are available on every Logger instance.
str
The exact value passed to the constructor (e.g. 'log.log' or './logs/app.log').
str
Absolute path of the directory that contains the script which created this Logger, resolved via inspect.currentframe().f_back.f_code.co_filename.
str
Fully resolved absolute path to the log file. When log_file_name is 'log.log' this is <caller_script>/log.log; otherwise it is the result of FileManager.get_abs_path(log_file_name).
TextIOWrapper | int
The open file object returned by FileManager.open_file(). Set to -1 if the file could not be opened (e.g. the parent directory does not exist).

Methods

log(msg)

Writes an [INFO] record to the log file. msg can be any Python object — it is converted to str automatically before writing. After writing to the file, log() also prints a colorized informational message to the console via chromolog, showing the path of the log file.
any
required
The value to log. Strings, numbers, dicts, objects — anything that can be meaningfully converted with str() is accepted.
Examples
Output format

log_e(e)

Writes an [ERROR] record with full exception context — exception type, source file, line number, and the exception message — all extracted via traceback.extract_tb(e.__traceback__).
Exception
required
A caught exception instance. The method reads e.__traceback__ to locate the precise file and line where the exception was raised, then reads e.__class__.__name__ for the type name and str(e) for the message.
Example
Output format
If the exception was never actually raised (i.e. it has no traceback attached), traceback.extract_tb() will return an empty list and indexing into it will itself raise an IndexError. In that case log_e() catches the secondary exception internally via __log() and writes it to the module’s own internal log — it will not propagate to your application code.

close()

Closes the log file and releases the underlying file handle.
bool
  • True — the file was a valid TextIOWrapper and has been closed successfully.
  • False — the file was never opened (i.e. self.file is -1); nothing was closed.
Example
Always call close() when your application exits (or when you are done with the logger) to ensure all buffered writes are flushed to disk and the OS file descriptor is freed.

Module-level Constants

These values are exported from chromologger alongside Logger.

Log Format Reference

Every entry written by Logger follows one of these two formats:
The timestamp is produced by Dates.now_date() which returns datetime.now() — local time with microsecond precision. See the Dates reference for details.