{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Logging to terminal\n\nThis example demonstrates how to configure and use the logging system in AequilibraE.\n\nFor details of the logging setup used with AequilibraE see `logging_internals`.\n\nAequilibraE uses the standard Python `logging` module, but with specific configurations to ensure compatibility with:\n\n1. Progress bars (tqdm), so logs do not break the visual bars in the terminal/notebook.\n\n2. QGIS integration.\n\n3. Multithreaded C++/Cython extensions.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Basic Configuration\nBy default, AequilibraE does not configure the root logger to avoid interfering with\nother libraries or your own script's settings. To see AequilibraE logs in your\nterminal or notebook, you should use the provided helper.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import logging\nfrom aequilibrae.utils import basic_config"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "This helper installs a custom StreamHandler that writes to ``stdout`` via ``tqdm.write``,\nensuring that log messages print cleanly above active progress bars.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Configure logging to INFO level\nhandler = basic_config(level=logging.INFO)\n\n# Now we can log\nlogger = logging.getLogger(\"aequilibrae\")\nlogger.info(\"This is a standard info message.\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## How it works\nWhen :py:func:`aequilibrae.utils.logging_utils.basic_config` is called:\n\n1. It sets the level for the ``\"aequilibrae\"`` logger.\n2. It sets ``logger.propagate = False``. This prevents AequilibraE logs from bubbling up\n   to the root logger. This is crucial because standard root handlers might print directly\n   to streams that conflict with the terminal-based progress bars.\n3. It attaches an ``aequilibrae.utils.logging_utils.AequilibraEStreamHandler`` which preserves the terminal-based\n   progress bars.\n\nYou can customise the format and stream:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "logger.removeHandler(handler)\nbasic_config(level=logging.DEBUG, format=\"%(levelname)s: %(message)s\")\nlogger.debug(\"This is a debug message with a custom format.\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Note that we removed the first handler that ``basic_config`` added otherwise the message will appear twice. If the\n``\"aequilibrae\"`` has any handlers writing to standard out or standard error then ``basic_config`` will do not do\nanything.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Configuring Specific Modules\nAll AequilibraE logging happens under the ``\"aequilibrae\"`` namespace. You can fine-tune\nlogging for specific modules if you want to debug only one part of the system.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Example: Turn on DEBUG logging only for the paths module\npaths_logger = logging.getLogger(\"aequilibrae.routing\")\npaths_logger.setLevel(logging.DEBUG)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The parent ``\"aequilibrae\"`` logger might be set to WARNING, but this specific module\nwill now output DEBUG messages.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Scenario-Based Logging\nAequilibraE Projects handle logging automatically when switching scenarios.\n\nWhen you open a project athe Project\nattaches a specific `FileHandler` to the logger.\n\n* **One Log per Scenario:** If you run multiple scenarios, AequilibraE automatically\n  switches the log file handler to a new file located in the scenario directory.\n* **Auto-Switching:** You do not need to manually close or remove handlers; the\n  logging system handles the rotation to ensure logs from scenario A don't end up in scenario B's file.\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.18"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}