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
ImportLogger 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
Logging messages
Calllogger.log() with any value. Chromologger converts the argument to a string automatically, so strings, dictionaries, numbers, and any other type all work.
chromolog. The file format is:
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).
finally block so the file is released even if an exception is raised.
Full example
basic_logging.py
./logs/app.log will contain: