Logger¶
Structured, consistent logging for long-running medical image processing pipelines.
Logger¶
TPTBox.logger.log_file.Logger
¶
Bases: Logger_Interface
Defines a logger object, that automatically creates a logs folder and file in it. Logs logger.print() calls to this file.
Source code in TPTBox/logger/log_file.py
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 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 | |
removed
property
¶
True if the log file has been closed and its finalizer is no longer alive.
__init__
¶
__init__(path: Path | str, log_filename: str | dict[str, str], default_verbose: bool = False, log_arguments=None, prefix: str | None = None)
Initialise a file-backed logger, creating the log directory and file automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to the folder that needs logging (usual dataset with raw/der in it). |
required |
log_filename
|
str | dict[str, str]
|
The filename or the bids-conform key-value pairs as dict. |
required |
default_verbose
|
bool
|
Default verbose behavior when not specified in calls. |
False
|
log_arguments
|
If set, will print the contents in a "run with arguments" section. |
None
|
|
prefix
|
str | None
|
If set, will use this string as prefix instead of the automatically chosen one. |
None
|
Source code in TPTBox/logger/log_file.py
create_from_bids
classmethod
¶
create_from_bids(bids_file: BIDS_FILE, log_filename: str | dict[str, str], default_verbose: bool = False, override_prefix: str | None = None)
Creates a logger object based on metadata from a BIDS_FILE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bids_file
|
BIDS_FILE
|
description |
required |
log_filename
|
str | dict[str, str]
|
description |
required |
default_verbose
|
bool
|
description. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
_type_ |
description |
Source code in TPTBox/logger/log_file.py
flush
¶
flush_sub_logger
¶
Append a sub-logger's accumulated content into this log file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sublogger
|
String_Logger
|
The sub-logger whose content should be flushed. |
required |
closed
|
bool
|
If True, also removes |
False
|
Source code in TPTBox/logger/log_file.py
remove
¶
close
¶
Flush all sub-loggers, write timing information, and close the log file.
Source code in TPTBox/logger/log_file.py
Logger_Interface¶
TPTBox.logger.log_file.Logger_Interface
¶
Bases: Protocol
Structural protocol defining the logging interface for medical imaging pipelines.
All concrete logger classes (Logger, No_Logger, String_Logger,
Reflection_Logger) implement this protocol. Client code should type-hint
against Logger_Interface to remain independent of the concrete
implementation.
Source code in TPTBox/logger/log_file.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 | |
print
¶
print(*text, end='\n', ltype=Log_Type.TEXT, verbose: bool | None = None, ignore_prefix: bool = False) -> None
Print text to the logger and optionally to the terminal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*text
|
Text to be printed/logged. |
()
|
|
end
|
End char (default: newline). |
'\n'
|
|
ltype
|
Log type (Text, Warning,...). If it is contained in *text and not here, it will still work. |
TEXT
|
|
verbose
|
bool | None
|
true/false: prints to terminal (if None, uses default verbose). |
None
|
ignore_prefix
|
bool
|
If False, will set a prefix character based on Log_Type (e.g. [*], [!], ...). |
False
|
Source code in TPTBox/logger/log_file.py
close
¶
flush
¶
flush_sub_logger
¶
add_sub_logger
¶
Creates a sub-logger that only logs to string. Will be appended in this loggers log file as sub-logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
name of the sub-logger |
required |
default_verbose
|
bool
|
default_verbose attribute for the sub-logger |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
sub_logger |
String_Logger | No_Logger
|
String_Logger |
Source code in TPTBox/logger/log_file.py
print_error
¶
log_statistic
¶
log_statistic(key: str, value: float, key2: str | int | None = None, verbose: bool = True, round_print: int | None = 5) -> None
Record a scalar metric and log it as a neutral message.
Values are accumulated under key / key2 and can later be
summarized with :meth:print_statistic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Primary metric name (e.g. |
required |
value
|
float
|
Numeric value to record. |
required |
key2
|
str | int | None
|
Secondary key (e.g. sample name). Defaults to the current
count for |
None
|
verbose
|
bool
|
If True, prints the entry immediately. |
True
|
round_print
|
int | None
|
Number of decimal places for display. Set to None to disable rounding. |
5
|
Source code in TPTBox/logger/log_file.py
print_statistic
¶
Print a summary table of all accumulated statistics to the log.
Computes and logs the mean, standard deviation, median, and count for
every key previously recorded via :meth:log_statistic.
Source code in TPTBox/logger/log_file.py
on_fail
¶
Log a message with Log_Type.FAIL severity.
on_log
¶
Log a message with Log_Type.LOG severity.
on_bold
¶
Log a message with Log_Type.BOLD formatting.
on_save
¶
Log a message with Log_Type.SAVE severity.
on_debug
¶
Log a message with Log_Type.STRANGE (debug) severity.
on_ok
¶
Log a message with Log_Type.OK severity.
on_neutral
¶
Log a message with Log_Type.NEUTRAL severity.
on_warning
¶
Log a message with Log_Type.WARNING severity.
on_text
¶
Log a message with default Log_Type.TEXT severity.
warning
¶
Log a warning message (drop-in for Python's logging.warning).
error
¶
Log an error message (drop-in for Python's logging.error).
info
¶
Log an informational message (alias for :meth:on_text).
No_Logger¶
TPTBox.logger.log_file.No_Logger
¶
Bases: Logger_Interface
Does not create any logs, but instead verbose defaults to true, printing calls to the terminal.
Source code in TPTBox/logger/log_file.py
String_Logger¶
TPTBox.logger.log_file.String_Logger
¶
Bases: Logger_Interface
Logger that logs only to a string object "log_content".
Source code in TPTBox/logger/log_file.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | |
as_sub_logger
classmethod
¶
Create a String_Logger that is wired to a parent Logger_Interface.
When flushed or closed the content is forwarded to head_logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
head_logger
|
Logger_Interface
|
Parent logger that will receive this sub-logger's content. |
required |
default_verbose
|
bool
|
Default verbosity for calls on the sub-logger. |
False
|
Returns:
| Type | Description |
|---|---|
String_Logger
|
A new |
Source code in TPTBox/logger/log_file.py
flush
¶
Forward accumulated content to the head logger without closing.
close
¶
Finalize the sub-logger and forward its content to the head logger.
Returns:
| Type | Description |
|---|---|
tuple[str, str]
|
A 2-tuple of |
Source code in TPTBox/logger/log_file.py
flush_sub_logger
¶
Log_Type¶
TPTBox.logger.log_constants.Log_Type
¶
Bases: Enum
The different types of Logs supported.