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
243 244 245 246 247 248 249 250 251 252 253 254 255 | |
Functions
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
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
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
827 828 829 830 831 832 833 834 835 836 | |
Functions
ZenMLJsonFormatter
Bases: Formatter
Format a log record as a single-line JSON object.
Functions
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
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 | |
ZenMLLoggingHandler()
Bases: Handler
Custom handler that routes logs through LoggingContext.
Initialize the logging handler.
Source code in src/zenml/logger.py
804 805 806 807 | |
Functions
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
809 810 811 812 813 814 815 816 817 | |
_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.
Functions
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
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 | |
_StepNameFilter
Bases: Filter
Inject pipeline step name into the log record when available.
Functions
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
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 | |
_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.
Functions
flush() -> None
Flush the stdout buffer.
Source code in src/zenml/logger.py
653 654 655 | |
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
640 641 642 643 644 645 646 647 648 649 650 651 | |
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
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 | |
_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
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
_get_console_logging_format() -> Optional[str]
Get the configured client console logging format.
Source code in src/zenml/logger.py
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 | |
_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
613 614 615 616 617 618 619 620 621 622 623 624 625 | |
_remove_zenml_handlers(root_logger: logging.Logger) -> None
Remove handlers owned by the ZenML logging setup.
Source code in src/zenml/logger.py
867 868 869 870 871 872 | |
_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
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | |
_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
658 659 660 661 662 663 664 665 666 667 668 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 | |
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
780 781 782 783 784 785 786 787 788 789 790 791 792 793 | |
bind_request_context(**fields: Any) -> None
Bind extra context variables to the current 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.
Any previously bound contextvars are cleared first, so this sets a fresh set of context variables to the current logging context.
Source code in src/zenml/logger.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
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_request_context or logging_scope.
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
164 165 166 167 168 169 170 171 172 173 | |
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
875 876 877 878 879 880 881 882 883 884 885 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 | |
logging_scope(**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_scope(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_request_context`) to the log records emitted within the scope.
Source code in src/zenml/logger.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
set_root_verbosity() -> LoggingLevels
Set the root verbosity.
Returns:
| Type | Description |
|---|---|
LoggingLevels
|
The active logging level. |
Source code in src/zenml/logger.py
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 | |
wrap_stdout_stderr() -> None
Wrap stdout and stderr write methods to route through LoggingContext.
Source code in src/zenml/logger.py
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 | |