logging in Django
Logging to console
1. Use Print
e.g.
def index(req):
print “hello!”
and you will see the ‘hello!’ on your console. (or apache log if running on linux)
2. Use Python logging feature
# in settings.py
import logging
logging.basicConfig(
level = logging.DEBUG,
format = ‘%(asctime)s %(levelname)s %(message)s’
)
# write log
import logging
logging.debug(‘hello’)
and you can also see the ‘hello’ on your console.
Logging to a file
# in settings.py
import logging
logging.basicConfig(
level = logging.DEBUG,
format = ‘%(asctime)s %(levelname)s %(message)s’,
filename = ‘/tmp/django.log’,
filemode=’w’
)
# write log
import logging
logging.debug(‘hello’)
you can see your ‘hello’ in the ‘/tmp’/django.log’ file
