Skip to main content
 首页 » 编程设计

python 日志记录模块向控制台输出额外信息

2024年11月24日8me-sa

我想使用日志记录将信息(当前信息相同)记录到控制台和文件。

但是,我看到控制台上打印了额外的不需要的信息。

我想将以下输出同时输出到控制台和文件:

INFO - thisuser executed the pipeline at 2019-04-17 13:44:50,626 
default log message 
other default log message 
INFO - pipeline execution completed at 2019-04-17 13:44:50,627 
INFO - total time elapsed: 100.4 minutes 

我在文件中得到预期的输出,但控制台输出如下:

INFO:start_log:thisuser 
INFO - thisuser executed the pipeline at 2019-04-17 13:44:50,626 
INFO:root:default log message 
default log message 
INFO:root:other default log message 
other default log message 
INFO:end_log:- 
INFO - pipeline execution completed at 2019-04-17 13:44:50,627 
INFO:duration_log:100.4 
INFO - total time elapsed: 100.4 minutes 

我想删除打印到控制台的额外信息(上面的奇数行)。任何帮助将不胜感激!

下面是我正在运行的代码:

import logging 
import getpass 
 
class DispatchingFormatter: 
 
    def __init__(self, formatters, default_formatter): 
        self._formatters = formatters 
        self._default_formatter = default_formatter 
 
    def format(self, record): 
        formatter = self._formatters.get(record.name, self._default_formatter) 
        return formatter.format(record) 
 
logging.basicConfig(level=logging.INFO) 
 
formatter = DispatchingFormatter({ 
        'start_log': logging.Formatter('%(levelname)s - %(message)s executed the pipeline at %(asctime)s'), 
        'end_log': logging.Formatter('%(levelname)s - pipeline execution completed at %(asctime)s'), 
        'duration_log': logging.Formatter('%(levelname)s - total time elapsed: %(message)s minutes') 
    }, 
    logging.Formatter('%(message)s'), 
) 
 
 
c_handler = logging.StreamHandler() 
c_handler.setFormatter(formatter) 
f_handler = logging.FileHandler('log.txt') 
f_handler.setFormatter(formatter) 
 
logging.getLogger().addHandler(c_handler) 
logging.getLogger().addHandler(f_handler) 
 
logging.getLogger('start_log').info(f'{getpass.getuser()}') 
 
logging.info('default log message') 
logging.info('other default log message') 
 
logging.getLogger('end_log').info('-') 
time_elapsed = 100.4 
logging.getLogger('duration_log').info(f'{time_elapsed}') 

管道将打印约 100 行信息(分析结果),我不想在任何日志记录级别前添加这些信息,因此我尝试使用多个格式化程序来实现。

我知道这是一个有点老套的解决方案。如果有人有一般性的改进建议,我们也将不胜感激!

请您参考如下方法:

作为 logging.basicConfig 的文档状态:

Does basic configuration for the logging system by creating a StreamHandler with a default Formatter and adding it to the root logger.

所以已经附加了一个流处理程序。您可以通过调用将其删除:

logging.getLogger().handlers.clear() 

在添加其他处理程序之前。

或者,您可以使用 logging.getLogger().setLevel(logging.INFO) 而不是 basicConfig。在这种情况下,您不必清除处理程序,因为默认情况下它们没有附加。