.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/programmers/plot_biogeme_logging.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_programmers_plot_biogeme_logging.py: 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. :author: Michel Bierlaire :date: Wed Nov 22 13:52:20 2023 .. GENERATED FROM PYTHON SOURCE LINES 17-21 .. code-block:: default import os import biogeme.version as ver import biogeme.biogeme_logging as blog .. GENERATED FROM PYTHON SOURCE LINES 22-23 Version of Biogeme. .. GENERATED FROM PYTHON SOURCE LINES 23-25 .. code-block:: default print(ver.getText()) .. rst-class:: sphx-glr-script-out .. code-block:: none biogeme 3.2.13 [2023-12-23] 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) .. GENERATED FROM PYTHON SOURCE LINES 26-35 In Python, the levels of reporting are: - DEBUG - INFO - WARNING - ERROR - CRITICAL In Biogeme, we basically use the first three. .. GENERATED FROM PYTHON SOURCE LINES 37-40 If we request a specific level, all message from this level and all levels above are displayed. For example, if INFO is requested, everything except DEBUG will be diplayed. .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: default logger = blog.get_screen_logger(level=blog.INFO) .. GENERATED FROM PYTHON SOURCE LINES 44-46 .. code-block:: default logger.info('A test') .. rst-class:: sphx-glr-script-out .. code-block:: none A test .. GENERATED FROM PYTHON SOURCE LINES 47-49 If a debug message is generated, it is not displayed, as the INFO level has been requested above. .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: default logger.debug('A debug message') .. GENERATED FROM PYTHON SOURCE LINES 54-55 But a warning message is displayed, as it comes higher in the hierarchy. .. GENERATED FROM PYTHON SOURCE LINES 57-59 .. code-block:: default logger.warning('A warning message') .. rst-class:: sphx-glr-script-out .. code-block:: none A warning message .. GENERATED FROM PYTHON SOURCE LINES 60-61 It is also possible to log the messages on file. .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: default THE_FILE = '_test.log' .. GENERATED FROM PYTHON SOURCE LINES 66-67 Let's first erase the file if it happens to exist. .. GENERATED FROM PYTHON SOURCE LINES 67-73 .. code-block:: default try: os.remove(THE_FILE) print(f'File {THE_FILE} has been erased.') except FileNotFoundError: print('File {THE_FILE} does not exist.') .. rst-class:: sphx-glr-script-out .. code-block:: none File {THE_FILE} does not exist. .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: default file_logger = blog.get_file_logger(filename=THE_FILE, level=blog.DEBUG) .. GENERATED FROM PYTHON SOURCE LINES 77-81 .. code-block:: default file_logger.debug('A debug message') file_logger.warning('A warning message') file_logger.info('A info message') .. rst-class:: sphx-glr-script-out .. code-block:: none A warning message A info message .. GENERATED FROM PYTHON SOURCE LINES 82-85 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. .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: default with open(THE_FILE, encoding='utf-8') as f: print(f.read()) .. rst-class:: sphx-glr-script-out .. code-block:: none [DEBUG] 2023-12-23 19:09:29,627 A debug message [WARNING] 2023-12-23 19:09:29,627 A warning message [INFO] 2023-12-23 19:09:29,627 A info message .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.002 seconds) .. _sphx_glr_download_auto_examples_programmers_plot_biogeme_logging.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_biogeme_logging.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_biogeme_logging.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_