问题描述
我还没有找到一种使用Django设置Python日志记录的方式,我很高兴。我的要求很简单:
- 不同的事件的不同的日志处理程序 - 也就是说,我想要能够登录到不同的文件
-
- 轻松访问我的模块中的记录器。该模块应该能够轻松找到它的记录器。
- 应该很容易适用于命令行模块。系统的一部分是独立的命令行或守护进程。记录应该可以很容易地与这些模块一起使用。
我目前的设置是使用 logging.conf
文件和设置日志记录在我登录的每个模块。感觉不对
您是否拥有自己喜欢的日志记录设置?请详细说明:您如何设置配置(您是否使用 logging.conf
或设置代码),何时/何时启动记录器,以及如何您可以在模块等中访问它们。
目前我发现的最好的方法是初始化日志记录设置在settings.py - 其他地方。您可以使用配置文件或以编程方式逐步执行 - 它只取决于您的要求。关键是我通常添加我需要的处理程序到根记录器,使用级别和有时logging.Filter来获取我想要的相应的文件,控制台,syslogs等事件。你当然可以添加处理程序到任何其他记录器但是在我的经验中,通常不需要这样做。
在每个模块中,我使用
$ b定义一个记录器$ b
logger = logging.getLogger(__ name__)
并使用它来记录模块中的事件(如果我想进一步区分)使用上面创建的记录器的孩子的记录器。
如果我的应用程序将被用于不配置登录settings.py的站点,我在某处定义了一个NullHandler,如下所示:
#someutils.py
class NullHandler(logging.Handler):
def emit(self,record):
pass
null_handler = NullHandler()
and ensur e的一个实例被添加到我使用日志记录的应用程序模块中创建的所有日志记录。 (注意:NullHandler已经在Python 3.1的日志记录包中,并且将在Python 2.7中。)所以:
logger = log.getLogger(__ name__)
logger.addHandler(someutils.null_handler)
以确保您的模块在不配置登录settings.py的站点中很好地播放,并且您不会遇到任何烦人的无法找到记录器XYZ的消息(这是有关可能配置错误的日志记录的警告)
这样做符合您的要求:
- 您可以设置为您的不同事件提供不同的日志处理程序,如您所做。
- 轻松访问模块中的记录器 - 使用
getLogger(__ name __)
。 - 轻松适用于命令行模块 - 它们还导入
settings.py
。 - Different log handlers for different events - that is, I want to be able to log to different files
- Easy access to loggers in my modules. The module should be able to find its logger with little effort.
- Should be easily applicable to command-line modules. Parts of the system are stand-alone command line or daemon processes. Logging should be easily usable with these modules.
更新:请注意,从1.3版开始,Django现在整合了。
I have yet to find a way of setting up Python logging with Django that I'm happy with. My requirements are fairly simple:
My current setup is to use a logging.conf
file and setup logging in each module I log from. It doesn't feel right.
Do you have a logging setup that you like? Please detail it: how do you setup the configuration (do you use logging.conf
or set it up in code), where/when do you initiate the loggers, and how do you get access to them in your modules, etc.
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.
这篇关于在Django中优化Python日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!