Instantly share code, notes, and snippets.

Embed
What would you like to do?
Django Logging Variations

Default Django Logging Tree

app.py

#!/usr/bin/env python
import os

import django
import logging_tree

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app')

SECRET_KEY = 'not-secret'

if __name__ == '__main__':
    django.setup()
    logging_tree.printout()

Output

<--""
   Level WARNING
   |
   o<--"django"
       Level INFO
       Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
         Level INFO
         Filter <django.utils.log.RequireDebugTrue object at 0x102853c18>
       Handler <AdminEmailHandler (ERROR)>
         Level ERROR
         Filter <django.utils.log.RequireDebugFalse object at 0x102853c88>
       |
       o<--[django.db]
       |   |
       |   o<--"django.db.backends"
       |       Level NOTSET so inherits level INFO
       |
       o<--"django.request"
       |   Level NOTSET so inherits level INFO
       |
       o   "django.server"
       |   Level INFO
       |   Propagate OFF
       |   Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
       |     Level INFO
       |     Formatter <django.utils.log.ServerFormatter object at 0x102853a20>
       |
       o<--"django.template"
           Level NOTSET so inherits level INFO

Disable Existing Logging Tree

app.py

#!/usr/bin/env python
import os

import django
import logging_tree

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app')

SECRET_KEY = 'not-secret'
LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
}

if __name__ == '__main__':
    django.setup()
    logging_tree.printout()

Output

<--""
   Level WARNING
   |
   o<--"django"
       Level INFO
       Disabled
       Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
         Level INFO
         Filter <django.utils.log.RequireDebugTrue object at 0x109bd5c50>
       Handler <AdminEmailHandler (ERROR)>
         Level ERROR
         Filter <django.utils.log.RequireDebugFalse object at 0x109bd5cc0>
       |
       o<--[django.db]
       |   |
       |   o<--"django.db.backends"
       |       Level NOTSET so inherits level INFO
       |       Disabled
       |
       o<--"django.request"
       |   Level NOTSET so inherits level INFO
       |   Disabled
       |
       o   "django.server"
       |   Level INFO
       |   Propagate OFF
       |   Disabled
       |   Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
       |     Level INFO
       |     Formatter <django.utils.log.ServerFormatter object at 0x109bd5a58>
       |
       o<--"django.template"
           Level NOTSET so inherits level INFO
           Disabled

Redefine Logger Logging Tree

app.py

#!/usr/bin/env python
import os

import django
import logging_tree

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app')

SECRET_KEY = 'not-secret'
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'INFO',
        },
    }
}

if __name__ == '__main__':
    django.setup()
    logging_tree.printout()

Output

<--""
   Level WARNING
   |
   o<--"django"
       Level INFO
       Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
         Level INFO
       |
       o<--[django.db]
       |   |
       |   o<--"django.db.backends"
       |       Level NOTSET so inherits level INFO
       |
       o<--"django.request"
       |   Level NOTSET so inherits level INFO
       |
       o<--"django.server"
       |   Level NOTSET so inherits level INFO
       |
       o<--"django.template"
           Level NOTSET so inherits level INFO

Copy Default Logging Tree

app.py

#!/usr/bin/env python
import os
from copy import deepcopy

import django
from django.utils.log import DEFAULT_LOGGING
import logging_tree

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app')

SECRET_KEY = 'not-secret'

logging_dict = deepcopy(DEFAULT_LOGGING)
logging_dict['loggers']['django']['handlers'] = ['console']
LOGGING = logging_dict

if __name__ == '__main__':
    django.setup()
    logging_tree.printout()

Output

<--""
   Level WARNING
   |
   o<--"django"
       Level INFO
       Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
         Level INFO
         Filter <django.utils.log.RequireDebugTrue object at 0x107d8ff28>
       |
       o<--[django.db]
       |   |
       |   o<--"django.db.backends"
       |       Level NOTSET so inherits level INFO
       |
       o<--"django.request"
       |   Level NOTSET so inherits level INFO
       |
       o   "django.server"
       |   Level INFO
       |   Propagate OFF
       |   Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
       |     Level INFO
       |     Formatter <django.utils.log.ServerFormatter object at 0x107d8fd68>
       |
       o<--"django.template"
           Level NOTSET so inherits level INFO

LOGGING_CONFIG = None Logging Tree

app.py

#!/usr/bin/env python
import logging.config
import os

import django
import logging_tree

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app')

SECRET_KEY = 'not-secret'
LOGGING_CONFIG = None
logging.config.dictConfig({
    "version": 1,
    "disable_existing_loggers": False,
})

if __name__ == '__main__':
    django.setup()
    logging_tree.printout()

Output

<--""
   Level WARNING
   |
   o<--[django]
       |
       o<--[django.db]
       |   |
       |   o<--"django.db.backends"
       |       Level NOTSET so inherits level WARNING
       |
       o<--"django.request"
       |   Level NOTSET so inherits level WARNING
       |
       o<--"django.template"
           Level NOTSET so inherits level WARNING
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment