博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SysLogHandler not writing to syslog with Python logging
阅读量:6199 次
发布时间:2019-06-21

本文共 1496 字,大约阅读时间需要 4 分钟。

1 January 2008

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 syslogsyslog.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 , but it works.

For example:

import loggingfrom logging.handlers import SysLogHandlerlogger = 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.

转载地址:http://tevca.baihongyu.com/

你可能感兴趣的文章
干货!9种高性能可用高并发的技术架构
查看>>
Elasticsearch PHP MYSQL的同步使用
查看>>
关于生成二维码导致显示不出来 vivo手机出现问题解决办法
查看>>
模拟光链路 动态范围
查看>>
20天时间,一个人怎么搞定这个后台管理项目
查看>>
go并发基础数据加锁解锁
查看>>
6-SpringIOC原理
查看>>
北大教授邱泽奇:农村电商,为什么是菏泽?
查看>>
黄仁勋打响CES第一枪:全球最强芯DRIVE Xavier武装自动驾驶
查看>>
Tengine TLSv1.3最佳实践
查看>>
利用redis缓存对 list集合中的数据 进行分页操作
查看>>
云效(原RDC)如何构建一个基于Maven的Java项目
查看>>
RabbitMQ消息反序列化失败问题回顾
查看>>
Oracle 中实现查找树形结构节点功能
查看>>
aliyun 搭建redis集群
查看>>
java算法-二维极点算法
查看>>
总结-HttpClient-RetryHandler重试
查看>>
Java回调机制如何理解?
查看>>
mybatis mapper里面>= ,<=转义
查看>>
Android实时监听网络状态
查看>>