zoukankan      html  css  js  c++  java
  • logging

    很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误、警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以为 debug()info()warning()error() and critical() 5个级别,下面我们看一下怎么用。 

    简单的将日志打印到屏幕

     1 import logging
     2 logging.debug("This is debug message")
     3 logging.info("This is info message")
     4 logging.warning("This is warning message")
     5 
     6 
     7 
     8 输出:
     9 WARNING:root:This is warning message
    10 ERROR:root:This is error message
    11 CRITICAL:root:This is critical message

    默认情况下,logging将日志打印到屏幕,日志级别为WARNING;
    日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,当然也可以自己定义日志级别。

    通过logging.basicConfig(为日志系统做基本配置)函数对日志的输出格式及方式做相关配置(日志写到文件里)

     1 #!/urs/bin/evn python
     2 # -*- coding:utf-8 -*-
     3 import logging
     4 logging.basicConfig(filename="zq.log",
     5                     level=logging.DEBUG,
     6                     format="%(asctime)s :%(name)s:%(levelno)s-%(levelname)s %(message)s ",
     7                     datefmt="%Y/%m/%d %I:%M:%S %p")
     8 
     9 logging.debug("This is debug message")
    10 logging.info("This is info message")
    11 logging.warning("This is warning message")
    12 logging.error("This is error message")
    13 logging.critical("This is critical message")

    logging.basicConfig函数各参数:
    filename: 指定日志文件名
    filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
    format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:

    datefmt: 指定时间格式,同time.strftime()
    level: 设置日志级别,默认为logging.WARNING
    stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略
     %(levelno)s: 打印日志级别的数值
     %(levelname)s: 打印日志级别名称
     %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
     %(filename)s: 打印当前执行程序名
     %(funcName)s: 打印日志的当前函数
     %(lineno)d: 打印日志的当前行号
     %(asctime)s: 打印日志的时间
     %(thread)d: 打印线程ID
     %(threadName)s: 打印线程名称
     %(process)d: 打印进程ID
     %(message)s: 打印日志信息

  • 相关阅读:
    随笔2
    随笔
    关于updateElement接口
    随笔1
    本地访问正常,服务器访问乱码 记录
    Redis (error) NOAUTH Authentication required.解决方法
    tomcat启动很慢 停留在 At least one JAR was scanned for TLDs yet contained no TLDs.
    微信公众号消息回复
    微信公众号 报token验证失败
    idea中web.xml报错 Servlet should have a mapping
  • 原文地址:https://www.cnblogs.com/zqxqx/p/8483031.html
Copyright © 2011-2022 走看看