Skip to content

Class swarmauri_standard.logger_formatters.HTMLFormatter.HTMLLoggingFormatter

swarmauri_standard.logger_formatters.HTMLFormatter.HTMLLoggingFormatter

HTMLLoggingFormatter(config)

Bases: Formatter

Custom logging.Formatter implementation that outputs HTML-formatted logs.

This class handles the actual formatting of log records into HTML.

Initialize the HTML formatter with the given configuration.

PARAMETER DESCRIPTION
config

The HTMLFormatter configuration to use

TYPE: HTMLFormatter

Source code in swarmauri_standard/logger_formatters/HTMLFormatter.py
87
88
89
90
91
92
93
94
95
def __init__(self, config: HTMLFormatter):
    """
    Initialize the HTML formatter with the given configuration.

    Args:
        config: The HTMLFormatter configuration to use
    """
    super().__init__()
    self.config = config

config instance-attribute

config = config

format

format(record)

Format the specified log record as HTML.

PARAMETER DESCRIPTION
record

The log record to format

TYPE: LogRecord

RETURNS DESCRIPTION
str

HTML-formatted log message

Source code in swarmauri_standard/logger_formatters/HTMLFormatter.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def format(self, record: logging.LogRecord) -> str:
    """
    Format the specified log record as HTML.

    Args:
        record: The log record to format

    Returns:
        HTML-formatted log message
    """
    # Start with the main container div
    container_attrs = (
        f'class="{self.config.css_class}"' if self.config.css_class else ""
    )
    html_output = f"<div {container_attrs}>"

    # Add timestamp if configured
    if self.config.include_timestamp:
        timestamp = self.formatTime(record, self.config.date_format)
        html_output += f'<span class="log-timestamp">{timestamp}</span>'
        if self.config.include_line_breaks:
            html_output += " "

    # Add log level with appropriate styling
    level_name = record.levelname
    level_style = ""
    if self.config.use_colors and level_name in self.config.level_colors:
        level_style = f' style="color: {self.config.level_colors[level_name]}"'

    level_class = ""
    if level_name in self.config.level_css_classes:
        level_class = f' class="{self.config.level_css_classes[level_name]}"'

    html_output += f"<span{level_class}{level_style}>[{level_name}]</span>"

    # Add logger name
    html_output += f' <span class="log-name">[{record.name}]</span>'

    # Add the log message (escaped to prevent HTML injection)
    message = self.config.escape_html(record.getMessage())
    html_output += f' <span class="log-message">{message}</span>'

    # Add exception info if present
    if record.exc_info:
        # Format the exception info
        exc_text = self.formatException(record.exc_info)
        # Escape HTML characters and replace newlines with <br> tags
        exc_html = self.config.escape_html(exc_text).replace("\n", "<br>")
        html_output += f'<pre class="log-exception">{exc_html}</pre>'

    # Close the container div
    html_output += "</div>"

    return html_output