Skip to content

Commit 983e9c1

Browse files
author
C. E. Brasseur
authored
Merge pull request #2021 from keflavich/fix_logging
BUGFIX: Logger will try to download too-large files
2 parents 3d73db4 + 4b882ca commit 983e9c1

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

astroquery/alma/tests/test_alma_remote.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,3 +677,23 @@ def test_staging_stacking(dataarchive_url):
677677

678678
alma.stage_data(['uid://A001/X13d5/X1d', 'uid://A002/X3216af/X31',
679679
'uid://A001/X12a3/X240'])
680+
681+
682+
@pytest.mark.remote_data
683+
@pytest.mark.skipif("SKIP_SLOW", "Huge data file download")
684+
@pytest.mark.parametrize('dataarchive_url', _test_url_list)
685+
def test_big_download_regression(dataarchive_url):
686+
"""
687+
Regression test for #2020/#2021 - this download fails if logging tries to
688+
load the whole data file into memory.
689+
"""
690+
result = Alma.query({'project_code': '2013.1.01365.S'})
691+
uids = np.unique(result['member_ous_uid'])
692+
files = Alma.get_data_info(uids)
693+
694+
# we may need to change the cache dir for this to work on testing machines?
695+
# savedir='/big/data/path/'
696+
# Alma.cache_dir=savedir
697+
698+
# this is a big one that fails
699+
Alma.download_files([files['access_url'][3]])

astroquery/query.py

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -172,26 +172,38 @@ def __call__(self, *args, **kwargs):
172172
return self.__class__(*args, **kwargs)
173173

174174
def _response_hook(self, response, *args, **kwargs):
175-
# Log request at INFO severity
176-
request_hdrs = '\n'.join(f'{k}: {v}' for k, v in response.request.headers.items())
177-
request_log = textwrap.indent(
178-
f"-----------------------------------------\n"
179-
f"{response.request.method} {response.request.url}\n"
180-
f"{request_hdrs}\n"
181-
f"\n"
182-
f"{response.request.body}\n"
183-
f"-----------------------------------------", '\t')
184-
log.debug(f"HTTP request\n{request_log}")
185-
# Log response at DEBUG severity
186-
response_hdrs = '\n'.join(f'{k}: {v}' for k, v in response.headers.items())
187-
response_log = textwrap.indent(
188-
f"-----------------------------------------\n"
189-
f"{response.status_code} {response.reason} {response.url}\n"
190-
f"{response_hdrs}\n"
191-
f"\n"
192-
f"{response.text}\n"
193-
f"-----------------------------------------", '\t')
194-
log.log(5, f"HTTP response\n{response_log}")
175+
loglevel = log.getEffectiveLevel()
176+
177+
if loglevel >= 10:
178+
# Log request at DEBUG severity
179+
request_hdrs = '\n'.join(f'{k}: {v}' for k, v in response.request.headers.items())
180+
request_log = textwrap.indent(
181+
f"-----------------------------------------\n"
182+
f"{response.request.method} {response.request.url}\n"
183+
f"{request_hdrs}\n"
184+
f"\n"
185+
f"{response.request.body}\n"
186+
f"-----------------------------------------", '\t')
187+
log.debug(f"HTTP request\n{request_log}")
188+
if loglevel >= 5:
189+
# Log response at super-DEBUG severity
190+
response_hdrs = '\n'.join(f'{k}: {v}' for k, v in response.headers.items())
191+
if kwargs.get('stream'):
192+
response_log = textwrap.indent(
193+
f"-----------------------------------------\n"
194+
f"{response.status_code} {response.reason} {response.url}\n"
195+
f"{response_hdrs}\n"
196+
"Streaming Data\n"
197+
f"-----------------------------------------", '\t')
198+
else:
199+
response_log = textwrap.indent(
200+
f"-----------------------------------------\n"
201+
f"{response.status_code} {response.reason} {response.url}\n"
202+
f"{response_hdrs}\n"
203+
f"\n"
204+
f"{response.text}\n"
205+
f"-----------------------------------------", '\t')
206+
log.log(5, f"HTTP response\n{response_log}")
195207

196208
def _request(self, method, url,
197209
params=None, data=None, headers=None,
@@ -387,6 +399,9 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
387399

388400
blocksize = astropy.utils.data.conf.download_block_size
389401

402+
log.debug(f"Downloading URL {url} to {local_filepath} with size {length} "
403+
f"by blocks of {blocksize}")
404+
390405
bytes_read = 0
391406

392407
# Only show progress bar if logging level is INFO or lower.

0 commit comments

Comments
 (0)