Skip to content

Commit

Permalink
Use the default logging handler on warnings.warnings calls (#22267)
Browse files Browse the repository at this point in the history
Summary:
This ensures that warnings.warnings calls get basic logging information
like timestamps and don't look totally different from other warnings

Test Plan:
Load the following code:
```
from dagster import AutoMaterializePolicy, asset

@asset(auto_materialize_policy=AutoMaterializePolicy.lazy())
def my_asset():
    print("HELLO")
```

Before, it looked like:

```
2024-06-03 17:16:59 -0500 - dagster - INFO - Launching Dagster services...
/Users/dgibson/dagster/python_modules/dagster/dagster/_core/definitions/auto_materialize_policy.py:243: ExperimentalWarning: Class `AutoMaterializePolicy` is experimental. It may break in future versions, even between dot releases. To mute warnings for experimental functionality, invoke warnings.filterwarnings("ignore", category=dagster.ExperimentalWarning) or use one of the other methods described at https://docs.python.org/3/library/warnings.html#describing-warning-filters.
  return AutoMaterializePolicy(
/Users/dgibson/dagster/python_modules/dagster/dagster/_core/decorator_utils.py:203: ExperimentalWarning: Parameter `auto_materialize_policy` of function `asset` is experimental. It may break in future versions, even between dot releases. To mute warnings for experimental functionality, invoke warnings.filterwarnings("ignore", category=dagster.ExperimentalWarning) or use one of the other methods described at https://docs.python.org/3/library/warnings.html#describing-warning-filters.
  return fn(*args, **kwargs)
```

Now it looks like:
```
2024-06-04 09:43:13 -0500 - dagster - INFO - Launching Dagster services...
2024-06-04 09:43:14 -0500 - dagster - WARNING - /Users/dgibson/dagster/python_modules/dagster/dagster/_core/definitions/auto_materialize_policy.py:247: ExperimentalWarning: Class `AutoMaterializePolicy` is experimental. It may break in future versions, even between dot releases. To mute warnings for experimental functionality, invoke warnings.filterwarnings("ignore", category=dagster.ExperimentalWarning) or use one of the other methods described at https://docs.python.org/3/library/warnings.html#describing-warning-filters.
  return AutoMaterializePolicy(

2024-06-04 09:43:14 -0500 - dagster - WARNING - /Users/dgibson/dagster/python_modules/dagster/dagster/_core/decorator_utils.py:203: ExperimentalWarning: Parameter `auto_materialize_policy` of function `asset` is experimental. It may break in future versions, even between dot releases. To mute warnings for experimental functionality, invoke warnings.filterwarnings("ignore", category=dagster.ExperimentalWarning) or use one of the other methods described at https://docs.python.org/3/library/warnings.html#describing-warning-filters.
  return fn(*args, **kwargs)

```

## Summary & Motivation

## How I Tested These Changes
  • Loading branch information
gibsondan authored Jun 4, 2024
1 parent d1177f1 commit 38e9204
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python_modules/dagster/dagster/_utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging.config
import sys
import traceback
import warnings
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -320,6 +321,14 @@ def configure_loggers(

logging.config.dictConfig(LOGGING_CONFIG)

# override the default warnings handler as per https://docs.python.org/3/library/warnings.html#warnings.showwarning
# to use the same formatting
def custom_warning_handler(message, category, filename, lineno, file=None, line=None):
log_message = warnings.formatwarning(message, category, filename, lineno, line)
logging.getLogger("dagster").warning(log_message)

warnings.showwarning = custom_warning_handler


def create_console_logger(name: str, level: Union[str, int]) -> logging.Logger:
klass = logging.getLoggerClass()
Expand Down

0 comments on commit 38e9204

Please sign in to comment.