最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】使Python中的logging的日志内容不输出到(Windows的cmd,Linux的terminal等)终端中

Python crifan 14246浏览 0评论

【背景】

看到:

python logging 定向不输出到屏幕?

所以去试试,看看能否实现,对于python的logging,使得不输出信息到对应的windows的cmd中。

 

此处,假设需求是:

只输出内容到文件中,但是不显示到windows的cmd中。

 

【解决过程】

1.参考这里:

Easier python logging

去写测试代码。

经过一番折腾,代码如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:   
【已解决】使Python中的logging的日志内容不输出到(Windows的cmd,Linux的terminal等)终端中
https://www.crifan.com/python_logging_not_ouput_info_to_command_line_or_terminal

Author:     Crifan Li
Time:       2013-01-30
"""

#---------------------------------import---------------------------------------
import logging;

#------------------------------------------------------------------------------
def main():
    logging.info("logging.info: This is logging example, current file log level is logging.DEBUG");
    logging.debug("logging.debug: you should not see this line in log file, for current file log level to logging.DEBUG");
    logging.warning("logging.warning: you can see this line in log file , for current file log level to logging.DEBUG, and logging.warning is above logging.DEBUG");
    logging.error("logging.error: you can see this line in log file , for current file log level to logging.DEBUG, and logging.error is above logging.DEBUG");

###############################################################################
if __name__=="__main__":
    #related article:
    #【整理】Python中的logging模块的使用(可以实现同时输出信息到cmd终端窗口和log文件(txt)中)
    #https://www.crifan.com/summary_python_logging_module_usage/
    
    # logging.basicConfig(
                # level    = logging.DEBUG,
                # format   = 'LINE %(lineno)-4d  %(levelname)-8s %(message)s',
                # datefmt  = '%m-%d %H:%M',
                # filename = "logging_no_output.log",
                # filemode = 'w');
    
    #file handler
    fileHandler = logging.FileHandler("logging_no_output.log", mode="w", encoding="UTF-8");
    fileHandler.setLevel(logging.DEBUG);
    formatter = logging.Formatter('LINE %(lineno)-4d  %(levelname)-8s %(message)s', '%m-%d %H:%M');
    fileHandler.setFormatter(formatter);
    logging.getLogger('').addHandler(fileHandler);
    
    try:
        main();
    except:
        logging.exception("Unknown Error !");
        raise;

运行效果如下:

(1)在windows的cmd中,无任何输出:

D:\tmp\tmp_dev_root\python\tutorial_summary\logging_no_output>logging_no_output.py

D:\tmp\tmp_dev_root\python\tutorial_summary\logging_no_output>

截图如下:

no any output in cmd

(2)当前文件夹下的log文件:

D:\tmp\tmp_dev_root\python\tutorial_summary\logging_no_output\logging_no_output.log

中的内容是:

LINE 19    WARNING  logging.warning: you can see this line in log file , for current file log level to logging.DEBUG, and logging.warning is above logging.DEBUG

LINE 20    ERROR    logging.error: you can see this line in log file , for current file log level to logging.DEBUG, and logging.error is above logging.DEBUG

2.不过,另外也测试了,如果上述是使用logging.basicConfig的那个方案的话,即代码改为:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:   
【未解决】使Python中的logging的日志内容不输出到(Windows的cmd,Linux的terminal等)终端中
https://www.crifan.com/python_logging_not_ouput_info_to_command_line_or_terminal

Author:     Crifan Li
Time:       2013-01-30
"""

#---------------------------------import---------------------------------------
import logging;

#------------------------------------------------------------------------------
def main():
    logging.info("logging.info: This is logging example, current file log level is logging.DEBUG");
    logging.debug("logging.debug: you should not see this line in log file, for current file log level to logging.DEBUG");
    logging.warning("logging.warning: you can see this line in log file , for current file log level to logging.DEBUG, and logging.warning is above logging.DEBUG");
    logging.error("logging.error: you can see this line in log file , for current file log level to logging.DEBUG, and logging.error is above logging.DEBUG");

###############################################################################
if __name__=="__main__":
    #related article:
    #【整理】Python中的logging模块的使用(可以实现同时输出信息到cmd终端窗口和log文件(txt)中)
    #https://www.crifan.com/summary_python_logging_module_usage/
    
    logging.basicConfig(
                level    = logging.DEBUG,
                format   = 'LINE %(lineno)-4d  %(levelname)-8s %(message)s',
                datefmt  = '%m-%d %H:%M',
                filename = "logging_no_output.log",
                filemode = 'w');
    
    # #file handler
    # fileHandler = logging.FileHandler("logging_no_output.log", mode="w", encoding="UTF-8");
    # fileHandler.setLevel(logging.DEBUG);
    # formatter = logging.Formatter('LINE %(lineno)-4d  %(levelname)-8s %(message)s', '%m-%d %H:%M');
    # fileHandler.setFormatter(formatter);
    # logging.getLogger('').addHandler(fileHandler);
    
    try:
        main();
    except:
        logging.exception("Unknown Error !");
        raise;

也可以达到,类似的效果:

(1)windows的cmd中,无输出。

(2)D:\tmp\tmp_dev_root\python\tutorial_summary\logging_no_output\logging_no_output.log

中有输出,但是有点区别的是,logging_no_output.log中,也会输出info和debug的信息的:

LINE 17    INFO     logging.info: This is logging example, current file log level is logging.DEBUG

LINE 18    DEBUG    logging.debug: you should not see this line in log file, for current file log level to logging.DEBUG

LINE 19    WARNING  logging.warning: you can see this line in log file , for current file log level to logging.DEBUG, and logging.warning is above logging.DEBUG

LINE 20    ERROR    logging.error: you can see this line in log file , for current file log level to logging.DEBUG, and logging.error is above logging.DEBUG

 

3.另外,无意间,好像也搜到了:

Django’s default logging configuration

即,Django中,貌似对于其配置参数DEBUG设置为False时,本质上也是用的将log输出到NullHandler

 

 

【总结】

1.其实对于不想把log内容输出到终端(windows的cmd)中的话,其实只是针对之前在:

【整理】Python中的logging模块的使用(可以实现同时输出信息到cmd终端窗口和log文件(txt)中)

中的那部分console的代码,去掉即可。

即,不把console作为handler,所以输出的任何信息,就都不会输出到console中了。

2.想要输出log到文件中,可以有两种方式:

  • logging.basicConfig加上filename参数
  • logging.FileHandler

转载请注明:在路上 » 【已解决】使Python中的logging的日志内容不输出到(Windows的cmd,Linux的terminal等)终端中

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.201 seconds, using 22.22MB memory