完整的 demo
# coding: utf-8
'''
@
date: 2016-10-13
@
author: alex.lin
'''
import logging
class LogLevelFilter(logging.Filter):
def __init__(self, name='', level=logging.DEBUG):
super(LogLevelFilter,self).__init__(name)
self.level = level
def filter(self, record):
return record.levelno == self.level
# create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
fh_info = logging.FileHandler('logging_info.log')
fh_info.setLevel(
logging.INFO)
fh_debug = logging.FileHandler('logging_debug.log')
fh_debug.setLevel(logging.DEBUG)
filter_info = LogLevelFilter(level=
logging.INFO)
filter_debug = LogLevelFilter(level=logging.DEBUG)
fh_info.addFilter(filter_info)
fh_debug.addFilter(filter_debug)
logger.addHandler(fh_info)
logger.addHandler(fh_debug)
logger.debug('for debug')
logger.info('for info')
logger.error('for error')