New guys in Django commonly have a headache to see Logger configuration in settings.py. Sometimes people only want use Logger to print error / warning / debug in server development. Here are how to do that.
First, open up your settings.py and see at logger section :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # A sample logging configuration. The only tangible logging # performed by this configuration is to send an email to # the site admins on every HTTP 500 error. # See http://docs.djangoproject.com/en/dev/topics/logging for # more details on how to customize your logging configuration. LOGGING = { ‘version’: 1, ‘disable_existing_loggers’: False, ‘handlers’: { ‘mail_admins’: { ‘level’: ‘ERROR’, ‘class’: ‘django.utils.log.AdminEmailHandler’ } }, ‘loggers’: { ‘django.request’: { ‘handlers’: [‘mail_admins’], ‘level’: ‘ERROR’, ‘propagate’: True, }, } } |
There are two part that you should know, Handler and Loggers. Basically, this is python logging configuration. Really? you can read at http://docs.python.org/library/logging.config.html#configuration-dictionary-schema.
1. Handlers
This will handling incoming loggers. As defined in python logging config :
the handler with id console is instantiated as a logging.StreamHandler, using sys.stdout as the underlying stream
So, we create console handlers by :
1 2 3 4 5 6 7 8 9 10 11 | ‘handlers’: { ‘mail_admins’: { ‘level’: ‘ERROR’, ‘class’: ‘django.utils.log.AdminEmailHandler’ }, ‘console’: { ‘level’: ‘DEBUG’, ‘class’: ‘logging.StreamHandler’, ‘formatter’: ‘verbose’ }, }, |
2. Formatter
Wait, I see formatter here called “verbose”. What it is? Actually it’s about formatting print out logger result. For instance, you have :
1 | msg = logger.error("Some error") |
If there no formatter it will return “Some error”. But we want to show informative log, like :
1 2 3 4 | <level> <time> <module> <message> # Will produce: ERROR 2012-03-22 11:17:19 views Some error |
There are two example for formatter here :
1 2 3 4 5 6 7 8 | formatters’: { ‘verbose’: { ‘format’: ‘%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s’ }, ‘simple’: { ‘format’: ‘%(levelname)s %(asctime)s %(module)s %(message)s’ }, }, |
3. Logger
Then, the last thing is Logger, which we create name of logger and use console as handlers.
1 2 3 4 5 6 7 8 9 10 11 12 | ‘loggers’: { ‘django.request’: { ‘handlers’: [‘mail_admins’], ‘level’: ‘ERROR’, ‘propagate’: True, }, ‘app’: { ‘handlers’: [‘console’], ‘level’: ‘DEBUG’, ‘propagate’: True, }, } |
All of code :
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 | # A sample logging configuration. The only tangible logging # performed by this configuration is to send an email to # the site admins on every HTTP 500 error. # See http://docs.djangoproject.com/en/dev/topics/logging for # more details on how to customize your logging configuration. LOGGING = { ‘version’: 1, ‘disable_existing_loggers’: False, ‘formatters’: { ‘verbose’: { ‘format’: ‘%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s’ }, ‘simple’: { ‘format’: ‘%(levelname)s %(asctime)s %(module)s %(message)s’ }, }, ‘handlers’: { ‘mail_admins’: { ‘level’: ‘ERROR’, ‘class’: ‘django.utils.log.AdminEmailHandler’ }, ‘console’: { ‘level’: ‘DEBUG’, ‘class’: ‘logging.StreamHandler’, ‘formatter’: ‘verbose’ }, }, ‘loggers’: { ‘django.request’: { ‘handlers’: [‘mail_admins’], ‘level’: ‘ERROR’, ‘propagate’: True, }, ‘app’: { ‘handlers’: [‘console’], ‘level’: ‘DEBUG’, ‘propagate’: True, }, } } |
Now we are ready to test-drive. For example, run django server development. Then, choose some views.py and test your logger using “app” as logger.
Eg : views.py
1 2 3 4 5 6 7 8 9 10 | from django.shortcuts import render from django.utils.log import getLogger logger = getLogger(‘app’) def index(request): logger.warning("This is warning") logger.error("This is error") return render(request, ‘web/index.html’) |
Also make sure your enable DEBUG in settings.py. Now, you can see that logger show up in the console 🙂
For more detail explanation, you should read :
https://docs.djangoproject.com/en/dev/topics/logging/
Some advanced example :
https://gist.github.com/887364