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

Python的logging模块简介

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

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

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

最常见的应用是:把info,warning,error同时输出到cmd窗口(显示)和(写入)log文件中;其中info表示告诉用户,这个是普通的信息;

warning和error分别提醒用户,有些警告,甚至是错误信息,需要用户注意;

把debug类信息,只输出(写入)到log文件中;

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

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中的logging,相对来说,还是很好用的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值