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'))