How do I configure the Python logging module in Django?
Posted
by mipadi
on Stack Overflow
See other posts from Stack Overflow
or by mipadi
Published on 2010-06-02T19:35:35Z
Indexed on
2010/06/02
19:44 UTC
Read the original article
Hit count: 296
I'm trying to configure logging for a Django app using the Python logging
module. I have placed the following bit of configuration code in my Django project's settings.py
file:
import logging
import logging.handlers
import os
date_fmt = '%m/%d/%Y %H:%M:%S'
log_formatter = logging.Formatter(u'[%(asctime)s] %(levelname)-7s: %(message)s (%(filename)s:%(lineno)d)', datefmt=date_fmt)
log_dir = os.path.join(PROJECT_DIR, "var", "log", "my_app")
log_name = os.path.join(log_dir, "nyrb.log")
bytes = 1024 * 1024 # 1 MB
if not os.path.exists(log_dir):
os.makedirs(log_dir)
handler = logging.handlers.RotatingFileHandler(log_name, maxBytes=bytes, backupCount=7)
handler.setFormatter(log_formatter)
handler.setLevel(logging.DEBUG)
logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger().addHandler(handler)
logging.getLogger(__name__).info("Initialized logging subsystem")
At startup, I get a couple Django-related messages, as well as the "Initialized logging subsystem", in the log files, but then all the log messages end up going to the web server logs (/var/log/apache2/error.log
, since I'm using Apache), and use the standard log format (not the formatter I designated). Am I configuring logging incorrectly?
© Stack Overflow or respective owner