您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python开发_logging_日志处理

5b51 2022/1/14 8:24:19 python 字数 6291 阅读 671 来源 www.jb51.cc/python

python开发_logging_日志处理

概述

在很多编程语言中,都会出现日志处理操作,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 ser<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s problem,the software has not been able to perform some funciton
  CRITICAL                      A ser<a href="https://www.jb51.cc/tag/IoU/" target="_blank" class="keywords">IoU</a>s 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.basicCon<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>(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!

如果你想格式化<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>日志,你可以这样做:

Here is an Example:
import logging
logging.basicCon<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>(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,在格式化<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>日志的时候需要用到:
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                              <a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a><a href="https://www.jb51.cc/tag/mingcheng/" target="_blank" class="keywords">名称</a>
levelname                         %(levelname)s                             日志级别
levelno                           %(levelno)s                               日志id号
lineno                            %(lineno)s                                行号
module                            %(module)s                                模块<a href="https://www.jb51.cc/tag/mingcheng/" target="_blank" class="keywords">名称</a>
mescs                             %(mescs)s                                 Millisecond portion of the time when the LogRecord was created.
message                           %(message)s                               日志信息
name                              %(name)s                                  日志<a href="https://www.jb51.cc/tag/mingcheng/" target="_blank" class="keywords">名称</a>
pathname                          %(pathname)s                              <a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a><a href="https://www.jb51.cc/tag/jueduilujing/" target="_blank" class="keywords">绝对路径</a>
process                           %(process)s                               进程id
processName                       %(processName)s                           进程<a href="https://www.jb51.cc/tag/mingcheng/" target="_blank" class="keywords">名称</a>
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                            线程<a href="https://www.jb51.cc/tag/mingcheng/" target="_blank" class="keywords">名称</a>

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

以下是我做的demo:

效果:

代码部分:

总结

以上是编程之家为你收集整理的python开发_logging_日志处理全部内容,希望文章能够帮你解决python开发_logging_日志处理所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶