Tony Wang
Tony Wang

Reputation: 1021

How to configure rsyslog to use the centralized log with Python

The rsyslog with elk runs well in a docker of localhost.

I could see the logs in Kibana with commands below:

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'

The questions here, I want to use rsyslog with my Python application. The demo code shows below with the same configuration of rsyslog.

But I couldn't get anything from the Python application. So what's wrong with my configuration or code?

10.211.55.12 is the IP address of my localhost

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

Upvotes: 2

Views: 1612

Answers (1)

Tony Wang
Tony Wang

Reputation: 1021

@VPfB's comment is right. Change logging.handlers.SysLogHandler('/dev/log') to logging.handlers.SysLogHandler() will work for my case. Thanks @VPfB!

Upvotes: 1

Related Questions