Note
Go to the end to download the full example code.
biogeme.biogeme_logging¶
Examples of use of several functions.
This is designed for programmers who need examples of use of the functions of the module. The examples are designed to illustrate the syntax. They do not correspond to any meaningful model.
Michel Bierlaire Sun Jun 29 2025, 02:24:09
import os
import biogeme.biogeme_logging as blog
from biogeme.version import get_text
Version of Biogeme.
print(get_text())
biogeme 3.3.1 [2025-09-03]
Home page: http://biogeme.epfl.ch
Submit questions to https://groups.google.com/d/forum/biogeme
Michel Bierlaire, Transport and Mobility Laboratory, Ecole Polytechnique Fédérale de Lausanne (EPFL)
In Python, the levels of reporting are:
DEBUG
INFO
WARNING
ERROR
CRITICAL
In Biogeme, we basically use the first three.
If we request a specific level, all messages from this level and all levels above are displayed. For example, if INFO is requested, everything except DEBUG will be displayed.
logger = blog.get_screen_logger(level=blog.INFO)
logger.info('A test')
A test
If a debug message is generated, it is not displayed, as the INFO level has been requested above.
logger.debug('A debug message')
But a warning message is displayed, as it comes higher in the hierarchy.
logger.warning('A warning message')
A warning message
It is also possible to log the messages on file.
THE_FILE = '_test.log'
Let’s first erase the file if it happens to exist.
try:
os.remove(THE_FILE)
print(f'File {THE_FILE} has been erased.')
except FileNotFoundError:
print('File {THE_FILE} does not exist.')
File _test.log has been erased.
file_logger = blog.get_file_logger(filename=THE_FILE, level=blog.DEBUG)
file_logger.debug('A debug message')
file_logger.warning('A warning message')
file_logger.info('A info message')
A warning message
A info message
Here is the content of the log file. Note that the message includes the filename, which is not informative in the context of this Notebook.
with open(THE_FILE, encoding='utf-8') as f:
print(f.read())
[DEBUG] 2025-09-03 01:58:55,791 A debug message <plot_biogeme_logging.py:78>
[WARNING] 2025-09-03 01:58:55,792 A warning message <plot_biogeme_logging.py:79>
[INFO] 2025-09-03 01:58:55,792 A info message <plot_biogeme_logging.py:80>
Total running time of the script: (0 minutes 0.006 seconds)