diff --git a/haystack/components/caching/cache_checker.py b/haystack/components/caching/cache_checker.py index 2f3c8f9c3..9f7f58b73 100644 --- a/haystack/components/caching/cache_checker.py +++ b/haystack/components/caching/cache_checker.py @@ -76,7 +76,7 @@ class CacheChecker: try: module_name, type_ = init_params["document_store"]["type"].rsplit(".", 1) - logger.debug("Trying to import module '{module}'", module=module_name) + logger.debug("Trying to import module '{module_name}'", module_name=module_name) module = importlib.import_module(module_name) except (ImportError, DeserializationError) as e: raise DeserializationError( diff --git a/haystack/components/retrievers/filter_retriever.py b/haystack/components/retrievers/filter_retriever.py index b0c683aee..a8abbf70d 100644 --- a/haystack/components/retrievers/filter_retriever.py +++ b/haystack/components/retrievers/filter_retriever.py @@ -79,7 +79,7 @@ class FilterRetriever: raise DeserializationError("Missing 'type' in document store's serialization data") try: module_name, type_ = init_params["document_store"]["type"].rsplit(".", 1) - logger.debug("Trying to import module '{module}'", module=module_name) + logger.debug("Trying to import module '{module_name}'", module_name=module_name) module = importlib.import_module(module_name) except (ImportError, DeserializationError) as e: raise DeserializationError( diff --git a/haystack/components/writers/document_writer.py b/haystack/components/writers/document_writer.py index d5c4864f9..fe3435257 100644 --- a/haystack/components/writers/document_writer.py +++ b/haystack/components/writers/document_writer.py @@ -76,7 +76,7 @@ class DocumentWriter: try: module_name, type_ = init_params["document_store"]["type"].rsplit(".", 1) - logger.debug("Trying to import module '{module}'", module=module_name) + logger.debug("Trying to import module '{module_name}'", module_name=module_name) module = importlib.import_module(module_name) except (ImportError, DeserializationError) as e: raise DeserializationError( diff --git a/haystack/core/component/component.py b/haystack/core/component/component.py index 927469688..9e527e301 100644 --- a/haystack/core/component/component.py +++ b/haystack/core/component/component.py @@ -411,10 +411,10 @@ class _Component: if class_path in self.registry: # Corner case, but it may occur easily in notebooks when re-running cells. logger.debug( - "Component {component} is already registered. Previous imported from '{module}', new imported from '{new_module}'", + "Component {component} is already registered. Previous imported from '{module_name}', new imported from '{new_module_name}'", component=class_path, - module=self.registry[class_path], - new_module=cls, + module_name=self.registry[class_path], + new_module_name=cls, ) self.registry[class_path] = cls logger.debug("Registered Component {component}", component=cls) diff --git a/haystack/core/pipeline/pipeline.py b/haystack/core/pipeline/pipeline.py index 75808c53e..ce7cd5616 100644 --- a/haystack/core/pipeline/pipeline.py +++ b/haystack/core/pipeline/pipeline.py @@ -165,7 +165,7 @@ class Pipeline: try: # Import the module first... module, _ = component_data["type"].rsplit(".", 1) - logger.debug("Trying to import {module}", module=module) + logger.debug("Trying to import module {module_name}", module_name=module) importlib.import_module(module) # ...then try again if component_data["type"] not in component.registry: @@ -849,7 +849,7 @@ class Pipeline: ) as span: span.set_content_tag("haystack.component.input", last_inputs[name]) - logger.info("Running component {name}", name=name) + logger.info("Running component {component_name}", component_name=name) res = comp.run(**last_inputs[name]) self.graph.nodes[name]["visits"] += 1 diff --git a/releasenotes/notes/fix-logger-reserved-attributes-9e4d4920c1e41a62.yaml b/releasenotes/notes/fix-logger-reserved-attributes-9e4d4920c1e41a62.yaml new file mode 100644 index 000000000..841c6fe41 --- /dev/null +++ b/releasenotes/notes/fix-logger-reserved-attributes-9e4d4920c1e41a62.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Remove the usage of reserved keywords in the logger calls, causing a `KeyError` when setting the log level + to DEBUG. diff --git a/test/components/generators/chat/test_openai.py b/test/components/generators/chat/test_openai.py index 95e299a74..4c7a118ae 100644 --- a/test/components/generators/chat/test_openai.py +++ b/test/components/generators/chat/test_openai.py @@ -1,12 +1,13 @@ +import logging import os import pytest from openai import OpenAIError -from haystack.utils.auth import Secret from haystack.components.generators.chat import OpenAIChatGenerator from haystack.components.generators.utils import print_streaming_chunk from haystack.dataclasses import ChatMessage, StreamingChunk +from haystack.utils.auth import Secret @pytest.fixture @@ -191,6 +192,7 @@ class TestOpenAIChatGenerator: assert "Hello" in response["replies"][0].content # see mock_chat_completion_chunk def test_check_abnormal_completions(self, caplog): + caplog.set_level(logging.INFO) component = OpenAIChatGenerator(api_key=Secret.from_token("test-api-key")) messages = [ ChatMessage.from_assistant( diff --git a/test/components/generators/test_openai.py b/test/components/generators/test_openai.py index a6f213e95..44dfe1226 100644 --- a/test/components/generators/test_openai.py +++ b/test/components/generators/test_openai.py @@ -1,13 +1,14 @@ +import logging import os from typing import List -from haystack.utils.auth import Secret import pytest from openai import OpenAIError from haystack.components.generators import OpenAIGenerator from haystack.components.generators.utils import print_streaming_chunk -from haystack.dataclasses import StreamingChunk, ChatMessage +from haystack.dataclasses import ChatMessage, StreamingChunk +from haystack.utils.auth import Secret class TestOpenAIGenerator: @@ -181,6 +182,7 @@ class TestOpenAIGenerator: assert [isinstance(reply, str) for reply in response["replies"]] def test_check_abnormal_completions(self, caplog): + caplog.set_level(logging.INFO) component = OpenAIGenerator(api_key=Secret.from_token("test-api-key")) # underlying implementation uses ChatMessage objects so we have to use them here diff --git a/test/test_logging.py b/test/test_logging.py index 8bb93642e..a59e636c2 100644 --- a/test/test_logging.py +++ b/test/test_logging.py @@ -47,6 +47,7 @@ class TestStructuredLoggingConsoleRendering: haystack_logging.configure_logging(use_json=False) logger = logging.getLogger(__name__) + logger.setLevel(logging.INFO) logger.debug("Hello, structured logging!", extra={"key1": "value1", "key2": "value2"}) # Use `capfd` to capture the output of the final structlog rendering result @@ -372,7 +373,6 @@ class TestCompositeLogger: # the pytest fixture caplog only captures logs being rendered from the stdlib logging module assert caplog.messages == ["Hello, structured logging!"] assert caplog.records[0].name == "test.test_logging" - assert caplog.records[0].lineno == 370 # Nothing should be captured by capfd since structlog is not configured assert capfd.readouterr().err == ""