简体   繁体   English

python防止日志记录将日志写入syslog

[英]python prevent logging writing log to syslog

This is my logging setting. 这是我的日志记录设置。 It works fine and I can find log in test.log but I also find log in /var/log/messages something looks like 它工作正常,我可以在test.log中找到日志,但是在/var/log/messages也可以找到日志,看起来像

Dec  9 16:13:39 ip-10-80-48-6 python: DEBUG:__main__:Test Log

I don't want to log to /var/log/messages maybe other path is better because the log is too big 我不想登录到/var/log/messages也许其他路径更好,因为日志太大

import logging
logger = logging.getLogger(__name__)
logging.basicConfig()
logger.setLevel(logging.DEBUG)

handler = logging.handlers.RotatingFileHandler('test.log', "a", maxBytes=50000000, backupCount=10)
handler.setLevel(logging.DEBUG)
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))

logger.addHandler(handler)

#Update #更新

This code is in my twisted service I use systemctl to start this service 这段代码在我的扭曲服务中,我使用systemctl启动此服务

Refer below links. 请参考以下链接。 Explained with examples. 举例说明。

https://docs.python.org/2.7/howto/logging-cookbook.html#logging-to-multiple-destinations https://docs.python.org/2.7/howto/logging-cookbook.html#logging-to-multiple-destinations

https://docs.python.org/3/howto/logging-cookbook.html#logging-to-multiple-destinations . https://docs.python.org/3/howto/logging-cookbook.html#logging-to-multiple-destinations

In the example (Explained in the link), they have redirected output to a file and console. 在示例(在链接中解释)中,他们已将输出重定向到文件和控制台。 If your requirement is to redirect to some other file instead of /var/log/messages; 如果您的要求是重定向到其他文件而不是/ var / log / messages; pass "test.log" as parameter to basicConfig() function and create one more handler for syslog as below. 将“ test.log”作为参数传递给basicConfig()函数,并为syslog创建另一个处理程序,如下所示。

sysloghandler = logging.handlers.SysLogHandler("/dev/log") 
sysloghandler.setLevel(logging.INFO) 
syslogformatter = logging.Formatter('%(name)s: %(level)s %(message)s') 
sysloghandler.setFormatter(syslogformatter) 
logging.getLogger('myapp').addHandler(sysloghandler)
logging.basicConfig()

is what obviously sets a standard handler that logs to /var/log/messages Try removing it or simply remove all handlers before adding yours. 显然是设置了登录到/ var / log / messages的标准处理程序的原因尝试将其删除,或者在添加您的处理程序之前简单地删除所有处理程序。 I have never used python :) 我从没用过python :)

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

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