configparser logging
生活随笔
收集整理的這篇文章主要介紹了
configparser logging
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
configparser模塊 # 該模塊適用于配置文件的格式與windows ini文件類似,可以包含一個或多個節(section),每個節可以有多個參數(鍵=值)。
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)
#---------------------------查找文件內容,基于字典的形式
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會默認default的鍵
print(config.options('bitbucket.org')) # 同for循環,找到'bitbucket.org'下所有鍵
print(config.items('bitbucket.org')) # 找到'bitbucket.org'下所有鍵值對
print(config.get('bitbucket.org','compression')) # yes get方法Section下的key對應的value
#---------------------------增刪改
config = configparser.ConfigParser()
config.read('example.ini')
config.add_section('yuan') # 增加section
config.remove_section('bitbucket.org') # 刪除一個section
config.remove_option('topsecret.server.com',"forwardx11") # 刪除一個配置項
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') # 低級別的 # 排錯信息
# logging.info('info message') # 正常信息
# logging.warning('warning message') # 警告信息
# logging.error('error message') # 錯誤信息
# logging.critical('critical message') # 高級別的 # 嚴重錯誤信息
# # basicconfig 簡單 能做的事情相對少 # 中文的亂碼問題 # 不能同時往文件和屏幕上輸出
#
# 配置log對象 稍微有點復雜 能做的事情相對多
import logging
logger = logging.getLogger() #logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('log.log',encoding='utf-8') # 創建一個文件控制對象
sh = logging.StreamHandler() # 創建一個屏幕控制對象
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')
# 文件操作符 和 格式關聯
fh.setFormatter(formatter)
sh.setFormatter(formatter2)
# logger 對象 和 文件操作符 關聯
logger.addHandler(fh)
logger.addHandler(sh)
logging.debug('debug message') # 低級別的 # 排錯信息
logging.info('info message') # 正常信息
logging.warning('警告錯誤') # 警告信息
logging.error('error message') # 錯誤信息
logging.critical('critical message') # 高級別的 # 嚴重錯誤信息
?
轉載于:https://www.cnblogs.com/ming-yuan/p/9536876.html
總結
以上是生活随笔為你收集整理的configparser logging的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 接口IDisposable的用法
- 下一篇: 6759: 异或序列