• Python day21模块介绍4(logging模块,configparser模块)


    1.日志等级从上往下依次降低

    logging.basicConfig(#日志报错打印的基础配置
        level=logging.DEBUG,
        filename="logger.log",#报错路径,在文件上进行追加
        filemode="w",
        format="%(asctime)s %(filename)s [%(lineno)d] %(message)s"
    )
    logging.debug("debug message")#没多大用
    logging.info("indo message")#没多大用
    logging.warning("warning message")#这个往下都会打印
    logging.error("error message")
    logging.critical("critical message")

    2.上述方法不用,一般用下面的方法

    logger=logging.getLogger()
    
    fh=logging.FileHandler("test_log")#文件输出
    ch=logging.StreamHandler()#屏幕流输出
    
    fm=logging.Formatter("%(asctime)s %(message)s")
    
    fh.setFormatter(fm)
    ch.setFormatter(fm)
    
    logger.addHandler(fh)
    logger.addHandler(ch)
    
    
    logger.debug("hello")
    logger.info("hello")
    logger.warning("hello")
    logger.error("hello")
    logger.critical("hello")

    3.也可下面方法设置函数

    def logger():
        logger=logging.getLogger()
        logger.setLevel("INFO")
    
        fh=logging.FileHandler("test_log")
        ch=logging.StreamHandler()
    
        fm=logging.Formatter("%(asctime)s %(message)s")
    
        fh.setFormatter(fm)
        ch.setFormatter(fm)
    
        logger.addHandler(fh)
        logger.addHandler(ch)
    
        return logger
    
    if __name__ == '__main__':
        logger = logger()
        logger.debug("debug")
        logger.warning("warn")

     4.configparser模块

    configparser模块
    
    import configparser
    conf=configparser.ConfigParser()
    conf["Default"]={
        'SerberAliveInterval':'45',
        'Compression':'yes',
        'CompressionLevel':'9'
    }
    conf['bitbucket.org']={'user':'hg'}
    
    with open('exam.ini','w') as f:
        conf.write(f)
    
    import  configparser
    
    conf=configparser.ConfigParser()
    conf.read('exam.ini')
    print(conf.sections())
    print(conf['DEFAULT']['compression'])
    for key in conf['DEFAULT']:
        print(key)
    
    print(conf.options('bitbucket.org'))#取得默认和选中字典的键
    print(conf.items('bitbucket.org'))#取得默认和选中字典的键和值
    print(conf.get('bitbucket.org','compression'))
    conf.add_section('www')
    conf.remove_section('s')
    conf.remove_option('bitbucket.org','user')
    
    conf.write(open('i.cfg','w'))
  • 相关阅读:
    poj2352树状数组
    hdu1166树状数组
    poj2785双向搜索
    poj2566尺取变形
    poj2100还是尺取
    poj3061尺取法
    poj3320尺取法
    hdu3829最大独立集
    poj2594最小顶点覆盖+传递闭包
    经典换根dp——hdu2196
  • 原文地址:https://www.cnblogs.com/littlepage/p/9427044.html
Copyright © 2020-2023  润新知