Login

Reusable Logging

Author:
mihasya
Posted:
February 28, 2009
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

"Thus, if a LOGGER is configured inside settings.py, we use that. Otherwise, we just use vanilla logging functions with the global logging configuration. Nice and sweet."

Naturally, the logger can be anything described here, I'm just using the RotatingFileHandler as an example because that's what I was using in my project.

Full write up+shamless plug here

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#in settings.py

LOG_FILENAME = '/var/log/somefile.log'

logger = logging.getLogger('foo')
logger.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(filename=LOG_FILENAME, maxBytes=1000000000, backupCount=5)
FORMAT = "[ %(asctime)s  %(levelname)s ] %(message)s"
formatter = logging.Formatter(FORMAT)
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)

LOGGER = logger

#to use in apps/modules:
try:
    logging = settings.LOGGERG
except AttributeError:
    import logging

More like this

  1. Add Toggle Switch Widget to Django Forms by OgliariNatan 2 weeks, 1 day ago
  2. get_object_or_none by azwdevops 4 months, 1 week ago
  3. Mask sensitive data from logger by agusmakmun 6 months ago
  4. Template tag - list punctuation for a list of items by shapiromatron 1 year, 8 months ago
  5. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 8 months ago

Comments

Please login first before commenting.