问题:如何在Python中将日志记录配置为syslog?
我无法理解Python的logging模块。我的需求非常简单:我只想将所有内容记录到syslog中。阅读文档后,我想到了这个简单的测试脚本:
import logging
import logging.handlers
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler()
my_logger.addHandler(handler)
my_logger.debug('this is debug')
my_logger.critical('this is critical')
但是此脚本不会在syslog中产生任何日志记录。怎么了?
回答 0
将行更改为此:
handler = SysLogHandler(address='/dev/log')这对我有用
import logging
import logging.handlers
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(address = '/dev/log')
my_logger.addHandler(handler)
my_logger.debug('this is debug')
my_logger.critical('this is critical')回答 1
无论是通过TCP堆栈登录到/ dev / log还是localhost,都应始终使用本地主机进行日志记录。这允许完全符合RFC且功能强大的系统日志记录守护程序来处理syslog。这消除了远程守护程序起作用的需要,并提供了syslog守护程序的增强功能,例如rsyslog和syslog-ng。SMTP也遵循相同的原则。只需将其交给本地SMTP软件即可。在这种情况下,请使用“程序模式”而不是守护程序,但这是相同的想法。让功能更强大的软件来处理它。使用TCP代替UDP进行syslog重试,排队,本地后台处理成为可能。您也可以按原样与代码分开[重新]配置这些守护程序。
为您的应用程序保存代码,让其他软件协同工作。
回答 2
我发现syslog模块可以很容易地获得您描述的基本日志记录行为:
import syslog
syslog.syslog("This is a test message")
syslog.syslog(syslog.LOG_INFO, "Test message at INFO priority")您还可以做其他事情,但是就我所知,即使只是前两行也可以满足您的要求。
回答 3
从这里和其他地方将东西拼凑在一起,这就是我想出的在unbuntu 12.04和centOS6上运行的结果
创建一个/etc/rsyslog.d/以.conf结尾的文件,并添加以下文本
local6.*        /var/log/my-logfile重新启动rsyslog,重新加载似乎不适用于新的日志文件。也许它只重新加载现有的conf文件?
sudo restart rsyslog然后,您可以使用此测试程序来确保它确实有效。
import logging, sys
from logging import config
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(module)s P%(process)d T%(thread)d %(message)s'
            },
        },
    'handlers': {
        'stdout': {
            'class': 'logging.StreamHandler',
            'stream': sys.stdout,
            'formatter': 'verbose',
            },
        'sys-logger6': {
            'class': 'logging.handlers.SysLogHandler',
            'address': '/dev/log',
            'facility': "local6",
            'formatter': 'verbose',
            },
        },
    'loggers': {
        'my-logger': {
            'handlers': ['sys-logger6','stdout'],
            'level': logging.DEBUG,
            'propagate': True,
            },
        }
    }
config.dictConfig(LOGGING)
logger = logging.getLogger("my-logger")
logger.debug("Debug")
logger.info("Info")
logger.warn("Warn")
logger.error("Error")
logger.critical("Critical")回答 4
我添加了一些额外的注释,以防万一,它对任何人都有帮助,因为我发现此交换很有用,但需要一些额外的信息才能使其正常工作。
要使用SysLogHandler登录到特定工具,您需要指定工具值。例如,您已定义:
local3.*    /var/log/mylog
在syslog中,那么您将要使用:
handler = logging.handlers.SysLogHandler(address = ('localhost',514), facility=19)
并且还需要让syslog侦听UDP以使用localhost而不是/ dev / log。
回答 5
是否已将syslog.conf设置为处理工具=用户?
您可以使用工具参数设置python记录器使用的工具,如下所示:
handler = logging.handlers.SysLogHandler(facility=SysLogHandler.LOG_DAEMON)回答 6
import syslog
syslog.openlog(ident="LOG_IDENTIFIER",logoption=syslog.LOG_PID, facility=syslog.LOG_LOCAL0)
syslog.syslog('Log processing initiated...')上面的脚本将使用我们的自定义“ LOG_IDENTIFIER”登录到LOCAL0工具…您可以将LOCAL [0-7]用于本地目的。
回答 7
来自https://github.com/luismartingil/per.scripts/tree/master/python_syslog
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Implements a new handler for the logging module which uses the pure syslog python module.
@author:  Luis Martin Gil
@year: 2013
'''
import logging
import syslog
class SysLogLibHandler(logging.Handler):
    """A logging handler that emits messages to syslog.syslog."""
    FACILITY = [syslog.LOG_LOCAL0,
                syslog.LOG_LOCAL1,
                syslog.LOG_LOCAL2,
                syslog.LOG_LOCAL3,
                syslog.LOG_LOCAL4,
                syslog.LOG_LOCAL5,
                syslog.LOG_LOCAL6,
                syslog.LOG_LOCAL7]
    def __init__(self, n):
        """ Pre. (0 <= n <= 7) """
        try:
            syslog.openlog(logoption=syslog.LOG_PID, facility=self.FACILITY[n])
        except Exception , err:
            try:
                syslog.openlog(syslog.LOG_PID, self.FACILITY[n])
            except Exception, err:
                try:
                    syslog.openlog('my_ident', syslog.LOG_PID, self.FACILITY[n])
                except:
                    raise
        # We got it
        logging.Handler.__init__(self)
    def emit(self, record):
        syslog.syslog(self.format(record))
if __name__ == '__main__':
    """ Lets play with the log class. """
    # Some variables we need
    _id = 'myproj_v2.0'
    logStr = 'debug'
    logFacilityLocalN = 1
    # Defines a logging level and logging format based on a given string key.
    LOG_ATTR = {'debug': (logging.DEBUG,
                          _id + ' %(levelname)-9s %(name)-15s %(threadName)-14s +%(lineno)-4d %(message)s'),
                'info': (logging.INFO,
                         _id + ' %(levelname)-9s %(message)s'),
                'warning': (logging.WARNING,
                            _id + ' %(levelname)-9s %(message)s'),
                'error': (logging.ERROR,
                          _id + ' %(levelname)-9s %(message)s'),
                'critical': (logging.CRITICAL,
                             _id + ' %(levelname)-9s %(message)s')}
    loglevel, logformat = LOG_ATTR[logStr]
    # Configuring the logger
    logger = logging.getLogger()
    logger.setLevel(loglevel)
    # Clearing previous logs
    logger.handlers = []
    # Setting formaters and adding handlers.
    formatter = logging.Formatter(logformat)
    handlers = []
    handlers.append(SysLogLibHandler(logFacilityLocalN))
    for h in handlers:
        h.setFormatter(formatter)
        logger.addHandler(h)
    # Yep!
    logging.debug('test debug')
    logging.info('test info')
    logging.warning('test warning')
    logging.error('test error')
    logging.critical('test critical')回答 8
这是推荐用于3.2及更高版本的yaml dictConfig方式。
在日志中cfg.yml:
version: 1
disable_existing_loggers: true
formatters:
    default:
        format: "[%(process)d] %(name)s(%(funcName)s:%(lineno)s) - %(levelname)s: %(message)s"
handlers:
    syslog:
        class: logging.handlers.SysLogHandler
        level: DEBUG
        formatter: default
        address: /dev/log
        facility: local0
    rotating_file:
        class: logging.handlers.RotatingFileHandler
        level: DEBUG
        formatter: default
        filename: rotating.log
        maxBytes: 10485760 # 10MB
        backupCount: 20
        encoding: utf8
root:
    level: DEBUG
    handlers: [syslog, rotating_file]
    propogate: yes
loggers:
    main:
        level: DEBUG
        handlers: [syslog, rotating_file]
        propogate: yes使用以下命令加载配置:
log_config = yaml.safe_load(open('cfg.yml'))
logging.config.dictConfig(log_config)配置了系统日志和直接文件。请注意,/dev/log是特定于操作系统的。
回答 9
我将其修复在笔记本上。rsyslog服务未侦听套接字服务。
我在/etc/rsyslog.conf文件中配置此行,   并解决了问题:
$SystemLogSocketName  /dev/log
回答 10
您还可以添加文件处理程序或旋转文件处理程序,以将日志发送到本地文件:http : //docs.python.org/2/library/logging.handlers.html

