Logger
zenml.logger
Logger Implementation.
Attributes
ENABLE_RICH_TRACEBACK = handle_bool_env_var(ENV_ZENML_ENABLE_RICH_TRACEBACK, True)
module-attribute
ENV_ZENML_CONSOLE_LOGGING_FORMAT = 'ZENML_CONSOLE_LOGGING_FORMAT'
module-attribute
ENV_ZENML_LOGGING_COLORS_DISABLED = 'ZENML_LOGGING_COLORS_DISABLED'
module-attribute
ENV_ZENML_LOGGING_FORMAT = 'ZENML_LOGGING_FORMAT'
module-attribute
ENV_ZENML_SERVER = 'ZENML_SERVER'
module-attribute
ENV_ZENML_SUPPRESS_LOGS = 'ZENML_SUPPRESS_LOGS'
module-attribute
ZENML_LOGGING_VERBOSITY = os.getenv(ENV_ZENML_LOGGING_VERBOSITY, default='DEBUG').upper()
module-attribute
ZENML_STORAGE_LOGGING_VERBOSITY = os.getenv(ENV_ZENML_STORAGE_LOGGING_VERBOSITY, default=None)
module-attribute
_RESERVED_LOG_RECORD_ATTRS: frozenset[str] = frozenset({'args', 'asctime', 'created', 'exc_info', 'exc_text', 'filename', 'funcName', 'levelname', 'levelno', 'lineno', 'message', 'module', 'msecs', 'msg', 'name', 'pathname', 'process', 'processName', 'relativeCreated', 'stack_info', 'taskName', 'thread', 'threadName'})
module-attribute
_original_stderr_write: Optional[Any] = None
module-attribute
_original_stdout_write: Optional[Any] = None
module-attribute
_stderr_wrapped: bool = False
module-attribute
_stdout_wrapped: bool = False
module-attribute
step_names_in_console: ContextVar[bool] = ContextVar('step_names_in_console', default=False)
module-attribute
Classes
LoggingLevels
Bases: Enum
Enum for logging levels.
ZenMLConsoleFormatter(custom_log_format: Optional[str] = None)
Bases: Formatter
Stdlib console formatter for all ZenML console output.
Custom format
- User-provided Python
%-style format string.
Client Side
INFO+default: bare log message with extras and highlights.DEBUGdefault: structured log format with full context.
Server Side
- Always: structured log format.
Decides based on whether
ENV_ZENML_SERVERis set to True.
Initialize the formatter.
Source code in src/zenml/logger.py
254 255 256 257 258 259 260 261 262 263 264 265 266 | |
Methods:
format(record: logging.LogRecord) -> str
Render a log record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to render. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The fully formatted line. |
Source code in src/zenml/logger.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
ZenMLConsoleHandler(stream: Optional[Any] = None)
Bases: StreamHandler
Console handler owned by the ZenML logging setup.
Default stream is _ZenMLStdoutStream() which writes
to the original stdout, bypassing the ZenML wrapper.
Initialize the console handler.
Source code in src/zenml/logger.py
838 839 840 841 842 843 844 845 846 847 | |
Methods:
ZenMLJsonFormatter
Bases: Formatter
Format a log record as a single-line JSON object.
Methods:
format(record: logging.LogRecord) -> str
Render record as a single-line JSON object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to render. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A JSON string (no trailing newline; the handler adds one). |
Source code in src/zenml/logger.py
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 | |
ZenMLLoggingHandler()
Bases: Handler
Custom handler that routes logs through LoggingContext.
Initialize the logging handler.
Source code in src/zenml/logger.py
815 816 817 818 | |
Methods:
emit(record: logging.LogRecord) -> None
Emit a log record through LoggingContext.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to emit. |
required |
Source code in src/zenml/logger.py
820 821 822 823 824 825 826 827 828 | |
_ContextVarsFilter
Bases: Filter
Copy structlog contextvars onto every log record.
If the attribute already exists, it is left untouched.
This bridges the gap between the structlog contextvars and the log record. Without this filter, the structlog contextvars would not be available to the log record and neither the formatter nor the OTel handler would be able to access them.
Methods:
filter(record: logging.LogRecord) -> bool
Enrich a LogRecord with structlog contextvars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to enrich. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Always True (never filters out records). |
Source code in src/zenml/logger.py
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 | |
_StepNameFilter
Bases: Filter
Inject pipeline step name into the log record when available.
Methods:
filter(record: logging.LogRecord) -> bool
Set record.step to the active step name when available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to enrich. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Always True (never filters records out). |
Source code in src/zenml/logger.py
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 | |
_ZenMLStdoutStream
Stream that writes to the original stdout, bypassing the ZenML wrapper.
This ensures console logging doesn't trigger the LoggingContext wrapper, preventing duplicate log entries in stored logs.
Methods:
flush() -> None
Flush the stdout buffer.
Source code in src/zenml/logger.py
664 665 666 | |
write(text: str) -> Any
Write text to the original stdout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to write. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The number of characters written. |
Source code in src/zenml/logger.py
651 652 653 654 655 656 657 658 659 660 661 662 | |
Functions:
_add_step_name_to_message(message: str) -> str
Adds the step name to the message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The message to add the step name to. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The message with the step name added. |
Source code in src/zenml/logger.py
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 | |
_collect_extra_fields(record: logging.LogRecord, exclude_attrs: Optional[set[str]] = None) -> dict[str, Any]
Extract structured fields that aren't stdlib LogRecord attrs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record whose |
required |
exclude_attrs
|
Optional[set[str]]
|
Extra attributes to skip, if any. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Mapping of extra-field name to value. |
Source code in src/zenml/logger.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
_get_console_logging_format() -> Optional[str]
Get the configured client console logging format.
Source code in src/zenml/logger.py
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 | |
_prefix_step_name(message: str, step_name: str) -> str
Prefix a console/stdout message with the active step name.
Source code in src/zenml/logger.py
624 625 626 627 628 629 630 631 632 633 634 635 636 | |
_remove_zenml_handlers(root_logger: logging.Logger) -> None
Remove handlers owned by the ZenML logging setup.
Source code in src/zenml/logger.py
878 879 880 881 882 883 | |
_select_console_formatter() -> logging.Formatter
Return the configured formatter for terminal output.
Returns:
| Type | Description |
|---|---|
Formatter
|
The formatter to attach to the console handler. |
Source code in src/zenml/logger.py
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | |
_wrapped_write(original_write: Any, stream_name: str) -> Any
Wrap stdout/stderr write method to route logs to LoggingContext.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_write
|
Any
|
The original write method. |
required |
stream_name
|
str
|
The name of the stream. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The wrapped write method. |
Source code in src/zenml/logger.py
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 | |
add_zenml_filters(handler: logging.Handler) -> logging.Handler
Add filters to logging handler to attach structlog contextvars and step name to the log record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handler
|
Handler
|
The logging handler to enrich. |
required |
Returns:
| Type | Description |
|---|---|
Handler
|
The same handler, with the filters attached. |
Source code in src/zenml/logger.py
791 792 793 794 795 796 797 798 799 800 801 802 803 804 | |
bind_log_context(*, clear: bool = False, **fields: Any) -> None
Bind fields to the active logging context.
Use it to propagate common context variables to every downstream log record. The extra context persists until the current logging context is cleared.
E.g.: This can be used at request boundaries to propagate context like request_id, method, path, etc. to all log records in a request.
Set clear=True at request boundaries to avoid leaking context from
previous requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clear
|
bool
|
Whether to clear the active context before binding fields. |
False
|
**fields
|
Any
|
Context fields to bind to the active log context. |
{}
|
Source code in src/zenml/logger.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
clear_log_context() -> None
Clear structured context bound to the active request.
Source code in src/zenml/logger.py
148 149 150 | |
get_logger(logger_name: str) -> logging.Logger
Returns a stdlib logger by name.
To attach structured fields to a log record, use the stdlib pattern:
``logger.info("event", extra={"key": "value"})``
Source code in src/zenml/logger.py
90 91 92 93 94 95 96 97 98 | |
get_logging_context() -> dict[str, Any]
Return a snapshot of the currently bound logging context.
Returns a fresh dict (safe to mutate) with all the context
variables bound via bind_log_context or logging_context.
Useful at response/error boundaries that need to surface a single field
(typically request_id) back to the caller.
Source code in src/zenml/logger.py
175 176 177 178 179 180 181 182 183 184 | |
get_logging_level() -> LoggingLevels
Get logging level from the env variable.
Returns:
| Type | Description |
|---|---|
LoggingLevels
|
The logging level. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the logging level is not found. |
Source code in src/zenml/logger.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
handle_bool_env_var(var: str, default: bool = False) -> bool
Converts normal env var to boolean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var
|
str
|
The environment variable to convert. |
required |
default
|
bool
|
The default value to return if the env var is not set. |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
The converted value. |
Source code in src/zenml/constants.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
init_logging() -> None
Initialize the ZenML logging system.
Source code in src/zenml/logger.py
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 | |
logging_context(**fields: Any) -> Generator[None, None, None]
Bind extra context to a specific set of log records.
Use this for narrow scopes inside a request (e.g. a single transaction, a step execution, a retry attempt) where you want a few fields to propagate to certain downstream log records within a scope.
E.g.::
with logging_context(transaction_id=tx_id, attempt=2):
logger.info("retrying transaction")
do_something()
logger.info("transaction attempt succeeded")
This will propagate extra context (on top of the context bound by
`bind_log_context`) to the log records emitted within the scope.
Source code in src/zenml/logger.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
set_root_verbosity() -> LoggingLevels
Set the root verbosity.
Returns:
| Type | Description |
|---|---|
LoggingLevels
|
The active logging level. |
Source code in src/zenml/logger.py
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 | |
wrap_stdout_stderr() -> None
Wrap stdout and stderr write methods to route through LoggingContext.
Source code in src/zenml/logger.py
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 | |