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

    一、日志记录的格式与调用方法

    1.配置日志打印格式:

    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s:%(message)s',
                        filename='test.log',
                        filemode='a')   #设置日志基本配置

    2.调用方式:

    logging.debug('debug message') #打印debug信息 logging.info('info message') #打印info信息 logging.warning('warning message') #打印warning信息 logging.error('grammer error') #打印error信息 logging.critical('critical message') #打印critical信息

    3.备注:

    level=logging.INFO               #设置最低信息水平(debug<info<warning<error<critical),
                                     #低于该水平的信息无法打印出,一般默认level=warning
    format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s:%(message)s',
                                     #设置日志打印格式(asctime:日志记录时间;filename:被执行程序所在的文件名;
                                     # 若lineno=8,表示是在程序第8行执行了该日志记录程序;)
    datefmt='%a,%d %b %Y %H:%M:%S',       #设置时间格式
    filename='test.log',                  #设定日志文件的名字
    filemode='a'                          #设置文件模式

    二、选择在日志文件/操作台显示日志的方法

    logger=logging.getLogger()             #拿到一个logger对象
    fh = logging.FileHandler('test.log')   #创建一个文件写入对象
    ch = logging.StreamHandler()           #创建一个操作台写入对象
    formater = logging.Formatter('%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s:%(message)s')
                                           #设置打印格式
    fh.setFormatter(formater)              #fh调用打印格式
    ch.setFormatter(formater)              #ch调用打印格式
    logger.addHandler(fh)                  #把fh加入到logger对象
    logger.addHandler(ch)                   #把ch加入到logger对象
    logger.debug('kokokok')
    logger.info('56')
     
  • 相关阅读:
    vtk 矩阵管理系统
    在opengl中使用纹理
    [译文]:单元测试的七种境界
    [翻译]:六分钟八法则塑造优秀程序员
    weekly review 200921: Power Sleep
    Bye, Scofield
    weekly review 200922: Goal
    weekly review 200920: Prototype Demo
    转载:测试驱动开发三原则
    weekly review 200918: productive
  • 原文地址:https://www.cnblogs.com/Finance-IT-gao/p/10427160.html
Copyright © 2011-2022 走看看