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

    日志介绍

    日志级别:
    默认显示级别为warning,(critical>error>warning>info>debug>notset)

    日志格式配置,测试使用

    import logging
    
    
    logging.basicConfig(
        level=logging.DEBUG,
        filename = "logger.log",
        # filemode="w", #以写入覆盖的方式添加内容,默认以追加方式
        format = "[%(lineno)d] %(asctime)s  %(levelname)s '%(message)s'"
    
    )
    logging.debug('debug message')
    logging.info('info message')
    logging.warning('warning message')
    logging.error('error message')
    logging.critical('critical message')
    

    日志格式配置

    import  logging
    
    def set_logger():
        logger = logging.getLogger() #创建logger对象o
    
        fileh = logging.FileHandler("test.log") #向文件test.log发送日志
        strh = logging.StreamHandler() #向屏幕发送日志
    
        # fm = logging.Formatter("%(lineno)s %(asctime)s %(message)s")
        fm = logging.Formatter("%(asctime)s    %(filename)s[%(lineno)d]   %(levelname)s '%(message)s'")
        fileh.setFormatter(fm) #将日志格式添加到fileh
        strh.setFormatter(fm)   #将日志格式添加到strh
    
        logger.addHandler(fileh)  #添加文件handler
        logger.addHandler(strh) #添加屏幕handler
        logger.setLevel("DEBUG")  #设置日志默认级别为debug
        return logger
    #其他函数调用要使用logger对象
    import xxx
    logger = set_logger()
    logger.debug("xxx")
    
  • 相关阅读:
    Codeforces Round#410 Div.2
    AtCoder Beginner Contest-060
    如何将gedit变成c++编译器
    洛谷 P2486 [SDOI2011]染色
    让lu哥头痛了许久的代码(洛谷:树的统计)
    字符串模拟入门
    luogu P1553 数字反转(升级版)
    那些令人难忘的——坑
    luogu P1341 无序字母对
    最短路相关题目
  • 原文地址:https://www.cnblogs.com/chrrydot/p/9809439.html
Copyright © 2011-2022 走看看