首页IT科技syslog和snmp区别(SysLogHandler not writing to syslog with Python logging)

syslog和snmp区别(SysLogHandler not writing to syslog with Python logging)

时间2025-04-28 03:52:05分类IT科技浏览3137
导读:SysLogHandler not writing to syslog with Python logging...

SysLogHandler not writing to syslog with Python logging

Logging to syslog in Python

I was trying to use the standard Python logging module to write messages to syslog. The logging module has a SysLogHandler class which can log to a local or remote syslog daemon.

With no host specified, SysLogHandler uses localhost which is what I wanted. I tried to use SysLogHandler, but it just wouldn’t work. There was no error when I called the logging methods, but my messages didn’t show up in /var/log/syslog.

syslog module works

Python also has a standard syslog module. I tried it and it worked fine; my messages were written to the syslog file.

For example:

import syslog syslog.syslog(test)

syslogd isn’t listening

After running Wireshark I found the SysLogHandler was correctly sending a UDP packet to localhost on port 514. I could also see there was an ICMP response indicating the UDP packet was not received on that port. syslog wasn’t listening!

Use /dev/log

Instead of sending to localhost, I wanted SysLogHandler to pass the message to syslog on the local machine in the same way the syslog Python module was doing.

The solution is to pass /dev/log as the address parameter to SysLogHandler. It’s not well documented, but it works.

For example:

import logging from logging.handlers import SysLogHandler logger = logging.getLogger() logger.setLevel(logging.INFO) syslog = SysLogHandler(address=/dev/log) formatter = logging.Formatter(%(name)s: %(levelname)s %(message)s) syslog.setFormatter(formatter) logger.addHandler(syslog)

Easy when you know how.

声明:本站所有文章          ,如无特殊说明或标注              ,均为本站原创发布         。任何个人或组织     ,在未征得本站同意时          ,禁止复制         、盗用               、采集     、发布本站内容到任何网站    、书籍等各类媒体平台               。如若本站内容侵犯了原著者的合法权益              ,可联系我们进行处理     。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
如何提升网站搜索排名的方法(提升网站收录的10个实用技巧) newaletter(newaliases命令 – 文字模式转换)