Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KEDRO_LOGGING_CONFIG env variable to specific where to read logging.yml #2535

Merged
merged 14 commits into from
Apr 28, 2023
Merged
2 changes: 2 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
# Upcoming Release 0.18.8

## Major features and improvements
* Added `KEDRO_LOGGING_CONFIG` environment variable, which can be used to configure logging from the beginning of the `kedro` process.
* Removed logs folder from the kedro new project template. File-based logging will remain but just be level INFO and above and go to project root instead.


## Bug fixes and other changes
* Improvements to documentation about configuration.
* Improvements to Jupyter E2E tests.
Expand Down
15 changes: 15 additions & 0 deletions docs/source/logging/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ The project-side logging configuration also ensures that [logs emitted from your

We now give some common examples of how you might like to change your project's logging configuration.

### Using `KEDRO_LOGGING_CONFIG` environment variable

`KEDRO_LOGGING_CONFIG` is an optional environment variable that you can use to specify the path of your logging configuration file, overriding the default path of `conf/base/logging.yml`.

To use this environment variable, set it to the path of your desired logging configuration file before running any Kedro commands. For example, if you have a logging configuration file located at `/path/to/logging.yml`, you can set `KEDRO_LOGGING_CONFIG` as follows:

```bash
export KEDRO_LOGGING_CONFIG=/path/to/logging.yml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same for Windows users?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends, if you are running on bash then it should be the same. I do a quick search that we only mention export for a few other documentations as well. If we want to add mentions for how to make it work with CMD we can open a new ticket to do that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine if it's consistent with how we refer to other environment variables. 👍

```

After setting the environment variable, any subsequent Kedro commands will use the logging configuration file at the specified path.

```{note}
If the `KEDRO_LOGGING_CONFIG` environment variable is not set, Kedro will default to using the logging configuration file at the project's default location of `conf/base/logging.yml`.
```
### Disable file-based logging

You might sometimes need to disable file-based logging, e.g. if you are running Kedro on a read-only file system such as [Databricks Repos](https://docs.databricks.com/repos/index.html). The simplest way to do this is to delete your `conf/base/logging.yml` file. With no project-side logging configuration specified, Kedro uses the default framework-side logging configuration, which does not include any file-based handlers.
Expand Down
13 changes: 7 additions & 6 deletions kedro/framework/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ class _ProjectSettings(LazySettings):
)

def __init__(self, *args, **kwargs):

kwargs.update(
validators=[
self._CONF_SOURCE,
Expand All @@ -133,6 +132,7 @@ def _load_data_wrapper(func):
"""Wrap a method in _ProjectPipelines so that data is loaded on first access.
Taking inspiration from dynaconf.utils.functional.new_method_proxy
"""

# pylint: disable=protected-access
def inner(self, *args, **kwargs):
self._load_data()
Expand Down Expand Up @@ -212,12 +212,13 @@ def configure(self, pipelines_module: Optional[str] = None) -> None:
class _ProjectLogging(UserDict):
# pylint: disable=super-init-not-called
def __init__(self):
"""Initialise project logging with default configuration. Also enable
rich tracebacks."""
default_logging = (Path(__file__).parent / "default_logging.yml").read_text(
encoding="utf-8"
"""Initialise project logging. The path to logging configuration is given in
environment variable KEDRO_LOGGING_CONFIG (defaults to default_logging.yml)."""
path = os.environ.get(
"KEDRO_LOGGING_CONFIG", Path(__file__).parent / "default_logging.yml"
)
self.configure(yaml.safe_load(default_logging))
logging_config = Path(path).read_text(encoding="utf-8")
self.configure(yaml.safe_load(logging_config))
logging.captureWarnings(True)

# We suppress click here to hide tracebacks related to it conversely,
Expand Down
15 changes: 15 additions & 0 deletions tests/framework/project/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# pylint: disable=import-outside-toplevel,unused-import,reimported
import logging
import sys
from pathlib import Path

import pytest
import yaml

from kedro.framework.project import LOGGING, configure_logging

Expand All @@ -27,6 +29,19 @@ def test_default_logging_config():
assert logging.getLogger("kedro").level == logging.INFO


def test_environment_variable_logging_config(monkeypatch, tmp_path):
config_path = Path(tmp_path) / "logging.yml"
monkeypatch.setenv("KEDRO_LOGGING_CONFIG", config_path.absolute())
logging_config = {"version": 1, "loggers": {"kedro": {"level": "WARNING"}}}
with config_path.open("w", encoding="utf-8") as f:
yaml.dump(logging_config, f)
del sys.modules["kedro.framework.project"]
from kedro.framework.project import LOGGING # noqa

assert LOGGING.data == logging_config
assert logging.getLogger("kedro").level == logging.WARNING


def test_configure_logging():
logging_config = {"version": 1, "loggers": {"kedro": {"level": "WARNING"}}}
configure_logging(logging_config)
Expand Down
1 change: 0 additions & 1 deletion tests/ipython/test_ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ def test_load_extension_not_in_kedro_project(self, mocker, caplog):
assert expected_message in log_messages

def test_load_extension_register_line_magic(self, mocker, ipython):

mocker.patch("kedro.ipython._find_kedro_project")
mock_reload_kedro = mocker.patch("kedro.ipython.reload_kedro")
load_ipython_extension(ipython)
Expand Down