【Python】logging.DEBUG级别没有输出

作者遇到一个问题:logging中error和warn工作正常,但是info和debug级别无法输出;

解决:不仅需要设置logging的level,还需要设置logger的level,通过logger.setlevel()

=================================================================


I have a problem with python's logging lib. With the code below I create a "logger":

logger = logging.getLogger()
def logger_init(level):
    try:
        syslog = SysLogHandler(address=LOG_DESTINATION)
    except Exception, ex:
        return
    formatter = logging.Formatter('%(module)s[%(process)d]: %(message)s')
    syslog.setFormatter(formatter)
    syslog.setLevel(level)
    logger.addHandler(syslog)

And I call it like:

logger.debug(SOME_STR_TO_BE_LOGGED)

OR like:

logger.error(SOME_STR_TO_BE_LOGGED)

And I initialize the logger with:

log_level = logging.ERROR
if options.DEBUG_LOG: ####  This comes from options parser and it is True.
    log_level = logging.DEBUG
logger_init(log_level)

The problem is that the error, and warn is working very well, but neither info nor debugmethods prints anything to syslog.

I'm using syslog-ng and I designed my filter, that is it will accept every level from debug to emerg.

What is the problem here? Any ideas?

shareimprove this question
 

You also have to set the level of the logger, not only the handler.

Add this to your logger_init:

logger.setLevel(level)
原文:

I have a problem with python's logging lib. With the code below I create a "logger":

logger = logging.getLogger()
def logger_init(level):
    try:
        syslog = SysLogHandler(address=LOG_DESTINATION)
    except Exception, ex:
        return
    formatter = logging.Formatter('%(module)s[%(process)d]: %(message)s')
    syslog.setFormatter(formatter)
    syslog.setLevel(level)
    logger.addHandler(syslog)

And I call it like:

logger.debug(SOME_STR_TO_BE_LOGGED)

OR like:

logger.error(SOME_STR_TO_BE_LOGGED)

And I initialize the logger with:

log_level = logging.ERROR
if options.DEBUG_LOG: ####  This comes from options parser and it is True.
    log_level = logging.DEBUG
logger_init(log_level)

The problem is that the error, and warn is working very well, but neither info nor debugmethods prints anything to syslog.

I'm using syslog-ng and I designed my filter, that is it will accept every level from debug to emerg.

What is the problem here? Any ideas?

shareimprove this question
 

You also have to set the level of the logger, not only the handler.

Add this to your logger_init:

logger.setLevel(level)

https://stackoverflow.com/questions/13857788/python-logging-debug-level-doesnt-logging

I have a problem with python's logging lib. With the code below I create a "logger":

logger = logging.getLogger()
def logger_init(level):
    try:
        syslog = SysLogHandler(address=LOG_DESTINATION)
    except Exception, ex:
        return
    formatter = logging.Formatter('%(module)s[%(process)d]: %(message)s')
    syslog.setFormatter(formatter)
    syslog.setLevel(level)
    logger.addHandler(syslog)

And I call it like:

logger.debug(SOME_STR_TO_BE_LOGGED)

OR like:

logger.error(SOME_STR_TO_BE_LOGGED)

And I initialize the logger with:

log_level = logging.ERROR
if options.DEBUG_LOG: ####  This comes from options parser and it is True.
    log_level = logging.DEBUG
logger_init(log_level)

The problem is that the error, and warn is working very well, but neither info nor debugmethods prints anything to syslog.

I'm using syslog-ng and I designed my filter, that is it will accept every level from debug to emerg.

What is the problem here? Any ideas?

shareimprove this question
 

You also have to set the level of the logger, not only the handler.

Add this to your logger_init:

logger.setLevel(level)
发布了397 篇原创文章 · 获赞 541 · 访问量 255万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览