加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python开发_logging_日志处理

发布时间:2020-12-17 00:19:59 所属栏目:Python 来源:网络整理
导读:在很多编程语言中,都会出现日志处理操作,python也不例外... 接下来我们来看看python中的 模块 模块主要是处理日志的。 所谓日志,可理解为在软件运行过程中,所记录的的一些运行情况信息 软件开发人员可以根据自己的需求添加日志,日志可以帮助软件开发人

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

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

模块主要是处理日志的。 所谓日志,可理解为在软件运行过程中,所记录的的一些运行情况信息 软件开发人员可以根据自己的需求添加日志,日志可以帮助软件开发人员 了解软件的运行信息,对软件的维护尤为重要。
日志级别:

   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:testhongten.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                            线程名称

<span style="color: #800000;">'''

以下是我做的demo:

Python 3.3.2 (v3.3.2:d047928ae3f6,May 16 2013,00:03:43) [MSC v.1600 32, >>> ================================ RESTART ================================ >>> 2013-08-26 11:01:58,076 - root - DEBUG - this 2013-08-26 11:01:58,080 - root - INFO - this 2013-08-26 11:01:58,085 - root - WARNING - this 2013-08-26 11:01:58,088 - root - ERROR - this 2013-08-26 11:01:58,091 - root - CRITICAL - this >>>

logging.warning( logging.info( logging.basicConfig(filename = path,level = logging.debug( logging.info( logging.warning( logging.warning(,3,4,3+4 logging.basicConfig(format=,level= logging.debug( logging.info( logging.warning( logging.config.fileConfig( logger = logging.getLogger( logger.debug( logger.info( logger.warn( logger.error( logger.critical( ( * 50 ( * 50 ( * 50 ( * 50 == main()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读