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

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

Python crifan 11577浏览 0评论

Python的logging模块简介

Python中的logging模块,是用于实现日志的功能。

此日志的功能,简单的说,可以控制需要输出的信息,输出(显示)到哪里;

相关要显示的信息,有很多种等级,比如info,warning,error等;

 

最常见的应用是:

  • 把info,warning,error同时输出到cmd窗口(显示)和(写入)log文件中;
    • 其中info表示告诉用户,这个是普通的信息;
    • warning和error分别提醒用户,有些警告,甚至是错误信息,需要用户注意;
  • 把debug类信息,只输出(写入)到log文件中;

 

官网的,在线手册中有详细的语法解释:

logging — Logging facility for Python

 

logging的基本用法

此处,直接给出示例代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
-------------------------------------------------------------------------------
Function:
【整理】Python中的logging模块的使用(可以实现同时输出信息到cmd终端窗口和log文件(txt)中)
https://www.crifan.com/summary_python_logging_module_usage

Author:     Crifan
Verison:    2012-11-23
-------------------------------------------------------------------------------
"""

import logging;

#-------------------------------------------------------------------------------
def loggingDemo():
    """Just demo basic usage of logging module
    """
    logging.info("You should see this info both in log file and cmd window");
    logging.warning("You should see this warning both in log file and cmd window");
    logging.error("You should see this error both in log file and cmd window");
    
    logging.debug("You should ONLY see this debug in log file");
    return;

#-------------------------------------------------------------------------------    
def initLogging(logFilename):
    """Init for logging
    """
    logging.basicConfig(
                    level    = logging.DEBUG,
                    format   = 'LINE %(lineno)-4d  %(levelname)-8s %(message)s',
                    datefmt  = '%m-%d %H:%M',
                    filename = logFilename,
                    filemode = 'w');
    # define a Handler which writes INFO messages or higher to the sys.stderr
    console = logging.StreamHandler();
    console.setLevel(logging.INFO);
    # set a format which is simpler for console use
    formatter = logging.Formatter('LINE %(lineno)-4d : %(levelname)-8s %(message)s');
    # tell the handler to use this format
    console.setFormatter(formatter);
    logging.getLogger('').addHandler(console);


###############################################################################
if __name__=="__main__":
    logFilename = "crifan_logging_demo.log";
    initLogging(logFilename);
    loggingDemo();

然后去windows的cmd中运行后,可以看到cmd中只看到对应的info,warning,error信息:

D:\tmp\tmp_dev_root\python\logging_demo>logging_demo.py
LINE 20   : INFO     You should see this info both in log file and cmd window
LINE 21   : WARNING  You should see this warning both in log file and cmd window
LINE 22   : ERROR    You should see this error both in log file and cmd window

而对应生成的log文件:crifan_logging_demo.log,中的内容是,除了上述内容,还有debug信息:

LINE 20    INFO     You should see this info both in log file and cmd window
LINE 21    WARNING  You should see this warning both in log file and cmd window
LINE 22    ERROR    You should see this error both in log file and cmd window
LINE 24    DEBUG    You should ONLY see this debug in log file

 

logging的更多的复杂应用

更复杂一些的应用,可以参考已实现的一些:

【已解决】Python中,如何让多个py文件的logging输出到同一个日志log文件

 

总结

Python中的logging,相对来说,还是很好用的。

转载请注明:在路上 » 【整理】Python中的logging模块的使用(可以实现同时输出信息到cmd终端窗口和log文件(txt)中)

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.266 seconds, using 22.19MB memory