在很多编程语言中,都会出现日志处理操作,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. 25 个常用的 Linux iptables 规则【转】

    转自 25 个常用的 Linux iptables 规则 - 文章 - 伯乐在线http://blog.jobbole.com/108468/ # 1. 删除所有现有规则 iptables -F # ...

  2. python3 asyncio官方文档中文版

    事件循环基类 事件循环基类 事件循环是由asyncio提供的核心执行装置.它提供了多种服务,包括: 注册.执行和关闭延时调用(超时) 为各种通信创建客户端和服务端传输 为一个外部程序通信启动子进程和相 ...

  3. C# 执行固定个数任务自行控制进入线程池的线程数量,多任务同时但是并发数据限定

    思路来源:http://bbs.csdn.NET/topics/390819824,引用该页面某网友提供的方法. 题目:我现在有100个任务,需要多线程去完成,但是要限定同时并发数量不能超过5个. 原 ...

  4. unity 2d 游戏优化之路 遇坑记录

    情景说明:  unity 出的Android包,在目前一些主流机型跑都没有问题,但是在 小米3 这种比较老的机器上跑,报如下错误 GLSL compilation failed, no infolog ...

  5. pip离线安装

    pip freeze > requirements.txt pip download <packages> pip install --no-index --find-links=& ...

  6. local class incompatible: stream classdesc serialVersionUID = -2897844985684768944, local class serialVersionUID = 7350468743759137184

    local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2427 ...

  7. day06作业

    一.方法 1.方法是完成特定功能的代码块. 修饰符  返回值类型  方法类型(参数类型  参数名1,参数类型  参数名2,...){ 方法体语句: return返回值: } 修饰符:目前就用publi ...

  8. java基础59 JavaScript运算符与控制流程语句(网页知识)

    1.JavaScript运算符 1.1.加减乘除法 加法:+(加法,连接符,正数)          true是1,false是0    减法:-    乘法:*    除法:/ 1.2.比较运算符 ...

  9. Elasticsearch 邻近查询示例

    Elasticsearch 邻近查询示例(全切分分词) JAVA API方式: SpanNearQueryBuilder span = QueryBuilders.spanNearQuery(); s ...

  10. 网络协议之TCP

    前言 近年来,随着信息技术的不断发展,各行各业也掀起了信息化浪潮,为了留住用户和吸引用户,各个企业力求为用户提供更好的信息服务,这也导致WEB性能优化成为了一个热点.据分析,网站速度越快,用户的黏性. ...