简体   繁体   中英

Python logging with rsyslog

I've inherited the following python file:

import logging
from logging.handlers import SysLogHandler

class Logger(object):

  # Return a logging instance used throughout the library
  def __init__(self):
    self.logger = logging.getLogger('my_daemon')

  # Log an info message
  def info(self, message, *args, **kwargs):
    self.__log(logging.INFO, message, *args, **kwargs)

  # Configure the logger to log to syslog
  def log_to_syslog(self):
    formatter = logging.Formatter('my_daemon: [%(levelname)s] %(message)s')

    handler = SysLogHandler(address='/dev/log', facility=SysLogHandler.LOG_DAEMON)
    handler.setFormatter(formatter)

    self.logger.addHandler(handler)
    self.logger.setLevel(logging.INFO)

I see that the init method looks for a logger called my_daemon , which I can't find anywhere on my system. Do I have to manually create the file and if so where should I put it?

Also, log_to_syslog appears to listen to socket /dev/log , and when I run sudo lsof /dev/log I get the following:

[vagrant@server]$ sudo lsof /dev/log
COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
rsyslogd 989 root    0u  unix 0xffff880037a880c0      0t0 8099 /dev/log

When I look at /etc/rsyslog.conf I see the following:

# rsyslog v5 configuration file

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none                /var/log/messages

So I'm a bit lost here. My init function seems to be instructing python to use a logfile called my_daemon , which I can't find anywhere, and /etc/rsyslog.conf seems to be telling the machine to use /var/log/messages, which does not contain any logs from my app.

Update: here's how I'm trying to log messages

import os
from logger import Logger

class Server(object):

  def __init__(self, options):
    self.logger = Logger()

  def write(self, data):
    self.logger.info('Received new data from controller, applying')
    print 'hello'

The write method from server.py does print 'hello' to the screen so I know we're getting close, it's just the logger that's not doing what I would expect

my_daemon is not a log file - it is just a developer-specified name indicating an "area" in an application. See this information on what loggers are.

The log_to_syslog is not listening on a socket - the rsyslog daemon is doing that. Also, I don't see where log_to_syslog is called - is it? If it isn't called, that would explain why no messages are being seen in the syslog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM