使用rsyslog进行Python日志记录 [英] Python logging with rsyslog
问题描述
我继承了以下python文件:
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)
我看到init方法正在寻找一个名为 my_daemon 的记录器,该记录器在系统上找不到任何位置.我必须手动创建文件吗?如果可以,我应该将其放在哪里?
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?
此外,log_to_syslog
似乎在监听套接字/dev/log
,当我运行sudo lsof /dev/log
时,我得到以下信息:
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
当我查看/etc/rsyslog.conf
时,会看到以下内容:
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
所以我在这里有点迷路了.我的init函数似乎正在指示python使用名为 my_daemon 的日志文件,该文件在任何地方都找不到,并且/etc/rsyslog.conf似乎正在告诉机器使用/var/log/消息,其中不包含我的应用程序中的任何日志.
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'
server.py中的write方法确实在屏幕上显示了"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
不是日志文件-它只是开发人员指定的名称,指示应用程序中的区域".请参阅此有关什么是记录器的信息.
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.
log_to_syslog
没有在套接字上侦听-rsyslog守护程序正在这样做.此外,我看不到log_to_syslog
的名称-是吗?如果未调用,则可以解释为什么在系统日志中看不到任何消息.
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.
这篇关于使用rsyslog进行Python日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!