如何在 Python 中配置日志记录到系统日志? [英] How to configure logging to syslog in Python?

查看:39
本文介绍了如何在 Python 中配置日志记录到系统日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解 Python 的 logging 模块.我的需求非常简单:我只想将所有内容都记录到系统日志中.阅读文档后,我想出了这个简单的测试脚本:

I can't get my head around Python's logging module. My needs are very simple: I just want to log everything to syslog. After reading documentation I came up with this simple test script:

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 中产生任何日志记录.怎么了?

But this script does not produce any log records in syslog. What's wrong?

推荐答案

改行:

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')

这篇关于如何在 Python 中配置日志记录到系统日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆