Skip to content

Commit 8230e9b

Browse files
smtakedaankit-bhatnagar167
authored andcommitted
SNOW-74772: Added log_max_query_length to adjust a query lenght in the
log
1 parent 42f55ee commit 8230e9b

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

connection.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
PARAMETER_CLIENT_STORE_TEMPORARY_CREDENTIAL,
3838
PARAMETER_CLIENT_PREFETCH_THREADS,
3939
)
40-
from .cursor import SnowflakeCursor
40+
from .cursor import SnowflakeCursor, LOG_MAX_QUERY_LENGTH
4141
from .errorcode import (ER_CONNECTION_IS_CLOSED,
4242
ER_NO_ACCOUNT_NAME, ER_OLD_PYTHON, ER_NO_USER,
4343
ER_NO_PASSWORD, ER_INVALID_VALUE,
@@ -131,7 +131,8 @@ def DefaultConverterClass():
131131
u'timezone': None, # snowflake
132132
u'consent_cache_id_token': True, # snowflake
133133
u'service_name': None, # snowflake,
134-
u'support_negative_year': True # snowflake
134+
u'support_negative_year': True, # snowflake
135+
u'log_max_query_length': LOG_MAX_QUERY_LENGTH, # snowflake
135136
}
136137

137138
APPLICATION_RE = re.compile(r'[\w\d_]+')
@@ -427,6 +428,10 @@ def service_name(self):
427428
def service_name(self, value):
428429
self._service_name = value
429430

431+
@property
432+
def log_max_query_length(self):
433+
return self._log_max_query_length
434+
430435
def connect(self, **kwargs):
431436
u"""
432437
Connects to the database
@@ -585,24 +590,30 @@ def __open_connection(self):
585590
self.port)
586591

587592
if 'SF_OCSP_RESPONSE_CACHE_SERVER_URL' in os.environ:
588-
logger.debug(u"Custom OCSP Cache Server URL found in environment - %s", os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'])
593+
logger.debug(
594+
u"Custom OCSP Cache Server URL found in environment - %s",
595+
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'])
589596

590597
if self.host.endswith(u".privatelink.snowflakecomputing.com"):
591598
ocsp_cache_server = \
592599
u'http://ocsp{}/ocsp_response_cache.json'.format(
593600
self.host[self.host.index('.'):])
594601
if 'SF_OCSP_RESPONSE_CACHE_SERVER_URL' not in os.environ:
595-
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'] = ocsp_cache_server
602+
os.environ[
603+
'SF_OCSP_RESPONSE_CACHE_SERVER_URL'] = ocsp_cache_server
596604
else:
597-
if not os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'].\
605+
if not os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL']. \
598606
startswith("http://"):
599-
ocsp_cache_server = "http://{0}/{1}".format(os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'],
600-
"ocsp_response_cache.json")
607+
ocsp_cache_server = "http://{0}/{1}".format(
608+
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'],
609+
"ocsp_response_cache.json")
601610
else:
602-
ocsp_cache_server = "{0}/{1}".format(os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'],
603-
"ocsp_response_cache.json")
611+
ocsp_cache_server = "{0}/{1}".format(
612+
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'],
613+
"ocsp_response_cache.json")
604614

605-
os.environ['SF_OCSP_RESPONSE_CACHE_SERVER_URL'] = ocsp_cache_server
615+
os.environ[
616+
'SF_OCSP_RESPONSE_CACHE_SERVER_URL'] = ocsp_cache_server
606617
logger.debug(u"OCSP Cache Server is updated: %s", ocsp_cache_server)
607618
else:
608619
if 'SF_OCSP_RESPONSE_CACHE_SERVER_URL' in os.environ:
@@ -783,9 +794,7 @@ def cmd_query(self, sql, sequence_counter, request_id,
783794
if logger.getEffectiveLevel() <= logging.DEBUG:
784795
logger.debug(
785796
u'sql=[%s], sequence_id=[%s], is_file_transfer=[%s]',
786-
u' '.join(
787-
line.strip() for line in
788-
data[u'sqlText'].split(u'\n')),
797+
self._format_query_for_log(data[u'sqlText']),
789798
data[u'sequenceId'],
790799
is_file_transfer
791800
)
@@ -1091,6 +1100,11 @@ def _set_parameters(self, ret, session_parameters):
10911100
elif PARAMETER_CLIENT_PREFETCH_THREADS == name:
10921101
self.client_prefetch_threads = value
10931102

1103+
def _format_query_for_log(self, query):
1104+
ret = u' '.join(line.strip() for line in query.split(u'\n'))
1105+
return (ret if len(ret) < self.log_max_query_length
1106+
else ret[0:self.log_max_query_length] + '...')
1107+
10941108
def __enter__(self):
10951109
u"""
10961110
context manager

cursor.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949

5050
logger = getLogger(__name__)
5151

52+
LOG_MAX_QUERY_LENGTH = 80
53+
5254

5355
class SnowflakeCursor(object):
5456
u"""
@@ -65,8 +67,6 @@ class SnowflakeCursor(object):
6567
u(r'alter\s+session\s+set\s+(.*)=\'?([^\']+)\'?\s*;'),
6668
flags=re.IGNORECASE | re.MULTILINE | re.DOTALL)
6769

68-
LOG_MAX_QUERY_LENGTH = 80
69-
7070
def __init__(self, connection):
7171
self._connection = connection
7272

@@ -97,6 +97,8 @@ def __init__(self, connection):
9797

9898
self._first_chunk_time = None
9999

100+
self._log_max_query_length = connection.log_max_query_length
101+
100102
self.reset()
101103

102104
def __del__(self):
@@ -303,9 +305,7 @@ def _execute_helper(
303305

304306
if logger.getEffectiveLevel() <= logging.DEBUG:
305307
logger.debug(
306-
u'running query [%s]',
307-
u' '.join(line.strip() for line in query.split(u'\n')),
308-
)
308+
u'running query [%s]', self._format_query_for_log(query))
309309
if _is_put_get is not None:
310310
# if told the query is PUT or GET, use the information
311311
self._is_file_transfer = _is_put_get
@@ -441,9 +441,10 @@ def execute(self, command, params=None, timeout=None,
441441
# pyformat/format paramstyle
442442
# client side binding
443443
processed_params = self._connection._process_params(params, self)
444-
logger.debug(u'binding: %s with input=%s, processed=%s',
445-
command,
446-
params, processed_params)
444+
if logger.getEffectiveLevel() <= logging.DEBUG:
445+
logger.debug(u'binding: [%s] with input=[%s], processed=[%s]',
446+
self._format_query_for_log(command),
447+
params, processed_params)
447448
if len(processed_params) > 0:
448449
query = command % processed_params
449450
else:
@@ -551,9 +552,7 @@ def execute(self, command, params=None, timeout=None,
551552
return self
552553

553554
def _format_query_for_log(self, query):
554-
ret = u' '.join(line.strip() for line in query.split(u'\n'))
555-
return (ret if len(ret) < SnowflakeCursor.LOG_MAX_QUERY_LENGTH
556-
else ret[0:SnowflakeCursor.LOG_MAX_QUERY_LENGTH] + '...')
555+
return self._connection._format_query_for_log(query)
557556

558557
def _is_dml(self, data):
559558
return u'statementTypeId' in data \

0 commit comments

Comments
 (0)