ColorFormatter
Logging formatter that adds colors based on severity.
Attributes
| Attribute | Type | Description |
|---|---|---|
| COLORS | dict = colored().names | Loglevel - > Color mapping. |
| colors | dict = {'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
| Name | Type | Description |
|---|---|---|
| fmt | string = None | The log format string. |
| use_color | boolean = True | Whether to enable colorized output based on log severity. |
Signature
def ColorFormatter(
fmt: string = None,
use_color: boolean = True
)
Parameters
| Name | Type | Description |
|---|---|---|
| fmt | string = None | The log message format string defining the structure of the output. |
| use_color | boolean = True | Flag 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
| Name | Type | Description |
|---|---|---|
| ei | tuple | The exception information tuple (type, value, traceback) to be formatted. |
Returns
| Type | Description |
|---|---|
string | The 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
| Name | Type | Description |
|---|---|---|
| record | LogRecord | The logging record object containing the message, level, and metadata to be formatted. |
Returns
| Type | Description |
|---|---|
string | The colorized or plain text representation of the log record, safely handled to avoid encoding errors. |