zoukankan      html  css  js  c++  java
  • 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'))
  • 相关阅读:
    Maven(二)Maven项目的创建(命令、myeclipse)及生命周期
    Maven(一)初识Maven
    MySQL(十一)之触发器
    MySQL(十)之视图
    MySQL(九)之数据表的查询详解(SELECT语法)二
    MySQL(九)之数据表的查询详解(SELECT语法)一
    关于oracle的锁表解决session marked for kill
    shell脚本清空redis库缓存
    Java 数组拷贝方法 System.arraycopy
    oracle 替换字符 replace
  • 原文地址:https://www.cnblogs.com/littlepage/p/9427044.html
Copyright © 2011-2022 走看看