zoukankan      html  css  js  c++  java
  • SysLogHandler not writing to syslog with Python logging

    SysLogHandler not writing to syslog with Python logging

    SysLogHandler not writing to syslog with Python logging

    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 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.

  • 相关阅读:
    二叉搜索树
    【树】List Leaves
    模板——dijkstra单源最短路
    余数求和——除法分块
    倍增——ST表
    线段树——内存池
    线段树——模板
    洛谷 P1498 南蛮图腾
    洛谷 P2199 最后的迷宫
    洛谷 P1495 中国剩余定理
  • 原文地址:https://www.cnblogs.com/lexus/p/2468002.html
Copyright © 2011-2022 走看看