fuel-octane/octane/log.py
Yuriy Taraday 26044fe907 Ensure ColorFormatter.format works during interpretator shutdown
Change-Id: Ie1650692715d07cf84ee0ea9ae1f98e6f7282c97
2015-09-03 12:03:19 +03:00

52 lines
1.9 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
import string
class ColorFormatter(logging.Formatter):
LEVEL_COLORS = {
logging.DEBUG: '\033[00;32m', # GREEN
logging.INFO: '\033[00;36m', # CYAN
logging.WARN: '\033[01;33m', # BOLD YELLOW
logging.ERROR: '\033[01;31m', # BOLD RED
logging.CRITICAL: '\033[01;31m', # BOLD RED
}
def format(self, record, string=string, Formatter=logging.Formatter):
# Hack to get last log line instead of exception on 2.6
if string.find is None:
string.find = str.find
res = Formatter.format(self, record) # old-style class on 2.6
return self.LEVEL_COLORS[record.levelno] + res + '\033[m'
def set_console_formatter(**formatter_kwargs):
formatter_kwargs.setdefault(
'fmt', '%(asctime)s %(levelname)-8s %(name)-15s %(message)s')
formatter_kwargs.setdefault('datefmt', '%Y-%m-%d %H:%M:%S')
root_logger = logging.getLogger('')
for handler in root_logger.handlers:
if isinstance(handler, logging.StreamHandler):
console_handler = handler
break
else:
return # Didn't find any StreamHandlers there
formatter = ColorFormatter(**formatter_kwargs)
console_handler.setFormatter(formatter)
def silence_iso8601():
iso8601_logger = logging.getLogger('iso8601')
iso8601_logger.setLevel(logging.INFO)