alog¶
Advanced logging utilities with custom levels, formatting, and async support.
This module provides: - Extended log levels (SPAM, VERBOSE, DEBUG0-3) - Custom formatter with compact headers - Easy setup via command-line arguments or programmatic config - Async-safe logging for signal handlers - Convenience functions for all log levels - Exception logging and raising helpers
The numeric values of the extended levels are: SPAM=8, VERBOSE=9, DEBUG0..3=11..14 (between DEBUG=10 and INFO=20).
Formatter
¶
Bases: Formatter
Custom log formatter with compact headers and optional extra fields.
Header format: LEVELYYMMDD HH:MM:SS.mmmmmm;PID;MODULE[;EXTRA...]
Example
import io
import logging
import py_misc_utils.alog as alog
buf = io.StringIO()
logging.basicConfig(level=logging.INFO, stream=buf)
alog.set_current_level(logging.INFO, set_logger=False)
alog.info('hello %s', 'world')
# Header: 'IN' + timestamp;PID;module, then the message.
assert buf.getvalue().startswith('IN')
assert 'hello world' in buf.getvalue()
Source code in py_misc_utils/alog.py
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 | |
format(r)
¶
Format a log record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
LogRecord to format. |
required |
Returns:
| Type | Description |
|---|---|
|
Formatted log string with header and message. |
Source code in py_misc_utils/alog.py
86 87 88 89 90 91 92 93 94 95 96 97 98 | |
formatTime(r, datefmt=None)
¶
Format the timestamp of a log record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
LogRecord. |
required | |
datefmt
|
Optional strftime format string. |
None
|
Returns:
| Type | Description |
|---|---|
|
Formatted timestamp string with microsecond precision. |
Source code in py_misc_utils/alog.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
make_header(r)
¶
Create the log header for a record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
LogRecord. |
required |
Returns:
| Type | Description |
|---|---|
|
Header string: LEVELYYMMDD HH:MM:SS.mmmmmm;PID;MODULE[;EXTRA...] |
Source code in py_misc_utils/alog.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
add_logging_options(parser)
¶
Add logging-related command-line arguments to an ArgumentParser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser
|
argparse.ArgumentParser instance to add arguments to. |
required |
Source code in py_misc_utils/alog.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
setup_logging(args)
¶
Configure logging from parsed command-line arguments or namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace or object with attributes: - log_level: Log level name (default: 'INFO') - log_file: Comma-separated list of files ('STDOUT', 'STDERR', or paths) - log_mod_levels: List of 'MODULE,LEVEL' strings - log_emit_extra: List of LogRecord fields to include in header |
required |
Source code in py_misc_utils/alog.py
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 | |
basic_setup(**kwargs)
¶
Simple programmatic logging setup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Logging configuration options (log_level, log_file, etc.). |
{}
|
Source code in py_misc_utils/alog.py
245 246 247 248 249 250 251 252 253 | |
get_main_config()
¶
Get a config object for use with py_misc_utils.config system.
Returns:
| Type | Description |
|---|---|
|
SimpleNamespace with add_arguments and config_module attributes. |
Source code in py_misc_utils/alog.py
256 257 258 259 260 261 262 263 | |
set_current_level(level, set_logger=True)
¶
Set the current log level threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
Numeric log level (e.g., logging.INFO). |
required | |
set_logger
|
Whether to also update the root logger and handlers (default: True). |
True
|
Source code in py_misc_utils/alog.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
level_active(level)
¶
Check if a log level is currently active (enabled).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
Numeric log level to check. |
required |
Returns:
| Type | Description |
|---|---|
|
True if the level is at or above the current threshold - e.g. with |
|
|
the threshold at INFO, level_active(INFO) is True while |
|
|
level_active(DEBUG) is False. |
Source code in py_misc_utils/alog.py
287 288 289 290 291 292 293 294 295 296 297 298 | |
level_run(level, fn)
¶
Execute a function only if a log level is active.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
Numeric log level to check. |
required | |
fn
|
Callable to execute if level is active. |
required |
Returns:
| Type | Description |
|---|---|
|
Result of fn() if active, otherwise None. |
Source code in py_misc_utils/alog.py
301 302 303 304 305 306 307 308 309 310 311 | |
logging_args(kwargs)
¶
Prepare logging keyword arguments with call limiting and stacklevel adjustment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Keyword arguments for logging call. |
required |
Returns:
| Type | Description |
|---|---|
|
Modified kwargs with adjusted stacklevel, or None if call limited. |
Source code in py_misc_utils/alog.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
log(level, msg, *args, **kwargs)
¶
Log a message at the specified level with call limiting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
Numeric log level (e.g., logging.INFO). |
required | |
msg
|
Message string or callable returning string. |
required | |
*args
|
Format arguments for the message. |
()
|
|
**kwargs
|
Additional logging arguments (limit, stacklevel, etc.). |
{}
|
Source code in py_misc_utils/alog.py
360 361 362 363 364 365 366 367 368 369 370 371 | |
spam(msg, *args, **kwargs)
¶
Log a SPAM level message (very verbose).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
Message string or callable. |
required | |
*args
|
Format arguments. |
()
|
|
**kwargs
|
Additional logging arguments. |
{}
|
Source code in py_misc_utils/alog.py
374 375 376 377 378 379 380 381 382 383 | |
verbose(msg, *args, **kwargs)
¶
Log a VERBOSE level message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
Message string or callable. |
required | |
*args
|
Format arguments. |
()
|
|
**kwargs
|
Additional logging arguments. |
{}
|
Source code in py_misc_utils/alog.py
386 387 388 389 390 391 392 393 394 395 | |
debug0(msg, *args, **kwargs)
¶
Log a DEBUG0 level message (least verbose debug).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
Message string or callable. |
required | |
*args
|
Format arguments. |
()
|
|
**kwargs
|
Additional logging arguments. |
{}
|
Source code in py_misc_utils/alog.py
398 399 400 401 402 403 404 405 406 407 | |
debug1(msg, *args, **kwargs)
¶
Log a DEBUG1 level message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
Message string or callable. |
required | |
*args
|
Format arguments. |
()
|
|
**kwargs
|
Additional logging arguments. |
{}
|
Source code in py_misc_utils/alog.py
410 411 412 413 414 415 416 417 418 419 | |
debug2(msg, *args, **kwargs)
¶
Log a DEBUG2 level message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
Message string or callable. |
required | |
*args
|
Format arguments. |
()
|
|
**kwargs
|
Additional logging arguments. |
{}
|
Source code in py_misc_utils/alog.py
422 423 424 425 426 427 428 429 430 431 | |
debug3(msg, *args, **kwargs)
¶
Log a DEBUG3 level message (most verbose debug).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
Message string or callable. |
required | |
*args
|
Format arguments. |
()
|
|
**kwargs
|
Additional logging arguments. |
{}
|
Source code in py_misc_utils/alog.py
434 435 436 437 438 439 440 441 442 443 | |
debug(msg, *args, **kwargs)
¶
Log a standard DEBUG level message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
Message string or callable. |
required | |
*args
|
Format arguments. |
()
|
|
**kwargs
|
Additional logging arguments. |
{}
|
Source code in py_misc_utils/alog.py
446 447 448 449 450 451 452 453 454 455 | |
info(msg, *args, **kwargs)
¶
Log an INFO level message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
Message string or callable. |
required | |
*args
|
Format arguments. |
()
|
|
**kwargs
|
Additional logging arguments. |
{}
|
Source code in py_misc_utils/alog.py
458 459 460 461 462 463 464 465 466 467 | |
warning(msg, *args, **kwargs)
¶
Log a WARNING level message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
Message string or callable. |
required | |
*args
|
Format arguments. |
()
|
|
**kwargs
|
Additional logging arguments. |
{}
|
Source code in py_misc_utils/alog.py
470 471 472 473 474 475 476 477 478 479 | |
error(msg, *args, **kwargs)
¶
Log an ERROR level message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
Message string or callable. |
required | |
*args
|
Format arguments. |
()
|
|
**kwargs
|
Additional logging arguments. |
{}
|
Source code in py_misc_utils/alog.py
482 483 484 485 486 487 488 489 490 491 | |
critical(msg, *args, **kwargs)
¶
Log a CRITICAL level message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
Message string or callable. |
required | |
*args
|
Format arguments. |
()
|
|
**kwargs
|
Additional logging arguments. |
{}
|
Source code in py_misc_utils/alog.py
494 495 496 497 498 499 500 501 502 503 | |
exception(e, *args, **kwargs)
¶
Log an exception with traceback at ERROR level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
e
|
Exception instance. |
required | |
*args
|
Format arguments. |
()
|
|
**kwargs
|
Additional logging arguments. Supports 'exmsg' for custom prefix. |
{}
|
Source code in py_misc_utils/alog.py
506 507 508 509 510 511 512 513 514 515 516 517 518 | |
xraise(e, msg, *args, **kwargs)
¶
Raise an exception carrying a formatted message (and optionally log it).
The message is formatted as msg % args when format arguments are
given - unlike the log functions, which use %-formatting at record
rendering time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
e
|
Exception class to raise (e.g., ValueError). |
required | |
msg
|
Error message string or callable. |
required | |
*args
|
Format arguments - applied with the % operator immediately. |
()
|
|
**kwargs
|
Additional logging arguments. If 'logit' is True, the message is logged at ERROR level before raising. |
{}
|
Raises:
| Type | Description |
|---|---|
e
|
Instance of the provided exception class with the formatted message. |
Source code in py_misc_utils/alog.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | |
async_log(level, msg, *args, **kwargs)
¶
Log a message asynchronously (safe for signal handlers).
This function avoids the logging module's locks and can be safely called from signal handlers or other asynchronous contexts. It bypasses the configured handlers entirely, formatting the record with the default Formatter() and writing it to the output stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
Numeric log level. |
required | |
msg
|
Message string or callable. |
required | |
*args
|
Format arguments. |
()
|
|
**kwargs
|
Additional arguments. 'file' specifies the output stream - stderr by default. |
{}
|
Example
import io
import py_misc_utils.alog as alog
buf = io.StringIO()
alog.async_log(alog.INFO, 'async %s', 'msg', file=buf)
# Header: 'IN' + timestamp;PID;module, then the message.
assert buf.getvalue().startswith('IN')
assert 'async msg' in buf.getvalue()
Source code in py_misc_utils/alog.py
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |