在很多编程语言中,都会出现日志处理操作,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. C# 执行固定个数任务自行控制进入线程池的线程数量,多任务同时但是并发数据限定

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

  2. 从一份配置清单详解 Nginx 服务器配置

      概述 在前面< Nginx 服务器开箱体验> 一文中我们从开箱到体验,感受了一下 Nginx 服务器的魅力.Nginx 是轻量级的高性能 Web 服务器,提供了诸如 HTTP 代理和反 ...

  3. MongoDB(3.6.3)的用户认证初识

    Windows 10家庭中文版,MongoDB 3.6.3, 前言 刚刚安装好了MongoDB,启动了服务器-mongod命令,启动了MongoDB shell-mongo命令,不过,全程都没有使用u ...

  4. Windows Phone 8 获取设备名称

    通过使用Microsoft.Phone.Info.DeviceStatus类,我们可以获取设备的一些信息,如设备厂商,设备名称等.通过Microsoft.Phone.Info.DeviceStatus ...

  5. (三)使用XML配置SQL映射器

    SqlSessionFactoryUtil.java package com.javaxk.util; import java.io.IOException; import java.io.Input ...

  6. jQuery使用JSONP时的错误处理

    概述 什么是域,简单来说就是协议+域名或地址+端口,3者只要有任何一个不同就表示不在同一个域.跨域,就是在一个域中访问另一个域的数据. 如果只是加载另一个域的内容,而不需要访问其中的数据的话,跨域是很 ...

  7. (最大矩阵链乘)Matrix-chain product

    Matrix-chain product. The following are some instances. a)       <3, 5, 2, 1,10> b)       < ...

  8. vue 插槽slot

    本文是对官网内容的整理 https://cn.vuejs.org/v2/guide/components.html#编译作用域 在使用组件时,我们常常要像这样组合它们: <app> < ...

  9. 使用Emacs:帮助篇

    当你打开emacs并认真阅读“快速指南”后,你已经知道用C-x C-f打开文件,用C-x C-s保存文件,用C-x C-c退出emacs,可以在emacs中生存了.这里把Tutorial.cn中出现的 ...

  10. hdu 1085 有num1个 1 ,num2个 2 ,num3个 5 (母函数)

    有num1个 1 ,num2个 2 ,num3个 5问它们不能组成的最小正整数是谁 样例的母函数 (1+X)(1+X2)(1+X5+X10+X15)展开后 X4的系数为0 Sample Input1 ...