简体   繁体   English

为什么日志记录不考虑我的配置?

[英]Why is logging not considering my configurations?

I would like to use the logging Library to print logging messages on the console I am running my script, and in logging files.我想使用日志库在运行脚本的控制台和日志文件中打印日志消息。

However, I am facing two problems:但是,我面临两个问题:

1) the setLevel function does not seem to work 1)setLevel function 似乎不起作用

2) When I print logging messages on the console, it appears that the buffer storing the logging messages of a run is not flushed between two run. 2)当我在控制台上打印日志消息时,似乎存储运行日志消息的缓冲区在两次运行之间没有刷新。 How should I do to dispose of a clean StreamHandler?我应该如何处理干净的 StreamHandler?

To solve the first problem, I tried to change the logger level to the minimum (logging.DEBUG), but it doesn't work为了解决第一个问题,我尝试将记录器级别更改为最低(logging.DEBUG),但它不起作用

To solve the second problem, I already tried the followings:为了解决第二个问题,我已经尝试了以下方法:

1) sys.stdout.flush() before declaring "Stream_log_handler = logging.StreamHandler(sys.stdout)" 1) sys.stdout.flush() 在声明“Stream_log_handler = logging.StreamHandler(sys.stdout)”之前

2) logger.propagate = False 2) logger.propagate = False

3) Stream_log_handler = logging.StreamHandler(sys.stdout) 3) Stream_log_handler = logging.StreamHandler(sys.stdout)

handlers = logger.handlers for handler in handlers: logger.removeHandler(handler) handlers = logger.handlers 用于处理程序中的处理程序:logger.removeHandler(handler)

4) logging.shutdown() 4) logging.shutdown()

None of these are successful.这些都没有成功。

Here is a portion of my code:这是我的代码的一部分:

import logging
import sys

logger = logging.getLogger(__name__)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')
level = 'DEBUG'

file_log_handler = logging.FileHandler('logging_test.log')
file_log_handler.setLevel(logging.DEBUG)
file_log_handler.setFormatter(formatter)
logger.addHandler(file_log_handler)

stream_log_handler = logging.StreamHandler(sys.stdout)
stream_log_handler.setLevel(logging.DEBUG)
stream_log_handler.setFormatter(formatter)
logger.addHandler(stream_log_handler)

def add(x,y):
    """Add function"""
    result = x+y
    logger.info('Add result : {}'.format(result))
    return result

def substract(x,y):
    """Substract function"""
    result = x-y
    logger.debug('Substract result : {}'.format(result))
    return result

def multiply(x,y):
    """Multiply function"""
    result = x*y
    logger.warning('Multiply result : {}'.format(result))
    return result

def divide(x,y):
    """Divide function"""
    try:
        result = x-y
        logger.debug('Divide result : {}'.format(result))
    except ZeroDivisionError:
        logger.error('Tried to divide by zero')
    else:
        return result


num_1 = 10
num_2 = 5

add_result = add(num_1, num_2)

sub_result = substract(num_1, num_2)

mul_result = multiply(num_1, num_2)

div_result = divide(num_1, num_2)

In the console, I get:在控制台中,我得到:

  • At the first run:在第一次运行时:

In [3]: runfile('...')在 [3] 中:runfile('...')

2019-09-18 21:56:56,636:WARNING: main :Multiply result: 50 2019-09-18 21:56:56,636:警告:主要:相乘结果:50

  • At the second run:在第二次运行时:

In [4]: runfile('...')在 [4] 中:runfile('...')

2019-09-18 21:56:56,636:WARNING: main :Multiply result: 50 2019-09-18 21:56:56,636:警告:主要:相乘结果:50

2019-09-18 21:56:56,636:WARNING: main :Multiply result: 50 2019-09-18 21:56:56,636:警告:主要:相乘结果:50

  • At the third run:在第三次运行时:

In [4]: runfile('...')在 [4] 中:runfile('...')

2019-09-18 21:56:56,636:WARNING: main :Multiply result: 50 2019-09-18 21:56:56,636:警告:主要:相乘结果:50

2019-09-18 21:56:56,636:WARNING: main :Multiply result: 50 2019-09-18 21:56:56,636:警告:主要:相乘结果:50

2019-09-18 21:56:56,636:WARNING: main :Multiply result: 50 2019-09-18 21:56:56,636:警告:主要:相乘结果:50

  • etc.等等

Why are the logging messages stacking from a run to another?为什么日志消息从一个运行堆叠到另一个? Why are only the WARNING level messages that are printed?为什么只打印警告级别的消息?

You've only set the level on your two handlers, not on the logger itself.您只在两个处理程序上设置了级别,而不是在记录器本身上设置了级别。

logger = logging.getLogger(__name__)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')

file_log_handler = logging.FileHandler('logging_test.log')
file_log_handler.setLevel(logging.DEBUG)
file_log_handler.setFormatter(formatter)
logger.addHandler(file_log_handler)

stream_log_handler = logging.StreamHandler(sys.stdout)
stream_log_handler.setLevel(logging.DEBUG)
stream_log_handler.setFormatter(formatter)
logger.addHandler(stream_log_handler)

logger.setLevel(logging.DEBUG)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM