# %(relativeCreated)d:输出日志信息时的,自Logger创建以来的毫秒数
# %(asctime)s:字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
# %(thread)d:线程ID。可能没有
# %(threadName)s:线程名。可能没有
# %(process)d:进程ID。可能没有
# %(message)s:用户输出的消息
3. logging模块的Formatter,Handler,Logger,Filter对象
def log():
# logger对象:负责产生日志,然后交给Filter过滤,然后交给不同的Handler输出
logger = logging.getLogger('mylogger')
# Filter对象:不常用,略
# Handler对象:接收logger传来的日志,然后控制输出
fh = logging.FileHandler('log_test') # 打印到文件
ch = logging.StreamHandler() # 打印到终端
# Formatter对象:日志格式
fm = logging.Formatter('%(asctime)s %(filename)s [%(lineno)d] %(message)s', datefmt='%Y-%m-%d %H:%M:%S %p')
# 为Handler对象绑定格式
fh.setFormatter(fm)
ch.setFormatter(fm)
# 将Handler添加给logger并设置日志级别
logger.addHandler(fh)
logger.addHandler(ch)
logger.setLevel('DEBUG')
return logger
# 测试
logger = log()
logger.info('hello')
logging.debug('kjhk')
logging.error('nhdvh')
logging.warning('sg')
logging.critical('sv')
4. logger的继承
import logging
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p', )
ch = logging.StreamHandler()
ch.setFormatter(formatter)
logger1 = logging.getLogger('root')
logger2 = logging.getLogger('root.child1')
logger3 = logging.getLogger('root.child1.child2')
logger1.addHandler(ch)
logger2.addHandler(ch)
logger3.addHandler(ch)
logger1.setLevel(10)
logger2.setLevel(10)
logger3.setLevel(10)
logger1.debug('log1 debug')
logger2.debug('log2 debug')
logger3.debug('log3 debug')
'''
2017-07-28 22:22:05 PM - root - DEBUG -test: log1 debug
2017-07-28 22:22:05 PM - root.child1 - DEBUG -test: log2 debug
2017-07-28 22:22:05 PM - root.child1 - DEBUG -test: log2 debug
2017-07-28 22:22:05 PM - root.child1.child2 - DEBUG -test: log3 debug
2017-07-28 22:22:05 PM - root.child1.child2 - DEBUG -test: log3 debug
2017-07-28 22:22:05 PM - root.child1.child2 - DEBUG -test: log3 debug
'''
十、configparser模块
configparser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)。
1. 配置文件样式
# 注释1
; 注释2
[section1]
k1 = v1
k2:v2
user=egon
age=18
is_admin=true
salary=31
[section2]
k1 = v1
2. 创建
import configparser
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'}
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
config.write(configfile)
3. 读取
import configparser
config = configparser.ConfigParser()
config.read('confile')
# 查看所有的标题
res = config.sections() # ['section1', 'section2']
print(res)
# 查看标题section1下所有key=value的key
options = config.options('section1')
print(options) # ['k1', 'k2', 'user', 'age', 'is_admin', 'salary']
# 查看标题section1下所有key=value的(key,value)格式
item_list = config.items('section1')
print(item_list) # [('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('a