Jus*_*ant 108 python django logging
print当我在运行我的Django应用程序时manage.py runserver,如何将跟踪消息发送到控制台(例如),但是当我在Apache下运行应用程序时将这些消息发送到日志文件?
我回顾了Django日志记录,虽然我对其高级用途的灵活性和可配置性印象深刻,但我仍然对如何处理我的简单用例感到困惑.
m01*_*m01 99
这是一个基于Django日志记录的解决方案.它使用DEBUG设置而不是实际检查您是否正在运行开发服务器,但是如果您找到更好的方法来检查它应该很容易适应.
LOGGING = {
'version': 1,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/path/to/your/file.log',
'formatter': 'simple'
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': True,
},
}
}
if DEBUG:
# make all loggers use the console.
for logger in LOGGING['loggers']:
LOGGING['loggers'][logger]['handlers'] = ['console']
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅https://docs.djangoproject.com/en/dev/topics/logging/.
Ign*_*ams 81
在mod_wsgi下运行时,打印到stderr的文本将显示在httpd的错误日志中.您可以print直接使用,也可以使用logging.
print >>sys.stderr, 'Goodbye, cruel world!'
Run Code Online (Sandbox Code Playgroud)
Ben*_*tin 26
您可以在settings.py文件中配置日志记录.
一个例子:
if DEBUG:
# will output to your console
logging.basicConfig(
level = logging.DEBUG,
format = '%(asctime)s %(levelname)s %(message)s',
)
else:
# will output to logging file
logging.basicConfig(
level = logging.DEBUG,
format = '%(asctime)s %(levelname)s %(message)s',
filename = '/my_log_file.log',
filemode = 'a'
)
Run Code Online (Sandbox Code Playgroud)
然而,这取决于设置DEBUG,也许你不想担心它是如何设置的.请参阅此答案如何判断我的Django应用程序是否在开发服务器上运行?为了更好的写条件的方式.编辑:上面的示例来自Django 1.1项目,Django中的日志记录配置自该版本以来有所改变.
| 归档时间: |
|
| 查看次数: |
116752 次 |
| 最近记录: |