import logging
class LogFilter(logging.Filter):
"""忽略带 db handle 的日志"""
def filter(self, record): # 固定写法
return 'db handle' not in record.getMessage()
# 1.生成 logging 的对象
logger = logging.getLogger('web')
logger.setLevel(logging.DEBUG)
# 2.生成 handler对象
ch = logging.StreamHandler()
fh = logging.FileHandler('web.log')
# 2.1 把 handler对象 绑定到logger
logger.addHandler(ch)
logger.addHandler(fh)
# 3.生成formatter 对象
file_formatter = logging.Formatter('%(asctime)s:%(name)s:%(levelname)s:%(message)s')
console_formatter = logging.Formatter('%(asctime)s-%(name)s-%(message)s')
# 3.1 把formatter 对象 绑定handler对象
ch.setFormatter(console_formatter)
fh.setFormatter(file_formatter)
logger.debug('test bug')
logger.error('test error')