在很多编程语言中,都会出现日志处理操作,python也不例外...

接下来我们来看看python中的logging模块

'''
python中,logging模块主要是处理日志的。
所谓日志,可理解为在软件运行过程中,所记录的的一些运行情况信息
软件开发人员可以根据自己的需求添加日志,日志可以帮助软件开发人员
了解软件的运行信息,对软件的维护尤为重要。 日志级别: Level When it's used
DEBUG detailed information,typically of interest only when diagnosing problems
INFO confirmation that things are working as expected
WARNING An indication that something unexpected happended,or indicative of some problem in the near future.The software is still working as expected
ERROR Due to a more serious problem,the software has not been able to perform some funciton
CRITICAL A serious error, indication that the program itself may be unable to continue running. The default level is WARNING. Here is an Example: import logging
logging.info('this is an info log!')
logging.warning('this is a warn log!') you can see the result:
WARNING:root:this is a warn log! 如果你想看到级别比较低的一些日志,你可以这样做: Here is an Example:
import logging
logging.basicConfig(filename = 'c:\\test\\hongten.log', level = logging.DEBUG)
logging.debug('this is a debug log!')
logging.info('this is an info log!')
logging.warning('this is a warn log!') you can see the result:
DEBUG:root:this is a debug log!
INFO:root:this is an info log!
WARNING:root:this is a warn log! 如果你想格式化输出日志,你可以这样做: Here is an Example:
import logging
logging.basicConfig(format = '%(levelname)s:%(message)s', level = logging.DEBUG)
logging.debug('this is a debug log!')
logging.info('this is an info log!')
logging.warning('this is a warn log!') you can see the result:
DEBUG:this is a debug log!
INFO:this is an info log!
WARNING:this is a warn log! 下面是LogRecord attributes,在格式化输出日志的时候需要用到:
Attribute name Format Description
args You shouldn’t need to format The tuple of arguments merged into msg to produce message.
this yourself.
asctime %(asctime)s 时间格式
created %(created)s 创建时间
filename %(filename)s 文件名称
levelname %(levelname)s 日志级别
levelno %(levelno)s 日志id号
lineno %(lineno)s 行号
module %(module)s 模块名称
mescs %(mescs)s Millisecond portion of the time when the LogRecord was created.
message %(message)s 日志信息
name %(name)s 日志名称
pathname %(pathname)s 文件绝对路径
process %(process)s 进程id
processName %(processName)s 进程名称
relativeCreated %(relativeCreated)s Time in milliseconds when the LogRecord was created,
relative to the time the logging module was loaded.
thread %(thread)s 线程id
threadName %(threadName)s 线程名称 '''

以下是我做的demo:

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
##################################################
##################################################
##################################################
##################################################
2013-08-26 11:01:58,076 - root - DEBUG - this is a debug log!
2013-08-26 11:01:58,080 - root - INFO - this is an info log!
2013-08-26 11:01:58,085 - root - WARNING - this is a warn log!
2013-08-26 11:01:58,088 - root - ERROR - this is an error log!
2013-08-26 11:01:58,091 - root - CRITICAL - this is a critical log!
>>>

============================================================

代码部分:

============================================================

 #python logging

 #Author  : Hongten
#Mailto : hongtenzone@foxmail.com
#Blog : http://www.cnblogs.com/hongten
#QQ : 648719819
#Create : 2013-08-26
#Version : 1.0 import logging
import logging.config '''
python中,logging模块主要是处理日志的。
所谓日志,可理解为在软件运行过程中,所记录的的一些运行情况信息
软件开发人员可以根据自己的需求添加日志,日志可以帮助软件开发人员
了解软件的运行信息,对软件的维护尤为重要。 日志级别: Level When it's used
DEBUG detailed information,typically of interest only when diagnosing problems
INFO confirmation that things are working as expected
WARNING An indication that something unexpected happended,or indicative of some problem in the near future.The software is still working as expected
ERROR Due to a more serious problem,the software has not been able to perform some funciton
CRITICAL A serious error, indication that the program itself may be unable to continue running. The default level is WARNING. Here is an Example: import logging
logging.info('this is an info log!')
logging.warning('this is a warn log!') you can see the result:
WARNING:root:this is a warn log! 如果你想看到级别比较低的一些日志,你可以这样做: Here is an Example:
import logging
logging.basicConfig(filename = 'c:\\test\\hongten.log', level = logging.DEBUG)
logging.debug('this is a debug log!')
logging.info('this is an info log!')
logging.warning('this is a warn log!') you can see the result:
DEBUG:root:this is a debug log!
INFO:root:this is an info log!
WARNING:root:this is a warn log! 如果你想格式化输出日志,你可以这样做: Here is an Example:
import logging
logging.basicConfig(format = '%(levelname)s:%(message)s', level = logging.DEBUG)
logging.debug('this is a debug log!')
logging.info('this is an info log!')
logging.warning('this is a warn log!') you can see the result:
DEBUG:this is a debug log!
INFO:this is an info log!
WARNING:this is a warn log! 下面是LogRecord attributes,在格式化输出日志的时候需要用到:
Attribute name Format Description
args You shouldn’t need to format The tuple of arguments merged into msg to produce message.
this yourself.
asctime %(asctime)s 时间格式
created %(created)s 创建时间
filename %(filename)s 文件名称
levelname %(levelname)s 日志级别
levelno %(levelno)s 日志id号
lineno %(lineno)s 行号
module %(module)s 模块名称
mescs %(mescs)s Millisecond portion of the time when the LogRecord was created.
message %(message)s 日志信息
name %(name)s 日志名称
pathname %(pathname)s 文件绝对路径
process %(process)s 进程id
processName %(processName)s 进程名称
relativeCreated %(relativeCreated)s Time in milliseconds when the LogRecord was created,
relative to the time the logging module was loaded.
thread %(thread)s 线程id
threadName %(threadName)s 线程名称 ''' def test_log1():
'''因为logging的默认级别是:WARNING
所以,这里只会输出:this is a warn log!'''
logging.warning('this is a warn log!')
logging.info('this is an info log!') def test_log_2_file(path):
'''把运行的日志信息记录到指定的日志文件中
并且把日志的操作级别设置为:DEBUG模式'''
logging.basicConfig(filename = path, level = logging.DEBUG)
logging.debug('this is a debug log!')
logging.info('this is an info log!')
logging.warning('this is a warn log!') def logging_variable_data():
'''可变数据的日志'''
logging.warning('%s + %s = %s', 3, 4, 3+4) def logging_format():
'''格式化日志'''
logging.basicConfig(format='%(asctime)s --%(lineno)s -- %(levelname)s:%(message)s', level=logging.DEBUG)
logging.debug('This message should appear on the console')
logging.info('So should this')
logging.warning('And this, too') def logging_from_config():
'''
c:\\hongten_logging.conf配置文件信息如下:
[loggers]
keys=root,simpleExample [handlers]
keys=consoleHandler [formatters]
keys=simpleFormatter [logger_root]
level=DEBUG
handlers=consoleHandler [logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0 [handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,) [formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
'''
logging.config.fileConfig('c:\\hongten_logging.conf')
logger = logging.getLogger('root')
logger.debug('this is a debug log!')
logger.info('this is an info log!')
logger.warn('this is a warn log!')
logger.error('this is an error log!')
logger.critical('this is a critical log!') def main():
'''在这里,请一个一个的打开测试'''
#test_log_2_file('c:\\hongten.log')
print('#' * 50)
#test_log1()
print('#' * 50)
#logging_variable_data()
print('#' * 50)
#logging_format()
print('#' * 50)
logging_from_config() if __name__ == '__main__':
main()

python开发_logging_日志处理的更多相关文章

  1. Python开发之日志记录模块:logging

    1 引言 最近在开发一个应用软件,为方便调试和后期维护,在代码中添加了日志,用的是Python内置的logging模块,看了许多博主的博文,颇有所得.不得不说,有许多博主大牛总结得确实很好.似乎我再写 ...

  2. centos7.0 安装日志--图文具体解释-python开发环境配置

    centos7.0公布之后,就下载了everthing的DVD镜像.今天有时间,所以决定在vbox底下体验一番--- 上图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...

  3. Python开发【第六篇】:模块

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  4. [原]打造Python开发环境之初篇

    古语有云: 工欲善其事,必先利其器 拥有自己的一套得心应手的Python开发环境,开发起来,简直如丝般顺滑.以我工作中使用到的Python开发环境(主要是Web应用),先做个总体介绍 Python环境 ...

  5. Python学习笔记-CGI编程(如何在IIS上挂Python开发的Webservice)

    一.如何用Python开发一个简单的Webservice 利用python的cgi编程,可以传入参数将结果输出. 定义需要编码以及需要引用的模块 #conding=utf-8 #修正中文乱码 impo ...

  6. Python开发【第十篇】:模块

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  7. python之配置日志的三种方式

    以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用它们的配置函数: 2)创建一个日志配置文件,然后使用fileCo ...

  8. Python之配置日志的几种方式(logging模块)

    原文:https://blog.csdn.net/WZ18810463869/article/details/81147167 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Py ...

  9. 【转】python之配置日志的几种方式

    [转]python之配置日志的几种方式 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用 ...

随机推荐

  1. 13-6_mysql索引_1_Mysql_Learning_Notes_20180719_13-6

    mysql索引_1_Mysql_Learning_Notes 二分查找/折半查找法,binary search 一种在有序数组中查找某一特定元素的搜索算法; 二分查找法的优点是比较少次数,查找速度快, ...

  2. 02 How to Write Go Code 如何编写go语言代码

    How to Write Go Code   如何编写go语言代码 Introduction   介绍 Code organization  组织代码 Overview  概述 Workspaces  ...

  3. HDU 5115 Dire Wolf (区间DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5115 题目大意:有一些狼,从左到右排列,每只狼有一个伤害A,还有一个伤害B.杀死一只狼的时候,会受到这 ...

  4. Python SGMLParser 的1个BUG??

    首先说一下,我用的是python 2.7,刚好在学Python,今天想去爬点图片当壁纸,但是当我用 SGMLParser 做 <img> 标签解析的时候,发现我想要的那部分根本没获取到,我 ...

  5. » Working Around JNI UTF-8 Strings Deprogramming

    private static native void printString(String text); ... void examplePrintString() { String str = &q ...

  6. Winafl学习笔记

    最近在跟师傅们学习Winafl,也去搜集了一些资料,有了一些自己的理解,就此记录一下. Winafl是一个运行时插桩工具,可以提高crash的捕获率. 同时也有自己的遗传算法,可以根据代码覆盖程度进行 ...

  7. mysql中的包含语句INSTR的使用

    1.目前测试百万级数据,效率还是相当可观,感觉比like更精准! 例句 今天项目遇到一个问题,每个用户都有自己的所属渠道,当登录后台操作时,要列出隶属于自己拥有渠道的用户列表,当初想到使用全部遍历出来 ...

  8. USACO 6.2 Shaping Regions

    Shaping Regions N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white s ...

  9. vscode 配置import @ 路径提示及代码智提

    1.安装插件:Path Intellisense 2.配置: "path-intellisense.mappings": { "@": "${work ...

  10. scala递归实现换零钱算法

    import scala.collection.mutable.ArrayBuffer import scala.util.control.Breaks object Exchange { def d ...