Задать вопрос

Тел: +7 965 3737 888

525

Просмотров

15

Ответов

django log

set LOG_FILE = "log.log" in the settings.py

Вопрос полезен? Да0/Нет0
file_4101.py(460.0б)
None

Ответы (15):

Ответparxier:21.03.2009
Ответ полезен? Да0/Нет0

guettli, you can use setattr/getattr functions, this way you don't have to do initialisation of set_up_done attribute at all:

# file logconfig.py
def set_up(myhome):
    if getattr(logging, 'set_up_done', False):
        return
    setattr(logging, 'set_up_done', True)

Ответemily-wong:26.02.2009
Ответ полезен? Да0/Нет0

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:05.11.2007
Ответ полезен? Да0/Нет0

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.

# file logconfig.py
if not hasattr(logging, "set_up_done"):
    logging.set_up_done=False

def set_up(myhome):
    if logging.set_up_done:
        return
    logging.set_up_done=True

Ответguettli:05.11.2007
Ответ полезен? Да0/Нет0

Hi,

for me, the workaround with threading.local() did not work. I use the solution now:

# logconfig.py (same directory as settings.py)
def set_up():
    root_logger=logging.getLogger()
    if len(root_logger.handlers)>1:
        return
    root_logger....

And in settings.py:

import logconfig
logconfig.set_up()

Ответnesh:12.03.2007
Ответ полезен? Да0/Нет0

I forgot, just above return logger:

setattr(_LOCALS, 'logger', logger)

Ответnesh:12.03.2007
Ответ полезен? Да0/Нет0

Maybe this is better:

# stdlib
import threading
import logging
# django
from django.conf import settings

#===============================================================================
# Globals
#===============================================================================
_LOCALS = threading.local()

def get_logger():
    logger = getattr(_LOCALS, 'logger', None)
    if logger is not None:
        return logger

    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(getattr(settings, 'LOG_LEVEL', logging.NOTSET))

    return logger

Ответnikto:07.03.2007
Ответ полезен? Да0/Нет0

andeol: yes that's what's happening and what you suggest is basically what I ended up doing.

Ответandeol:06.03.2007
Ответ полезен? Да0/Нет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.

Ответjejwe:05.03.2007
Ответ полезен? Да0/Нет0

setting.py(Configuration) :

LOG_FILE = "log.log" LOG_LEVEL = 40 #CRITICAL 50 ERROR 40 WARNING 30 INFO 20 DEBUG 10 NOTSET 0

Ответjejwe:05.03.2007
Ответ полезен? Да0/Нет0

Wrong It's LOG_FILE='log.txt'

Ответjejwe:05.03.2007
Ответ полезен? Да0/Нет0

Another Configuration settings.py:

settings.LOG_FILE='log.txt'

Ответmaplye:03.03.2007
Ответ полезен? Да0/Нет0

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.

Ответnikto:02.03.2007
Ответ полезен? Да0/Нет0

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.

Ответludvig.ericson:27.02.2007
Ответ полезен? Да0/Нет0

Nice. [HTML_REMOVED]"X"[HTML_REMOVED]

Ответjcroft:25.02.2007
Ответ полезен? Да0/Нет0

Looks like the language on this one should actually be "Python," not "Django Template."