gpt4 book ai didi

python - 如何在 Python 中配置日志记录到 syslog?

转载 作者:IT老高 更新时间:2023-10-28 21:06:25 25 4
gpt4 key购买 nike

我无法理解 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 中产生任何日志记录。怎么了?

最佳答案

将行改为:

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 - 如何在 Python 中配置日志记录到 syslog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3968669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com