- Author:
- maplye
- Posted:
- February 25, 2007
- Language:
- Python
- Version:
- Pre .96
- Score:
- 8 (after 14 ratings)
set LOG_FILE = "log.log" in the settings.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import logging
from django.conf import settings
def getlogger():
logger = logging.getLogger()
hdlr = logging.FileHandler(settings.LOG_FILE)
formatter = logging.Formatter('[%(asctime)s]%(levelname)-8s"%(message)s"','%Y-%m-%d %a %H:%M:%S')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.NOTSET)
return logger
def debug(msg):
logger = getlogger()
logger.debug(msg)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 4 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Looks like the language on this one should actually be "Python," not "Django Template."
#
Nice. "X"
#
Maybe I'm not getting how to use this. But for me each time I call debug("some message") it adds another copy of the handler so it get dupli-tripli-quadrification of output in the log file.
#
i use this so that: 1.copy the code to a log.py. 2.import the log module when you use it in your code. 3.debug it.
#
Another Configuration settings.py:
settings.LOG_FILE='log.txt'
#
Wrong It's LOG_FILE='log.txt'
#
setting.py(Configuration) :
LOG_FILE = "log.log" LOG_LEVEL = 40 #CRITICAL 50 ERROR 40 WARNING 30 INFO 20 DEBUG 10 NOTSET 0
#
nikto: the reason that you get duplicates is that each call to getLogger adds a filehandler to the logger so that for example when the fifth message is printed the there are five filehandlers each writing to the same file resulting in five duplicates of the message.
Does this code actually work for anyone?
I would suggest using a module level variable to keep track of if the logger already has been configured, to avoid doing it more than once.
#
andeol: yes that's what's happening and what you suggest is basically what I ended up doing.
#
Maybe this is better:
#
I forgot, just above return logger:
#
Hi,
for me, the workaround with threading.local() did not work. I use the solution now:
And in settings.py:
#
The above solution works, since I insert two loggers into the root_logger. It is a hack. Remember: If you call logging.log(...) before the root logger contains a handler, one handler is added automatically.
The trick with the global variable _LOCALS does not work for me, since the module gets imported twice with different names (one time __name__ is "logconfig" and the second time it is "myproject.logconfig"). I guess, that python thinks that both are different modules/files.
A better solution is to use the namespace of a module, that does not get imported twice with different __name__s.
Here is a better solution.
#
I found that using the above the debug messages don't get written to log without first stopping the server.
Instead I passed the file handler back to debug function and just close the file handler each time with hdlr.close().
#
guettli, you can use setattr/getattr functions, this way you don't have to do initialisation of set_up_done attribute at all:
#
Please login first before commenting.