如何配置rsyslog以使用Python集中日志

时间:2017-05-06 05:43:08

标签: python logging rsyslog

带有elk的rsyslog在localhost的docker中运行良好。

我可以通过以下命令在Kibana中看到日志:

logger -n localhost 'log message from test99'
logger -n localhost 'log message from test99'
logger -n 10.211.55.12 'log message from test99'
logger -n 10.211.55.12 'log message from test99234234'

这里的问题,我想在我的Python应用程序中使用rsyslog。演示代码如下所示,具有相同的rsyslog配置。

但是我无法从Python应用程序中获得任何东西。那么我的配置或代码有什么问题呢?

10.211.55.12是我的localhost的IP地址

log_test.py

import logging
import logging.handlers

logger = logging.getLogger('myLogger')
logger.setLevel(logging.INFO)

# Add handler to the logger
handler = logging.handlers.SysLogHandler('/dev/log')

# Add formatter to the handler
formatter = logging.Formatter('Python: { "loggerName":"%(name)s", "asciTime":"%(asctime)s", "pathName":"%(pathname)s", "logRecordCreationTime":"%(created)f", "functionName":"%(funcName)s", "levelNo":"%(levelno)s", "lineNo":"%(lineno)d", "time":"%(msecs)d", "levelName":"%(levelname)s", "message":"%(message)s"}')

handler.formatter = formatter
logger.addHandler(handler)
for _ in range(100):
    logger.info("Test Message")

rsyslog.conf

 47 $DirCreateMode 0755
 48 $Umask 0022
 49 $PrivDropToUser syslog
 50 $PrivDropToGroup syslog
 51
 52 #
 53 # Where to place spool and state files
 54 #
 55 $WorkDirectory /var/spool/rsyslog
 56
 57 #
 58 # Include all configuration files in /etc/rsyslog.d/
 59 #
 60 $IncludeConfig /etc/rsyslog.d/*.conf
 61 *.* 10.211.55.12:514
 62
 63
 64 # Log anything (except mail) of level info or higher.
 65
 66 # Don't log private authentication messages!
 67
 68 *.info;mail.none;authpriv.none;cron.none      /var/log/messages
 69
 70 # The authpriv file has restricted access.
 71
 72 authpriv.*                                    /var/log/secure
 73
 74 # Log all the mail messages in one place.
 75
 76 mail.*                                        /var/log/maillog
 77
 78 # Log cron stuff
 79
 80 cron.*                                        /var/log/cron
 81
 82 # Everybody gets emergency messages
 83
 84 *.emerg                                       *
 85
 86 # Save news errors of level crit and higher in a special file.
 87
 88 uucp,news.crit                                /var/log/spooler
 89
 90 # Save boot messages also to boot.log
 91
 92 local7.*                                      /var/log/boot.log

1 个答案:

答案 0 :(得分:1)

@VPfB的评论是对的。将logging.handlers.SysLogHandler('/ dev / log')更改为logging.handlers.SysLogHandler()将适用于我的情况。谢谢@VPfB!