Logging¶
pyclifer ships a Rich-enhanced logging system with a custom TRACE level (5), automatic
secrets masking, and rotating file handler support.
get_logger¶
Main factory. Returns a logger pre-configured with Rich formatting.
pyclifer.get_logger(name=None)
¶
Factory function for creating loggers with Rich capabilities.
This function uses the global configuration system to provide loggers that automatically benefit from Rich enhancements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Logger name (optional). |
None
|
Returns:
| Type | Description |
|---|---|
SupportsTraceLogger
|
Logger configured with Rich capabilities. |
Source code in src/pyclifer/core/log/__init__.py
get_configured_logger¶
Returns a logger with full configuration applied (handlers, level, masker).
pyclifer.get_configured_logger(name=None)
¶
Get a logger that automatically benefits from global Rich configuration.
This function simply retrieves a standard logger that automatically inherits the global Rich configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Logger name (optional). |
None
|
Returns:
| Type | Description |
|---|---|
SupportsTraceLogger
|
Logger automatically configured with Rich capabilities. |
Source code in src/pyclifer/core/log/config.py
configure_rich_logging¶
Low-level setup function. Called internally by get_configured_logger.
pyclifer.configure_rich_logging(use_rich=True, rich_tracebacks=True, enable_secrets_filter=True, sensitive_fields=None, mask_free_text=False, force_reconfigure=False, replace_root_handlers=False)
¶
Configure the Rich logging system once and centrally.
This function sets up the entire logging system in one place: - Global configuration via basicConfig - Adds trace() method to all Python loggers - Prevents multiple configurations with built-in checks
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_rich
|
bool
|
Enable Rich logging capabilities. |
True
|
rich_tracebacks
|
bool
|
Enable Rich tracebacks for exceptions. |
True
|
enable_secrets_filter
|
bool
|
Enable automatic secrets filtering. |
True
|
sensitive_fields
|
list[str] | None
|
Additional field names to mask on top of the defaults. Merged into SecretsMasker.DEFAULT_FIELDS — does not replace them. |
None
|
mask_free_text
|
bool
|
Also mask key=value pairs written inside the text of a message. |
False
|
force_reconfigure
|
bool
|
Force reconfiguration even if already configured. |
False
|
replace_root_handlers
|
bool
|
Also remove the handlers of the root logger that pyclifer did not create (Sentry, syslog, pytest's caplog). By default only the console handler of pyclifer is replaced and those are kept. |
False
|
Source code in src/pyclifer/core/log/config.py
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
add_trace_method¶
Patches a logger instance with a .trace() method at level 5.
pyclifer.add_trace_method(logger_class)
¶
Add a trace method to a logger class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger_class
|
type
|
Logger class to extend. |
required |
Returns:
| Type | Description |
|---|---|
type
|
The updated logger class. |
Source code in src/pyclifer/core/log/levels.py
SecretsMasker¶
Log filter that redacts sensitive values from log records.
pyclifer.SecretsMasker
¶
Bases: Filter
Redact secrets from logs.
Source code in src/pyclifer/core/log/filters.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
__init__(sensitive_fields=None, mask_free_text=False)
¶
Initialize the masker, merging sensitive_fields into DEFAULT_FIELDS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sensitive_fields
|
list[str] | None
|
Additional field names to mask on top of DEFAULT_FIELDS. None and [] are equivalent — only DEFAULT_FIELDS are used. |
None
|
mask_free_text
|
bool
|
Also mask key=value pairs written inside the text of a message and of the exception it carries (message, cause, context, group, notes). Off by default: a name that contains a sensitive field is masked whatever its value means, so max_tokens=100 is masked too. |
False
|
Source code in src/pyclifer/core/log/filters.py
filter(record)
¶
Filter a log record by redacting sensitive values.
Checks whether the record was already processed to avoid duplicate redaction. If not processed, applies redaction to all record fields containing sensitive data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to filter. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the record was successfully filtered or already processed, |
bool
|
False if no regex is configured. |
Source code in src/pyclifer/core/log/filters.py
redact_text(text)
¶
Mask the value of every sensitive key written as key=value inside a text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to scan, for example a log message. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text with those values replaced by a placeholder. A secret with no key next to it, |
str
|
as in "the password is hunter2", cannot be found and is kept. |
Source code in src/pyclifer/core/log/filters.py
redact(item, name=None)
¶
Redact sensitive information from the given input data.
Processes the provided item and optionally uses the name for context during redaction to securely obfuscate sensitive fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Any
|
The data object to be redacted. |
required |
name
|
str | None
|
An optional name providing context for the redaction process. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The redacted version of the input data. |
Source code in src/pyclifer/core/log/filters.py
RichExtraFormatter / RichExtraStreamHandler¶
Rich-aware formatter and stream handler. Wired up automatically by configure_rich_logging.
pyclifer.RichExtraFormatter
¶
Bases: Formatter
Enhanced Formatter with Rich text capabilities and TRACE level support.
Extends click-extra's Formatter to support Rich markup and custom TRACE level while preserving a click-extra's colorization system.
Source code in src/pyclifer/core/log/formatters.py
formatMessage(record)
¶
Enhanced formatting with Rich support and TRACE level.
TRACE records are styled directly because click-extra's theme has no TRACE entry; all other levels delegate to the parent's colorization system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
LogRecord to format. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted message string. |
Source code in src/pyclifer/core/log/formatters.py
pyclifer.RichExtraStreamHandler
¶
Bases: StreamHandler
Enhanced StreamHandler with Rich support and built-in security filtering.
Extends click-extra's StreamHandler to use Rich for beautiful logging while maintaining compatibility with click.echo() and color support. Automatically includes SecretsMasker for sensitive data protection.
Source code in src/pyclifer/core/log/handlers.py
__init__(stream=None, rich_tracebacks=True, enable_secrets_filter=True, sensitive_fields=None, mask_free_text=False, **kwargs)
¶
Initialize the Rich Extra Stream Handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
TextIOBase | None
|
Output stream (defaults to sys.stderr). |
None
|
rich_tracebacks
|
bool
|
Enable Rich tracebacks. |
True
|
enable_secrets_filter
|
bool
|
Enable automatic secrets filtering. |
True
|
sensitive_fields
|
list[str] | None
|
Additional field names to mask on top of the defaults. Merged into SecretsMasker.DEFAULT_FIELDS — does not replace them. |
None
|
mask_free_text
|
bool
|
Also mask key=value pairs written inside the text of a message. |
False
|
**kwargs
|
Any
|
Additional keyword arguments passed to RichHandler. |
{}
|
Source code in src/pyclifer/core/log/handlers.py
emit(record)
¶
Use Rich handler for enhanced output while maintaining click-extra compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
LogRecord to emit. |
required |