zoukankan      html  css  js  c++  java
  • Python标准库-logging模块

    Logging模块可以替代print函数的功能,并能将标准输出输入到日志文件保存起来,而且利用logging模块可以部分替代debug的功能,给程序排错。

    一、logging模块的几个级别。默认情况下logging模块有6个级别。分别为NOSET值0,DeBUG值为10,INFO值为20,WARNING值为30,ERROR值为40,CRITICAL值为50(也可自己定义)。

    二、这些级别的用处是,先将自己的日志定一个级别,logging模块发出的信息级别高于定义的级别,将在标准输出(屏幕)显示出来。发出的信息级别低于定义的级别则略过。

    若未定义级别,默认定义的级别是WARNING

    testLogging.py用于测试logging模块,给自己日志级别定义为INFO

    import logging
    class TestLogging(object):
    def __init__(self):
    logFormat = '%(asctime)-12s %(levelname)-8s %(name)-10s %(message)-12s'
    logFileName = './testLog.txt'

    #该脚本使用不同级别向logger发送几条信息,debug级别低于info,不输出,其他全部输出
    logging.basicConfig(level=logging.INFO,
    format=logFormat,
    filename=logFileName,
    filemode='w')

    logging.debug('debug message')
    logging.info('info message')
    logging.warning('warning message')
    logging.error('error message')
    logging.critical('critical message')

    if __name__ == "__main__":
    test = TestLogging()
    运行后testLog中打印出来的结果如下:

    三、写好testLogging.py,在需要加日志的地方,可以调用logging方便定位问题了

  • 相关阅读:
    Entity SQL 初入
    ObjectQuery查询及方法
    Entity Framework 的事务 DbTransaction
    Construct Binary Tree from Preorder and Inorder Traversal
    Reverse Linked List
    Best Time to Buy and Sell Stock
    Remove Duplicates from Sorted Array II
    Reverse Integer
    Implement Stack using Queues
    C++中const限定符的应用
  • 原文地址:https://www.cnblogs.com/hazelrunner/p/8981463.html
Copyright © 2011-2022 走看看