Simple Log to File example for django 1.3+

The release notes say:

Django 1.3 adds basic support for the Pythons registry module.

It's good. I would like to take advantage of this. Unfortunately, the documentation does not give me everything on a silver platter in the form of a complete working example code that demonstrates how simple and valuable it is.

How to set up this funky new feature so that I can port my code using

logging.debug('really awesome stuff dude: %s' % somevar) 

and look at the file "/tmp/application.log" by filling out

 18:31:59 Apr 21 2011 awesome stuff dude: foobar 18:32:00 Apr 21 2011 awesome stuff dude: foobar 18:32:01 Apr 21 2011 awesome stuff dude: foobar 

What is the difference between the default Python protocol and support for this level?

+84
django django-settings
Apr 21 '11 at 5:12
source share
2 answers

I really love it so much, here is your working example! Seriously, this is awesome!

Start by placing this in settings.py

 LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'formatters': { 'standard': { 'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s", 'datefmt' : "%d/%b/%Y %H:%M:%S" }, }, 'handlers': { 'null': { 'level':'DEBUG', 'class':'django.utils.log.NullHandler', }, 'logfile': { 'level':'DEBUG', 'class':'logging.handlers.RotatingFileHandler', 'filename': SITE_ROOT + "/logfile", 'maxBytes': 50000, 'backupCount': 2, 'formatter': 'standard', }, 'console':{ 'level':'INFO', 'class':'logging.StreamHandler', 'formatter': 'standard' }, }, 'loggers': { 'django': { 'handlers':['console'], 'propagate': True, 'level':'WARN', }, 'django.db.backends': { 'handlers': ['console'], 'level': 'DEBUG', 'propagate': False, }, 'MYAPP': { 'handlers': ['console', 'logfile'], 'level': 'DEBUG', }, } } 

Now what does that mean?

  • Formatters I like that he comes out in the same style as. /manage.py runningerver
  • Handlers. I want two logs - a debug text file and an information console. This allows me to really dig around (if necessary) and watch a text file to see what is happening under the hood.
  • Loggers is where we nail what we want to record. In general, django gets WARN and higher β€” an exception (hence a redistributable) β€”this is the backends where I like to see SQL calls, since they can go crazy .. My last application: I have two handlers and click on it all.

Now how do I enable the use of MYAPP ...

In the documentation, put this at the top of your files (views.py) ..

 import logging log = logging.getLogger(__name__) 

Then, to do something, do it.

 log.debug("Hey there it works!!") log.info("Hey there it works!!") log.warn("Hey there it works!!") log.error("Hey there it works!!") 

Log levels are explained here and for pure python here .

+157
Aug 12 '11 at 20:30
source share

Based in part on the registration configuration proposed by rh0dium and a few more studies , I did it myself, I began to build an example Django project with good registration standards - fail-nicely-django .

Example output log file:

 2016-04-05 22:12:32,984 [Thread-1 ] [INFO ] [djangoproject.logger] This is a manually logged INFO string. 2016-04-05 22:12:32,984 [Thread-1 ] [DEBUG] [djangoproject.logger] This is a manually logged DEBUG string. 2016-04-05 22:12:32,984 [Thread-1 ] [ERROR] [django.request ] Internal Server Error: / Traceback (most recent call last): File "/Users/kermit/.virtualenvs/fail-nicely-django/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response response = self.process_exception_by_middleware(e, request) File "/Users/kermit/.virtualenvs/fail-nicely-django/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/kermit/projekti/git/fail-nicely-django/djangoproject/brokenapp/views.py", line 12, in brokenview raise Exception('This is an exception raised in a view.') Exception: This is an exception raised in a view. 

The detailed usage is explained in README , but essentially, you copy logger to your Django project and add from .logger import LOGGING at the bottom of your settings.py.

+1
Apr 05 '16 at 19:31
source share



All Articles