diff --git a/docs/source/logging/index.md b/docs/source/logging/index.md index 6e2eba1561..7b0226e3bd 100644 --- a/docs/source/logging/index.md +++ b/docs/source/logging/index.md @@ -42,6 +42,17 @@ After setting the environment variable, any subsequent Kedro commands use the lo If the `KEDRO_LOGGING_CONFIG` environment variable is not set, Kedro will use the [default logging configuration](https://github.com/kedro-org/kedro/blob/main/kedro/framework/project/default_logging.yml). ``` +## Custom `CONF_SOURCE` with logging + +When you customise the [`CONF_SOURCE`](../configuration/configuration_basics.md#how-to-change-the-configuration-source-folder-at-runtime) setting in your Kedro project, it determines where Kedro looks for configuration files, including the logging configuration file. However, changing `CONF_SOURCE` does not automatically update the path to `logging.yml`. To use a custom location or filename for the logging configuration, you must explicitly set the `KEDRO_LOGGING_CONFIG` environment variable. + +By default, Kedro looks for a file named `logging.yml` in the `conf` directory. If you move or rename your logging configuration file after changing `CONF_SOURCE`, specify the new path using the `KEDRO_LOGGING_CONFIG` environment variable: +```bash +export KEDRO_LOGGING_CONFIG=/custom_config_folder/custom_logging_name.yml +``` + +Please note that adjusting `CONF_SOURCE` or renaming `logging.yml` without updating the logging configuration accordingly can lead to Kedro not locating the file, which will result in the default logging settings being used instead. + ### How to show DEBUG level messages To see `DEBUG` level messages, change the level of logging in your project-specific logging configuration file (`logging.yml`). We provide a `logging.yml` template: diff --git a/kedro/framework/project/__init__.py b/kedro/framework/project/__init__.py index ea56f5d668..b996b08387 100644 --- a/kedro/framework/project/__init__.py +++ b/kedro/framework/project/__init__.py @@ -216,13 +216,34 @@ def configure(self, pipelines_module: str | None = None) -> None: class _ProjectLogging(UserDict): def __init__(self) -> None: """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" - ) + environment variable KEDRO_LOGGING_CONFIG (defaults to conf/logging.yml).""" + + # Check if a user path is set in the environment variable + user_logging_path = os.environ.get("KEDRO_LOGGING_CONFIG") + + # Check if the default logging configuration exists + default_logging_path = Path("conf/logging.yml") + if not default_logging_path.exists(): + default_logging_path = Path(__file__).parent / "default_logging.yml" + + # Use the user path if available, otherwise, use the default path + if user_logging_path and Path(user_logging_path).exists(): + path = Path(user_logging_path) + else: + path = default_logging_path + + # Load and apply the logging configuration logging_config = Path(path).read_text(encoding="utf-8") self.configure(yaml.safe_load(logging_config)) + # Log info about the logging configuration + if not user_logging_path and default_logging_path == Path("conf/logging.yml"): + logger = logging.getLogger(__name__) + logger.info( + f"Using `{path}` as logging configuration. " + f"You can change this by setting the KEDRO_LOGGING_CONFIG environment variable accordingly." + ) + def configure(self, logging_config: dict[str, Any]) -> None: """Configure project logging using ``logging_config`` (e.g. from project logging.yml). We store this in the UserDict data so that it can be reconfigured diff --git a/tests/framework/project/test_logging.py b/tests/framework/project/test_logging.py index 3ce6020d65..b2a1174cbe 100644 --- a/tests/framework/project/test_logging.py +++ b/tests/framework/project/test_logging.py @@ -144,3 +144,33 @@ def test_rich_traceback_disabled_on_databricks( rich_traceback_install.assert_not_called() rich_pretty_install.assert_called() + + +def test_default_logging_info_emission(monkeypatch, capsys): + # Expected path and logging configuration + expected_path = Path("conf/logging.yml") + dummy_logging_config = yaml.dump( + {"version": 1, "loggers": {"kedro": {"level": "INFO"}}} + ) + + # Setup environment and path mocks + monkeypatch.delenv("KEDRO_LOGGING_CONFIG", raising=False) + monkeypatch.setattr(Path, "exists", lambda x: x == expected_path) + monkeypatch.setattr( + Path, + "read_text", + lambda x, encoding="utf-8": dummy_logging_config + if x == expected_path + else FileNotFoundError("File not found"), + ) + + from kedro.framework.project import _ProjectLogging + + _ProjectLogging() + + captured = capsys.readouterr() + + expected_message = f"Using `{expected_path}`" + assert ( + expected_message in captured.out + ), f"Expected message not found in logs: {captured.out}"