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='INFO').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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
custom_log_format
|
Optional[str]
|
Optional standard library logging format. |
None
|
Source code in src/zenml/logger.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
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
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
Optional[Any]
|
Optional output stream for console logs. |
None
|
Source code in src/zenml/logger.py
910 911 912 913 914 915 916 917 918 919 920 921 922 923 | |
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
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 599 600 601 602 603 | |
ZenMLLoggingHandler()
Bases: Handler
Custom handler that routes logs through LoggingContext.
Initialize the logging handler.
Source code in src/zenml/logger.py
887 888 889 890 | |
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
892 893 894 895 896 897 898 899 900 | |
_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
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 | |
_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
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 | |
_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
736 737 738 | |
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
723 724 725 726 727 728 729 730 731 732 733 734 | |
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
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 | |
_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
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
_get_console_logging_format() -> Optional[str]
Get the configured client console logging format.
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The configured format, or |
Source code in src/zenml/logger.py
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
_prefix_step_name(message: str, step_name: str) -> str
Prefix a console/stdout message with the active step name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The message to prefix. |
required |
step_name
|
str
|
The active pipeline step name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The message with the step name prefix applied where appropriate. |
Source code in src/zenml/logger.py
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 | |
_remove_zenml_handlers(root_logger: logging.Logger) -> None
Remove handlers owned by the ZenML logging setup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root_logger
|
Logger
|
Root logger from which to remove ZenML handlers. |
required |
Source code in src/zenml/logger.py
954 955 956 957 958 959 960 961 962 963 | |
_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
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 | |
_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
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 | |
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
863 864 865 866 867 868 869 870 871 872 873 874 875 876 | |
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
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
clear_log_context() -> None
Clear structured context bound to the active request.
Source code in src/zenml/logger.py
154 155 156 | |
get_logger(logger_name: str) -> logging.Logger
Return a stdlib logger by name.
To attach structured fields to a log record, use the stdlib pattern:
``logger.info("event", extra={"key": "value"})``
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger_name
|
str
|
Name of the logger to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Logger
|
The requested standard library logger. |
Source code in src/zenml/logger.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
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.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A mutable copy of the currently bound logging context. |
Source code in src/zenml/logger.py
184 185 186 187 188 189 190 191 192 193 194 195 196 | |
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
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
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
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
init_logging() -> None
Initialize the ZenML logging system.
Source code in src/zenml/logger.py
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 | |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**fields
|
Any
|
Context fields to bind within the scope. |
{}
|
Source code in src/zenml/logger.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
set_root_verbosity() -> LoggingLevels
Set the root verbosity.
Returns:
| Type | Description |
|---|---|
LoggingLevels
|
The active logging level. |
Source code in src/zenml/logger.py
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 | |
wrap_stdout_stderr() -> None
Wrap stdout and stderr write methods to route through LoggingContext.
Source code in src/zenml/logger.py
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 | |