简体   繁体   English

Django:我如何让日志记录工作?

[英]Django: How do I get logging working?

I've added the following to my settings.py file: 我已将以下内容添加到我的settings.py文件中:

import logging
...
logging.basicConfig(level=logging.DEBUG,
    format='%(asctime)s %(levelname)s %(message)s',
    filename=os.path.join(rootdir, 'django.log'),
    filemode='a+')

And in views.py, I've added: 在views.py中,我添加了:

import logging
log = logging.getLogger(__name__)
...
log.info("testing 123!")

Unfortunately, no log file is being created. 不幸的是,没有创建日志文件。 Any ideas what I am doing wrong? 我有什么想法我做错了吗? And also is their a better method I should be using for logging? 还有他们应该用于记录的更好的方法吗? I am doing this on Webfaction. 我在Webfaction上这样做。

Python logging for Django is fine on somewhere like Webfaction. Django的Python日志记录在像Webfaction这样的地方很好。 If you were on a cloud-based provider (eg Amazon EC2) where you had a number of servers, it might be worth looking at either logging to key-value DB or using Python logging over the network. 如果您使用的是基于云的提供商(例如Amazon EC2),那里有许多服务器,那么可能值得查看登录到键值数据库或使用网络上的Python日志记录。

Your logging setup code in settings.py looks fine, but I'd check that you can write to rootdir -- your syslog might show errors, but it's more likely that Django would be throwing a 500 if it couldn't log properly. 您在settings.py中的日志记录设置代码看起来很好,但我会检查您是否可以写入rootdir - 您的系统日志可能会显示错误,但如果Django无法正确记录,则更有可能抛出500。

Which leads me to note that the only major difference in my logging (also on WebFaction) is that I do: 这让我注意到我的日志记录(也是WebFaction)的唯一主要区别在于:

import logging
logging.info("Something here") 

instead of log.info 而不是log.info

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM