Skip to main content

ColorFormatter

Logging formatter that adds colors based on severity.

Attributes

AttributeTypeDescription
COLORSdict = colored().namesLoglevel - > Color mapping.
colorsdict = {'DEBUG': COLORS['blue'], 'WARNING': COLORS['yellow'], 'ERROR': COLORS['red'], 'CRITICAL': COLORS['magenta']}A dictionary mapping logging severity levels to specific color objects used for terminal output formatting.

Constructor

Signature

def ColorFormatter(
fmt: string = None,
use_color: boolean = True
) - > null

Parameters

NameTypeDescription
fmtstring = NoneThe log format string.
use_colorboolean = TrueWhether to enable colorized output based on log severity.

Signature

def ColorFormatter(
fmt: string = None,
use_color: boolean = True
)

Parameters

NameTypeDescription
fmtstring = NoneThe log message format string defining the structure of the output.
use_colorboolean = TrueFlag to enable or disable colorized output in the logs.

Methods


formatException()

@classmethod
def formatException(
ei: tuple
) - > string

Formats the exception information into a string, ensuring the exception tuple is correctly resolved if not provided.

Parameters

NameTypeDescription
eitupleThe exception information tuple (type, value, traceback) to be formatted.

Returns

TypeDescription
stringThe formatted traceback string derived from the exception information.

format()

@classmethod
def format(
record: LogRecord
) - > string

Formats the specified log record as text and applies ANSI color codes based on the record's severity level if coloring is enabled.

Parameters

NameTypeDescription
recordLogRecordThe logging record object containing the message, level, and metadata to be formatted.

Returns

TypeDescription
stringThe colorized or plain text representation of the log record, safely handled to avoid encoding errors.