3

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

5
  • As the comments in rsyslog.conf specify, only messages with a priority of info or higher are logged. Does your program log such messages? Commented Sep 12, 2014 at 12:13
  • yes I've got self.logger.setLevel(logging.INFO) Commented Sep 12, 2014 at 12:17
  • Oh, I haven't noticed that. However, my comment still stands. logger.setLevel sets the threshold of the logger, but attempting to log messages which are less severe is still possible (for example, via logger.debug), though they are ignored. Can you attach an example of actual logging attempt of a message that you expect to find in the log? Commented Sep 12, 2014 at 12:29
  • Sure, I added a bit more code, plus I inserted a method into Logger class. Commented Sep 12, 2014 at 12:36
  • Your init function is not instructing to save it on file, but giving a "name" to your log registries. You should check the file of the rsyslog (/var/log/messages). Commented Sep 12, 2014 at 13:20

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I got this figured out now, thanks for the link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.