helpers
assemble_configdict(disable_existing_loggers=False, propagate=False, root_handlers=['console'], root_level='DEBUG', formatters=None, handlers=None, loggers=None)
Build a logging dictConfig dict.
Description
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disable_existing_loggers |
bool
|
When |
False
|
propagate |
bool
|
When |
False
|
root_handlers |
list[str]
|
List of handlers for the root logger. These handler configs must exist in the logging dictConfig. |
['console']
|
root_level |
str
|
The log level for the root logger. |
'DEBUG'
|
formatters |
list[FormatterConfig] | list[dict[str, dict[str, Any]]] | None
|
List of logging formatter config objects. |
None
|
handlers |
list[BaseHandlerConfig | dict[str, dict[str, Any]]] | None
|
List of logging handler config objects. |
None
|
loggers |
list[LoggerConfig | LoggerFactory | dict[str, dict[str, t.Any]]]] | None
|
List of logging logger config objects. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
An initialized logging config dict created from inputs. Used with |
Source code in src/red_logging/helpers/__methods.py
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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | |
ensure_logdir(p=None)
Ensure a directory exists.
Used by logging FileHandlers (RotatingFileHandler, TimedRotatingFileHandler, etc) if
a logging file path is nested, like logs/example/test.log.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p |
str | Path
|
A path to a logging directory, i.e. |
None
|
Raises:
| Type | Description |
|---|---|
PermissionError
|
When permission to create the path in |
Exception
|
When any unhandled exception occurs. |
Source code in src/red_logging/helpers/__methods.py
get_formatter_config(name='default', fmt=MESSAGE_FMT_STANDARD, datefmt=DATE_FMT_STANDARD, style='%', validate=True, as_dict=False)
Return a FormatterConfig, or a dict representing a Formatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
The name for the formatter. Reference this formatter by name in a LoggerConfig. |
'default'
|
fmt |
str
|
The string format for log messages. Python docs: Log Record Attributes |
MESSAGE_FMT_STANDARD
|
datefmt |
str
|
The format for log message timestamps, if |
DATE_FMT_STANDARD
|
style |
str
|
The style of string substitution to use for the formatter. Options include |
'%'
|
validate |
bool
|
If |
True
|
as_dict |
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]]
|
If |
FormatterConfig
|
If |
Source code in src/red_logging/helpers/__methods.py
get_logger_config(name='app', handlers=['console'], level='DEBUG', propagate=False, as_dict=False)
Return a LoggerConfig, or a dict representing a Logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
The name for the logger. Reference this logger by name in a LoggerConfig. |
'app'
|
level |
str
|
The logging level for this handler (NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL). |
'DEBUG'
|
handlers |
list[str]
|
List of handler names that exist in the logging configDict that this logger should use. |
['console']
|
propagate |
bool
|
If |
False
|
as_dict |
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]]
|
If |
RotatingFileHandlerConfig
|
If |
Source code in src/red_logging/helpers/__methods.py
get_rotatingfilehandler_config(name='rotating_app_file', level='DEBUG', formatter='default', filters=None, filename=None, maxBytes=100000, backupCount=3, as_dict=False)
Return a RotatingFileHandlerConfig, or a dict representing a RotatingFileHandler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
The name for the rotating file handler. Reference this handler by name in a LoggerConfig. |
'rotating_app_file'
|
level |
str
|
The logging level for this handler (NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL). |
'DEBUG'
|
formatter |
str
|
The name of a formatter that exists in the overall logging dictConfig. |
'default'
|
filters |
list[str] | None
|
A list of function names for logging filters. |
None
|
filename |
str | Path
|
The full path to the log file you want to create. If parent directories do not exist, this method will handle creating them. |
None
|
maxBytes |
int
|
The maximum size (in bytes) before a logfile is rotated. |
100000
|
backupCount |
int
|
The number of backups to keep as log files rotate. |
3
|
as_dict |
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]]
|
If |
RotatingFileHandlerConfig
|
If |
Source code in src/red_logging/helpers/__methods.py
get_streamhandler_config(name='console', level='INFO', formatter='default', filters=None, stream='ext://sys.stdout', as_dict=False)
Return a StreamHandlerConfig, or a dict representing a StreamingHandler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
The name for the stream handler. Reference this handler by name in a LoggerConfig. |
'console'
|
level |
str
|
The logging level for this handler (NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL). |
'INFO'
|
formatter |
str
|
The name of a formatter that exists in the overall logging dictConfig. |
'default'
|
filters |
list[str] | None
|
A list of function names for logging filters. |
None
|
stream |
str
|
The stream this handler should use, i.e. |
'ext://sys.stdout'
|
as_dict |
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]]
|
If |
StreamHandlerConfig
|
If |
Source code in src/red_logging/helpers/__methods.py
print_configdict(logging_config=None)
Print a logging config dict as a JSON string.
Source code in src/red_logging/helpers/__methods.py
save_configdict(logging_config=None, output_file=Path('logging_config.json'), overwrite=False)
Save a logging dictConfig to a JSON file.