Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def color(the_color, *args, reset=None):
"""Color helper."""
from colorlog.escape_codes import escape_codes, parse_colors
try:
if not args:
assert reset is None, "You cannot reset if nothing being printed"
return parse_colors(the_color)
return parse_colors(the_color) + " ".join(args) + escape_codes[reset or "reset"]
except KeyError as k:
raise ValueError("Invalid color {} in {}".format(str(k), the_color))
def color(the_color, message=''):
from colorlog.escape_codes import escape_codes, parse_colors
if not message:
res = parse_colors(the_color)
else:
res = parse_colors(the_color) + message + escape_codes['reset']
return res
def __init__(self, record):
"""Add attributes from the escape_codes dict and the record."""
self.__dict__.update(escape_codes)
self.__dict__.update(record.__dict__)
# Keep a reference to the original record so ``__getattr__`` can
# access functions that are not in ``__dict__``
self.__record = record
def format(self, record):
if not hasattr(record, self.log_color_field):
setattr(record, self.log_color_field, '?')
record.__dict__.update(escape_codes)
color_field = self._get_color_field(record)
if color_field and color_field in self.log_colors:
color = self.log_colors[color_field]
record.log_color = escape_codes[color]
else:
record.log_color = ""
# Format the message
if sys.version_info > (2, 7):
message = super(ColoredFormatter, self).format(record)
else:
message = logging.Formatter.format(self, record)
# Add a reset code to the end of the message
# (if it wasn't explicitly added in format str)
def format(self, record):
# Add the color codes to the record
record.__dict__.update(escape_codes)
# If we recognise the level name,
# add the levels color as `log_color`
if record.levelname in self.log_colors:
color = self.log_colors[record.levelname]
record.log_color = escape_codes[color]
else:
record.log_color = ""
# Format the message
if sys.version_info > (2, 7):
message = super(ColoredFormatter, self).format(record)
else:
message = logging.Formatter.format(self, record)
# Add a reset code to the end of the message
# (if it wasn't explicitly added in format str)
if self.reset and not message.endswith(escape_codes['reset']):
message += escape_codes['reset']
return message
# Set secondary log colors
if self.secondary_log_colors:
for name, log_colors in self.secondary_log_colors.items():
color = self.color(log_colors, record.levelname)
setattr(record, name + '_log_color', color)
# Format the message
if sys.version_info > (2, 7):
message = super(ColoredFormatter, self).format(record)
else:
message = logging.Formatter.format(self, record)
# Add a reset code to the end of the message
# (if it wasn't explicitly added in format str)
if self.reset and not message.endswith(escape_codes['reset']):
message += escape_codes['reset']
return message
# Set secondary log colors
if self.secondary_log_colors:
for name, log_colors in self.secondary_log_colors.items():
color = self.color(log_colors, record.levelname)
setattr(record, name + '_log_color', color)
# Format the message
if sys.version_info > (2, 7):
message = super(ColoredFormatter, self).format(record)
else:
message = logging.Formatter.format(self, record)
# Add a reset code to the end of the message
# (if it wasn't explicitly added in format str)
if self.reset and not message.endswith(escape_codes['reset']):
message += escape_codes['reset']
return message
if color_field and color_field in self.log_colors:
color = self.log_colors[color_field]
record.log_color = escape_codes[color]
else:
record.log_color = ""
# Format the message
if sys.version_info > (2, 7):
message = super(ColoredFormatter, self).format(record)
else:
message = logging.Formatter.format(self, record)
# Add a reset code to the end of the message
# (if it wasn't explicitly added in format str)
if self.reset and not message.endswith(escape_codes['reset']):
message += escape_codes['reset']
return message
def format(self, record):
# Add the color codes to the record
record.__dict__.update(escape_codes)
# If we recognise the level name,
# add the levels color as `log_color`
if record.levelname in self.log_colors:
color = self.log_colors[record.levelname]
record.log_color = escape_codes[color]
else:
record.log_color = ""
# Format the message
if sys.version_info > (2, 7):
message = super(ColoredFormatter, self).format(record)
else:
message = logging.Formatter.format(self, record)
# Add a reset code to the end of the message
def __init__(self, record):
"""Add attributes from the escape_codes dict and the record."""
self.__dict__.update(escape_codes)
self.__dict__.update(record.__dict__)
# Keep a reference to the original record so ``__getattr__`` can
# access functions that are not in ``__dict__``
self.__record = record