configparser logging
生活随笔
收集整理的這篇文章主要介紹了
configparser logging
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
configparser模塊 # 該模塊適用于配置文件的格式與windows ini文件類似,可以包含一個(gè)或多個(gè)節(jié)(section),每個(gè)節(jié)可以有多個(gè)參數(shù)(鍵=值)。
import configparser
config = configparser.ConfigParser()
config['bitbucket.org'] = {'User':'hg'}
config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}
with open('example.ini', 'w') as configfile:config.write(configfile)
#---------------------------查找文件內(nèi)容,基于字典的形式
config = configparser.ConfigParser()
config.read('example.ini')
print(config.sections())
print('bytebong.com' in config) # False
print(config['bitbucket.org']["user"]) # hg
for key in config['bitbucket.org']:print(key) # 注意,有default會(huì)默認(rèn)default的鍵
print(config.options('bitbucket.org')) # 同for循環(huán),找到'bitbucket.org'下所有鍵
print(config.items('bitbucket.org')) # 找到'bitbucket.org'下所有鍵值對(duì)
print(config.get('bitbucket.org','compression')) # yes get方法Section下的key對(duì)應(yīng)的value
#---------------------------增刪改
config = configparser.ConfigParser()
config.read('example.ini')
config.add_section('yuan') # 增加section
config.remove_section('bitbucket.org') # 刪除一個(gè)section
config.remove_option('topsecret.server.com',"forwardx11") # 刪除一個(gè)配置項(xiàng)
config.set('topsecret.server.com','k1','11111')
config.set('yuan','k2','22222')
config.write(open('new2.ini', "w"))import logging
# logging.basicConfig(level=logging.WARNING,
# format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
# datefmt='%a, %d %b %Y %H:%M:%S')
# logging.debug('debug message') # 低級(jí)別的 # 排錯(cuò)信息
# logging.info('info message') # 正常信息
# logging.warning('warning message') # 警告信息
# logging.error('error message') # 錯(cuò)誤信息
# logging.critical('critical message') # 高級(jí)別的 # 嚴(yán)重錯(cuò)誤信息
# # basicconfig 簡(jiǎn)單 能做的事情相對(duì)少 # 中文的亂碼問(wèn)題 # 不能同時(shí)往文件和屏幕上輸出
#
# 配置log對(duì)象 稍微有點(diǎn)復(fù)雜 能做的事情相對(duì)多
import logging
logger = logging.getLogger() #logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('log.log',encoding='utf-8') # 創(chuàng)建一個(gè)文件控制對(duì)象
sh = logging.StreamHandler() # 創(chuàng)建一個(gè)屏幕控制對(duì)象
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter2 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s [line:%(lineno)d] : %(message)s')
# 文件操作符 和 格式關(guān)聯(lián)
fh.setFormatter(formatter)
sh.setFormatter(formatter2)
# logger 對(duì)象 和 文件操作符 關(guān)聯(lián)
logger.addHandler(fh)
logger.addHandler(sh)
logging.debug('debug message') # 低級(jí)別的 # 排錯(cuò)信息
logging.info('info message') # 正常信息
logging.warning('警告錯(cuò)誤') # 警告信息
logging.error('error message') # 錯(cuò)誤信息
logging.critical('critical message') # 高級(jí)別的 # 嚴(yán)重錯(cuò)誤信息
?
轉(zhuǎn)載于:https://www.cnblogs.com/ming-yuan/p/9536876.html
總結(jié)
以上是生活随笔為你收集整理的configparser logging的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 接口IDisposable的用法
- 下一篇: 6759: 异或序列