Repository: dbt-labs/dbt-snowflake
Pull request #768 (2023-09-11 23:19:58)

Add debug logging
set snowflake connector log level on env variable using new dbt-core methods: https://github.com/dbt-labs/dbt-core/pull/8643

resolves #
[docs](https://github.com/dbt-labs/docs.getdbt.com/issues/new/choose) dbt-labs/docs.getdbt.com/#

<!---
  Include the number of the issue addressed by this PR above if applicable.
  PRs for code changes without an associated issue *will not be merged*.
  See CONTRIBUTING.md for more information.

  Include the number of the docs issue that was opened for this PR. If
  this change has no user-facing implications, "N/A" suffices instead. New
  docs tickets can be created by clicking the link above or by going to
  https://github.com/dbt-labs/docs.getdbt.com/issues/new/choose.
-->

### Problem

<!---
  Describe the problem this PR is solving. What is the application state
  before this PR is merged?
-->

### Solution

<!---
  Describe the way this PR solves the above problem. Add as much detail as you
  can to help reviewers understand your changes. Include any alternatives and
  tradeoffs you considered.
-->

### Checklist

- [ ] I have read [the contributing guide](https://github.com/dbt-labs/dbt-core/blob/main/CONTRIBUTING.md) and understand what's expected of me
- [ ] I have run this code in development and it appears to resolve the stated issue
- [ ] This PR includes tests, or tests are not required/relevant for this PR
- [ ] This PR has no interface changes (e.g. macros, cli, logs, json artifacts, config files, adapter interface, etc) or this PR has already received feedback and approval from Product or DX


github-actions[bot]: Thank you for your pull request! We could not find a changelog entry for this change. For details on how to document a change, see the [dbt-snowflake contributing guide](https://github.com/dbt-labs/dbt-snowflake/blob/main/CONTRIBUTING.md).

Patch:
diff --git a/.changes/unreleased/Under the Hood-20230925-120144.yaml b/.changes/unreleased/Under the Hood-20230925-120144.yaml
new file mode 100644
index 0000000..717f415
--- /dev/null
+++ b/.changes/unreleased/Under the Hood-20230925-120144.yaml
@@ -0,0 +1,6 @@
+kind: Under the Hood
+body: allow for adding snowflake-python-collector logs to dbt output
+time: 2023-09-25T12:01:44.778426-07:00
+custom:
+  Author: colin-rogers-dbt
+  Issue: "768"
diff --git a/dbt/adapters/snowflake/connections.py b/dbt/adapters/snowflake/connections.py
index 4bb00f4..b5fa300 100644
--- a/dbt/adapters/snowflake/connections.py
+++ b/dbt/adapters/snowflake/connections.py
@@ -1,5 +1,7 @@
 import base64
 import datetime
+import os
+
 import pytz
 import re
 from contextlib import contextmanager
@@ -47,6 +49,12 @@ from dbt.ui import line_wrap_message, warning_tag
 
 
 logger = AdapterLogger("Snowflake")
+
+if os.getenv("DBT_SNOWFLAKE_CONNECTOR_DEBUG_LOGGING"):
+    for logger_name in ["snowflake.connector", "botocore", "boto3"]:
+        logger.debug(f"Setting {logger_name} to DEBUG")
+        logger.set_adapter_dependency_log_level(logger_name, "DEBUG")
+
 _TOKEN_REQUEST_URL = "https://{}.snowflakecomputing.com/oauth/token-request"
 
 ERROR_REDACTION_PATTERNS = {


Test patch:
diff --git a/tests/unit/test_connections.py b/tests/unit/test_connections.py
new file mode 100644
index 0000000..87b0cf4
--- /dev/null
+++ b/tests/unit/test_connections.py
@@ -0,0 +1,25 @@
+import os
+from importlib import reload
+from unittest.mock import Mock
+import dbt.adapters.snowflake.connections as connections
+import dbt.events
+
+
+def test_connections_sets_logs_in_response_to_env_var(monkeypatch):
+    """Test that setting the DBT_SNOWFLAKE_CONNECTOR_DEBUG_LOGGING environment variable happens on import"""
+    log_mock = Mock()
+    monkeypatch.setattr(dbt.events, "AdapterLogger", Mock(return_value=log_mock))
+    monkeypatch.setattr(os, "environ", {"DBT_SNOWFLAKE_CONNECTOR_DEBUG_LOGGING": "true"})
+    reload(connections)
+
+    assert log_mock.debug.call_count == 3
+    assert log_mock.set_adapter_dependency_log_level.call_count == 3
+
+
+def test_connections_does_not_set_logs_in_response_to_env_var(monkeypatch):
+    log_mock = Mock()
+    monkeypatch.setattr(dbt.events, "AdapterLogger", Mock(return_value=log_mock))
+    reload(connections)
+
+    assert log_mock.debug.call_count == 0
+    assert log_mock.set_adapter_dependency_log_level.call_count == 0