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':'',
'Compression':'yes',
'CompressionLevel':''
}
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'))
Python day21模块介绍4(logging模块,configparser模块)的更多相关文章
- python基础之正则表达式爬虫应用,configparser模块和subprocess模块
正则表达式爬虫应用(校花网) 1 import requests 2 import re 3 import json 4 #定义函数返回网页的字符串信息 5 def getPage_str(url): ...
- 爬虫模块介绍--request(发送请求模块)
爬虫:可见即可爬 # 每个网站都有爬虫协议 基础爬虫需要使用到的三个模块 requests 模块 # 模拟发请求的模块 PS:python原来有两个模块urllib和urllib的升级urlli ...
- 函数和常用模块【day06】:configparser模块(七)
本节内容 1.简述 2.配置文件格式 3.创建配置文件 4.读取配置文件 5.增删该查语法 一.简述 在很多情况下,我们都需要修改配置文件,但是,有些配置文件,如mysql数据库的配置文件怎么修改呢? ...
- 8.2、常用模块介绍2:xml,configparser,hashlib
xml: 介绍:包含关于可扩展标记语言xml的函数 使用: python有三种方法解析XML--SAX,DOM,以及ElementTree,由于xml技术落后,所以这里不对sax,dom介绍: xml ...
- 爬虫模块介绍--Beautifulsoup (解析库模块,正则)
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时 ...
- 《Python》hashlib模块、configparser模块、logging模块
一.hashlib模块 Python的hashlib模块中提供了常见的摘要算法,如md5,sha1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的字符串(通 ...
- Python之日志处理(logging模块)
本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模块日志流处理流程 使用logging四大组件记录日志 配置logging的几种方式 向日 ...
- 【转】Python之日志处理(logging模块)
[转]Python之日志处理(logging模块) 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模块日志流处理流程 使用logging ...
- Python之日志处理(logging模块)转载
本人主要做一个知识的归类与记录,如是转载类文章,居首都会备注原链接,尊重原创者,谢谢! 此文转载原链接:https://www.cnblogs.com/yyds/p/6901864.html 本节内容 ...
- Python之日志处理(logging模块一基础)
转载自:https://www.cnblogs.com/yyds/p/6901864.html 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logg ...
随机推荐
- numpy中loadtxt 的用法
numpy中有两个函数可以用来读取文件,主要是txt文件, 下面主要来介绍这两个函数的用法 第一个是loadtxt, 其一般用法为 numpy.loadtxt(fname, dtype=, comme ...
- json 的相互 转换
using System.Runtime.Serialization.Json; //json 转化为List集合 public List<T> JSONStringToList<T ...
- Sizzle源码分析 (一)
Sizzle 源码分析 (一) 2.1 稳定 版本 Sizzle 选择器引擎博大精深,下面开始阅读它的源代码,并从中做出标记 .先从入口开始,之后慢慢切入 . 入口函数 Sizzle () 源码 19 ...
- yii2增删改查及AR的理解
yii2增删改查 // 返回 id 为 1 的客户 $customer = Customer::findOne(1); // 返回 id 为 1 且状态为 *active* 的客户 $customer ...
- memecached常用命令
memcached 常用命令及使用说明 1.启动Memcache 常用参数 -p <num> 设置TCP端口号(默认设置为: 11211) -U <num> UDP监听端口(默 ...
- [转]autoid文件上传
原文地址:https://www.cnblogs.com/yoyoketang/p/7612026.html 前言 关于非input文件上传,点上传按钮后,这个弹出的windows的控件了,已经跳出三 ...
- 持续集成之二:搭建SVN服务器--Apache HTTP Server安装
安装环境 Red Hat Enterprise Linux Server release 7.3 (Maipo) jdk1.7.0_80 httpd-2.4.35.tar.gz apr-1.6.5.t ...
- 数据在千万级别上进行全文检索有哪些技术?强大的大数据全文索引解决方案-ClouderaSearch
数据在千万级别上进行全文检索有哪些技术?强大的大数据全文索引解决方案-ClouderaSearch1.lucene (solr, elasticsearch 都是基于它) 2.sphinx3.elas ...
- Python之路----生成器函数进阶
def generator(): print(123) yield 1 print(456) yield 2 g = generator() ret = g.__next__() print('*** ...
- 短网址(short URL)系统的原理及其实现
短网址(short URL)系统的原理及其实现 https://hufangyun.com/2017/short-url/?hmsr=toutiao.io&utm_medium=toutiao ...