Skip to content

Commit cf54f6c

Browse files
SNOW-1292908:Initiatie log parser in SnowflakeConnection (#1919)
1 parent ff4b113 commit cf54f6c

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

DESCRIPTION.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne
88

99
# Release Notes
1010

11-
- v3.9.1(TBD)
11+
- v3.9.0(TBD)
1212

13+
- Added easy logging configuration so that users can easily generate log file by setup log config in `$SNOWFLAKE_HOME/config.toml`.
1314
- Improved s3 acceleration logic when connecting to China endpoint.
1415

1516
- v3.8.1(April 09, 2024)

src/snowflake/connector/connection.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
ER_NOT_IMPLICITY_SNOWFLAKE_DATATYPE,
9393
)
9494
from .errors import DatabaseError, Error, OperationalError, ProgrammingError
95+
from .log_configuration import EasyLoggingConfigPython
9596
from .network import (
9697
DEFAULT_AUTHENTICATOR,
9798
EXTERNAL_BROWSER_AUTHENTICATOR,
@@ -383,6 +384,9 @@ def __init__(
383384
If overwriting values from the default connection is desirable, supply
384385
the name explicitly.
385386
"""
387+
# initiate easy logging during every connection
388+
easy_logging = EasyLoggingConfigPython()
389+
easy_logging.create_log()
386390
self._lock_sequence_counter = Lock()
387391
self.sequence_counter = 0
388392
self._errorhandler = Error.default_errorhandler

test/integ/test_easy_logging.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#
2+
# Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
3+
#
4+
5+
from test.integ.conftest import create_connection
6+
7+
import pytest
8+
9+
pytestmark = pytest.mark.skipolddriver
10+
11+
import os.path
12+
from logging import getLogger
13+
from pathlib import Path
14+
15+
try:
16+
import tomlkit
17+
18+
from snowflake.connector.config_manager import CONFIG_MANAGER
19+
from snowflake.connector.constants import CONFIG_FILE
20+
except ModuleNotFoundError:
21+
pass
22+
23+
24+
@pytest.fixture(scope="function")
25+
def log_directory(tmp_path_factory):
26+
return tmp_path_factory.mktemp("log")
27+
28+
29+
@pytest.fixture(scope="function")
30+
def temp_config_file(tmp_path_factory):
31+
return tmp_path_factory.mktemp("config_file_path") / "config.toml"
32+
33+
34+
@pytest.fixture(scope="function")
35+
def config_file_setup(request, temp_config_file, log_directory):
36+
param = request.param
37+
CONFIG_MANAGER.file_path = Path(temp_config_file)
38+
configs = {
39+
"save_logs": {"log": {"save_logs": True, "path": str(log_directory)}},
40+
"no_save_logs": {"log": {"save_logs": False, "path": str(log_directory)}},
41+
}
42+
try:
43+
# create temp config file
44+
with open(temp_config_file, "w") as f:
45+
f.write(tomlkit.dumps(configs[param]))
46+
yield
47+
finally:
48+
# remove created dir and file, including log paths and config file paths
49+
CONFIG_MANAGER.file_path = CONFIG_FILE
50+
51+
52+
@pytest.mark.parametrize("config_file_setup", ["save_logs"], indirect=True)
53+
def test_save_logs(db_parameters, config_file_setup, log_directory):
54+
create_connection("default")
55+
56+
assert os.path.exists(os.path.join(log_directory, "python-connector.log"))
57+
with open(os.path.join(log_directory, "python-connector.log")) as f:
58+
data = f.read()
59+
try:
60+
assert "Snowflake Connector for Python" in data
61+
finally:
62+
# set logger back to default
63+
getLogger("snowflake.connector").setLevel(10)
64+
getLogger("botocore").setLevel(0)
65+
getLogger("boto3").setLevel(0)
66+
67+
68+
@pytest.mark.parametrize("config_file_setup", ["no_save_logs"], indirect=True)
69+
def test_no_save_logs(config_file_setup, log_directory):
70+
create_connection("default")
71+
72+
assert not os.path.exists(os.path.join(log_directory, "python-connector.log"))

0 commit comments

Comments
 (0)