1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
.. _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 ...
|