logging¶
ColourFormatter ¶
Bases: Formatter
Custom logging formatter that adds color to log level names.
This formatter applies color coding to log level names in the console output to improve readability and visual distinction between different log levels.
Source code in src/holmes/logging.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
__init__ ¶
__init__(
fmt: Optional[str] = None,
datefmt: Optional[str] = None,
style: Literal["%", "{", "$"] = "%",
use_colours: Optional[bool] = None,
) -> None
Initialize the ColourFormatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt
|
Optional[str]
|
Format string for log messages |
None
|
datefmt
|
Optional[str]
|
Format string for dates |
None
|
style
|
Literal['%', '{', '$']
|
Style of the format string |
"%"
|
use_colours
|
Optional[bool]
|
Whether to use colors. If None, colors are used if stdout is a TTY |
None
|
Source code in src/holmes/logging.py
colour_level_name ¶
Apply color to a log level name based on its severity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level_name
|
str
|
Name of the log level to color |
required |
level_no
|
int
|
Numeric value of the log level |
required |
Returns:
| Type | Description |
|---|---|
str
|
Colored log level name |
Source code in src/holmes/logging.py
formatMessage ¶
Format a log record with colored level name if colors are enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
Log record to format |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted log message |
Source code in src/holmes/logging.py
RouteFilter ¶
Bases: Filter
Filter for removing common route access logs.
This filter prevents logging of successful (200) GET requests to common routes like the homepage, static files, and ping endpoint to reduce log noise.
Source code in src/holmes/logging.py
__init__ ¶
Initialize the RouteFilter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
str
|
Positional arguments passed to the parent class |
()
|
**kwargs
|
str
|
Keyword arguments passed to the parent class |
{}
|
Source code in src/holmes/logging.py
filter ¶
Filter log records based on common routes.
This method filters out successful (200) GET requests to common routes to reduce log verbosity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
Log record to filter |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the record should be logged, False if it should be filtered out |
Source code in src/holmes/logging.py
get_correlation_id ¶
set_correlation_id ¶
log_with_timing ¶
Decorator that logs function execution time.
P7-LOG-04: Performance monitoring via timing decorator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Function to wrap with timing |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
Wrapped function that logs execution time |
Source code in src/holmes/logging.py
log_exception ¶
Log an exception with full stack trace.
P7-LOG-01: Ensure exceptions are logged with stack traces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
Exception
|
The exception to log |
required |
message
|
str
|
Additional context message |
'An error occurred'
|