.. _django:

Using with Django
=================

Django has a default logging configuration that executes when the application initializes.
This configuration is defined in the ``LOGGING`` variable in the ``settings.py`` file. By default, Django will
configure a set of streaming loggers from the standard library logging module.

To use picologging with Django, you need to change some settings.

1. Set ``LOGGING_CONFIG`` to ``None`` in ``settings.py``. This will prevent Django from configuring the default logging system.
2. Change the handler classes from ``logging.xxx`` to ``picologging.xxx``, e.g. ``logging.StreamHandler`` to ``picologging.StreamHandler``.
3. Call ``picologging.config.dictConfig(LOGGING)`` in ``settings.py`` to configure picologging.
4. Change your imports where logging is used to ```import picologging as logging``.

Here is a complete example of ``settings.py``

.. code-block: python

    LOGGING = {
        "version": 1,
        "disable_existing_loggers": False,
        "handlers": {
            "console": {
                "class": "picologging.StreamHandler",
            },
        },
        "root": {
            "handlers": ["console"],
            "level": "WARNING",
        },
    }
    LOGGING_CONFIG = None
    import picologging.config
    picologging.config.dictConfig(LOGGING)

Then in a view to use those loggers:

.. code-block: python

    import picologging as logging

    logger = logging.getLogger(__name__)

    def my_view(request, arg1, arg):
        logger.info("Logging in my_view")
        return ...