python开发_logging_日志处理
在很多编程语言中,都会出现日志处理操作,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_日志处理的更多相关文章
- Python开发之日志记录模块:logging
1 引言 最近在开发一个应用软件,为方便调试和后期维护,在代码中添加了日志,用的是Python内置的logging模块,看了许多博主的博文,颇有所得.不得不说,有许多博主大牛总结得确实很好.似乎我再写 ...
- centos7.0 安装日志--图文具体解释-python开发环境配置
centos7.0公布之后,就下载了everthing的DVD镜像.今天有时间,所以决定在vbox底下体验一番--- 上图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...
- Python开发【第六篇】:模块
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...
- [原]打造Python开发环境之初篇
古语有云: 工欲善其事,必先利其器 拥有自己的一套得心应手的Python开发环境,开发起来,简直如丝般顺滑.以我工作中使用到的Python开发环境(主要是Web应用),先做个总体介绍 Python环境 ...
- Python学习笔记-CGI编程(如何在IIS上挂Python开发的Webservice)
一.如何用Python开发一个简单的Webservice 利用python的cgi编程,可以传入参数将结果输出. 定义需要编码以及需要引用的模块 #conding=utf-8 #修正中文乱码 impo ...
- Python开发【第十篇】:模块
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...
- python之配置日志的三种方式
以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用它们的配置函数: 2)创建一个日志配置文件,然后使用fileCo ...
- Python之配置日志的几种方式(logging模块)
原文:https://blog.csdn.net/WZ18810463869/article/details/81147167 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Py ...
- 【转】python之配置日志的几种方式
[转]python之配置日志的几种方式 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用 ...
随机推荐
- tomcat发布html静态页面
一.环境 在Linux系统安装JDK并配置环境变量,安装tomcat(在tomcat官网下载压缩包即可,我使用的是tomcat7 https://tomcat.apache.org/download- ...
- 深度解析:python之浅拷贝与深拷贝
深度解析python之浅拷贝与深拷贝 本文包括知识点: 1.copy与deepcopy 2.可变类型与不可变类型 1.copy与deepcopy 在日常python编码过程中,经常会遇见变量的赋值.这 ...
- ansible的几点记录
user模块的password加密 ansible中user模块的password需要传入加密数据,可以通过这种方式快速加密: To get a sha512 password hash (rando ...
- 【小程序开发】购物车加减几件demo
<!-- 主容器 --> <view class="stepper"> <!-- 减号 --> <text class="{{m ...
- Unix IPC之Posix消息队列(1)
部分参考:http://www.cnblogs.com/Anker/archive/2013/01/04/2843832.html IPC对象的持续性:http://book.51cto.com/ar ...
- JavaScript闭包与变量的经典问题
许多人第一次接触闭包大概都是从高程里这段代码开始的: function createFunctions() { var result = new Array(); for(var i=0; i< ...
- 【LOJ】#2064. 「HAOI2016」找相同字符
题解 做后缀自动机题要一点脑洞,脑洞一开,就过了 我们显然要拿第二个串跑第一个串的后缀自动机 我们可以求出第二个串每个位置匹配到的节点,和匹配的长度L 那么我们统计一个后缀树上的根缀和,表示这样个节点 ...
- 8-5 Wine trading in Gergovia Gergovia的酒交易 uva11054
等价转换思维题 题意: 直线上有n(2<=n<=100000)个等距的村庄 每个村庄要么买酒 要么卖酒 设第i个村庄对酒的需求量为ai 绝对值小于一千 其中ai大于0表示买酒 ...
- Oracle数据库的基本查询
本文用的是Oracle 10g数据库,利用PL/SQL Developer的集成开发环境(安装可以自行百度) Oracle数据库 ---> 数据库实例 ---> 表空间(逻辑单位)( ...
- CodeForces - 620C Pearls in a Row 贪心 STL
C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...