# Copyright (c) 2021 Wind River Systems, Inc. # # 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. # # Copyright (C) 2021 Wind River Systems,Inc import logging def set_logger(logger): logger.setLevel(logging.DEBUG) class ColorFormatter(logging.Formatter): FORMAT = ("$BOLD%(name)-s$RESET - %(levelname)s: %(message)s") BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = list(range(8)) RESET_SEQ = "\033[0m" COLOR_SEQ = "\033[1;%dm" BOLD_SEQ = "\033[1m" COLORS = { 'WARNING': YELLOW, 'INFO': GREEN, 'DEBUG': BLUE, 'ERROR': RED } def formatter_msg(self, msg, use_color=True): if use_color: msg = msg.replace("$RESET", self.RESET_SEQ) msg = msg.replace("$BOLD", self.BOLD_SEQ) else: msg = msg.replace("$RESET", "").replace("$BOLD", "") return msg def __init__(self, use_color=True): msg = self.formatter_msg(self.FORMAT, use_color) logging.Formatter.__init__(self, msg) self.use_color = use_color def format(self, record): lname = record.levelname if self.use_color and lname in self.COLORS: fcolor = 30 + self.COLORS[lname] lncolor = self.COLOR_SEQ % fcolor + lname + self.RESET_SEQ record.levelname = lncolor return logging.Formatter.format(self, record) # create console handler and set level to debug ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) ch.setFormatter(ColorFormatter()) logger.addHandler(ch) fh = logging.FileHandler('/localdisk/builder.log') fh.setLevel(logging.DEBUG) fh.setFormatter(ColorFormatter()) logger.addHandler(fh) logger.propagate = 0