Изящная установка Python, входящего в систему Django

struct st* x = malloc( sizeof( struct st ));

94
задан Peter Mortensen 14 April 2010 в 04:21
поделиться

3 ответа

The best way I've found so far is to initialize logging setup in settings.py - nowhere else. You can either use a configuration file or do it programmatically step-by-step - it just depends on your requirements. The key thing is that I usually add the handlers I want to the root logger, using levels and sometimes logging.Filters to get the events I want to the appropriate files, console, syslogs etc. You can of course add handlers to any other loggers too, but there isn't commonly a need for this in my experience.

In each module, I define a logger using

logger = logging.getLogger(__name__)

and use that for logging events in the module (and, if I want to differentiate further) use a logger which is a child of the logger created above.

If my app is going to be potentially used in a site which doesn't configure logging in settings.py, I define a NullHandler somewhere as follows:

#someutils.py

class NullHandler(logging.Handler):
    def emit(self, record):
        pass

null_handler = NullHandler()

and ensure that an instance of it is added to all loggers created in the modules in my apps which use logging. (Note: NullHandler is already in the logging package for Python 3.1, and will be in Python 2.7.) So:

logger = logging.getLogger(__name__)
logger.addHandler(someutils.null_handler)

This is done to ensure that your modules play nicely in a site which doesn't configure logging in settings.py, and that you don't get any annoying "No handlers could be found for logger X.Y.Z" messages (which are warnings about potentially misconfigured logging).

Doing it this way meets your stated requirements:

  • You can set up different log handlers for different events, as you currently do.
  • Easy access to loggers in your modules - use getLogger(__name__).
  • Easily applicable to command-line modules - they also import settings.py.

Update: Note that as of version 1.3, Django now incorporates support for logging.

53
ответ дан 24 November 2019 в 06:05
поделиться

I am currently using a logging system, which I created myself. It uses CSV format for logging.

django-csvlog

This project still doesn't have full documentation, but I am working on it.

6
ответ дан 24 November 2019 в 06:05
поделиться

Мы инициализируем ведение журнала на urls.py верхнего уровня, используя файл logging.ini .

Местоположение ] logging.ini предоставляется в settings.py , но это все.

Каждый модуль затем выполняет

logger = logging.getLogger(__name__)

Чтобы различать экземпляры тестирования, разработки и производства, у нас есть разные файлы logging.ini . По большей части у нас есть «консольный журнал», который отправляется на stderr только с ошибками. У нас есть «журнал приложений», который использует обычный скользящий файл журнала, который идет в каталог журналов.

9
ответ дан 24 November 2019 в 06:05
поделиться
Другие вопросы по тегам:

Похожие вопросы: