zoukankan      html  css  js  c++  java
  • python logging模块

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

    将日志打印到屏幕

    1 import logging
    2 
    3 logging.debug('This is debug message')
    4 logging.info('This is info message')
    5 logging.warning('This is warning message')
    View Code

    通过logging.basicConfig函数对日志的输出格式及方式做相关配置

     1 import logging
     2 
     3 logging.basicConfig(level=logging.DEBUG,
     4                 format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
     5                 datefmt='%a, %d %b %Y %H:%M:%S',
     6                 filename='myapp.log',
     7                 filemode='w')
     8     
     9 logging.debug('This is debug message')
    10 logging.info('This is info message')
    11 logging.warning('This is warning message')
    View Code

    logging.basicConfig函数各参数:
    filename: 指定日志文件名
    filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
    format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:
     %(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: 打印日志信息
    datefmt: 指定时间格式,同time.strftime()
    level: 设置日志级别,默认为logging.WARNING
    stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略

    将日志同时输出到文件和屏幕

     1 import logging
     2 
     3 logging.basicConfig(level=logging.DEBUG,
     4                 format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
     5                 datefmt='%a, %d %b %Y %H:%M:%S',
     6                 filename='myapp.log',
     7                 filemode='w')
     8 
     9 #################################################################################################
    10 #定义一个StreamHandler,将INFO级别或更高的日志信息打印到标准错误,并将其添加到当前的日志处理对象#
    11 console = logging.StreamHandler()
    12 console.setLevel(logging.INFO)
    13 formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
    14 console.setFormatter(formatter)
    15 logging.getLogger('').addHandler(console)
    16 #################################################################################################
    17 
    18 logging.debug('This is debug message')
    19 logging.info('This is info message')
    20 logging.warning('This is warning message')
    View Code

      

  • 相关阅读:
    Java WEB 之页面间传递特殊字符
    c++ using Handle Class Pattern to accomplish implementation hiding
    c++ simple class template example: Stack
    c++ why can't class template hide its implementation in cpp file?
    c++ what happens when a constructor throws an exception and leaves the object in an inconsistent state?
    c++ 用namespace实现java的package的功能
    c++ virtual 和 pure virtual的区别
    c++ istream(ostream)是如何转换为bool的
    c++ 使用boost regex库 总结
    c++ 如何使用第三方的library
  • 原文地址:https://www.cnblogs.com/jcwit/p/7504711.html
Copyright © 2011-2022 走看看