#-*- coding: utf-8 -*- #!/usr/bin/pythonimport logging# create loggerlogger = logging.getLogger('simple_example')logger.setLevel(logging.DEBUG)# create console handler and set level to debugch = logging.StreamHandler()ch.setLevel(logging.DEBUG)# create formatterformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')# add formatter to chch.setFormatter(formatter)# add ch to loggerlogger.addHandler(ch)LOG_FILENAME = './logging_example.out'logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,)# 'application' codelogger.debug('debug message')logger.info('info message')logger.warn('warn message')logger.error('error message')logger.critical('critical message')
2种日志显示是一致的,可以单独设置
参考文档: