Skip to content

Commit 1a89486

Browse files
author
C. E. Brasseur
authored
Merge pull request #1992 from jwoillez/logging
Added HTTP logging capability
2 parents a6a83cc + 793d7b4 commit 1a89486

File tree

32 files changed

+157
-77
lines changed

32 files changed

+157
-77
lines changed

CHANGES.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ esa.xmm_newton
128128

129129
- Added new function to download EPIC sources metadate. [#1814]
130130

131+
132+
Infrastructure, Utility and Other Changes and Additions
133+
-------------------------------------------------------
134+
135+
- HTTP requests and responses can now be logged when the astropy
136+
logger is set to level "DEBUG" and "TRACE" respectively. [#1992]
137+
- Astroquery and all its modules now uses a logger similar to Astropy's. [#1992]
138+
139+
131140
0.4.1 (2020-06-19)
132141
==================
133142

astroquery/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# ----------------------------------------------------------------------------
1414

1515
import os
16+
import logging
17+
18+
from .logger import _init_log
1619

1720

1821
# Set the bibtex entry to the article referenced in CITATION.
@@ -39,3 +42,9 @@ def _get_bibtex():
3942
except ImportError:
4043
# TODO: Issue a warning using the logging framework
4144
__githash__ = ''
45+
46+
47+
# Setup logging for astroquery
48+
logging.addLevelName(5, "TRACE")
49+
log = logging.getLogger()
50+
log = _init_log()

astroquery/alma/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from six.moves.urllib_parse import urljoin
1717
import six
1818
from astropy.table import Table, Column, vstack
19-
from astropy import log
19+
from astroquery import log
2020
from astropy.utils import deprecated
2121
from astropy.utils.console import ProgressBar
2222
from astropy.utils.exceptions import AstropyDeprecationWarning

astroquery/alma/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88

99
from astropy import wcs
10-
from astropy import log
10+
from astroquery import log
1111
from astropy import units as u
1212
from astropy.io import fits
1313
from astroquery.utils.commons import ASTROPY_LT_4_1

astroquery/astrometry_net/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import six
77
from six import string_types
88
from astropy.io import fits
9-
from astropy import log
9+
from astroquery import log
1010
from astropy.stats import sigma_clipped_stats
1111
from astropy.coordinates import SkyCoord
1212

astroquery/cadc/core.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Module to query the Canadian Astronomy Data Centre (CADC).
88
"""
99

10-
import logging
10+
from astroquery import log
1111
import warnings
1212
import requests
1313
from numpy import ma
@@ -38,8 +38,6 @@
3838

3939
CADC_COOKIE_PREFIX = 'CADC_SSO'
4040

41-
logger = logging.getLogger(__name__)
42-
4341
# TODO figure out what to do if anything about them. Some might require
4442
# fixes on the CADC servers
4543
warnings.filterwarnings('ignore', module='astropy.io.votable')
@@ -192,7 +190,7 @@ def login(self, user=None, password=None, certificate_file=None):
192190
try:
193191
response.raise_for_status()
194192
except Exception as e:
195-
logger.error('Logging error: {}'.format(e))
193+
log.error('Logging error: {}'.format(e))
196194
raise e
197195
# extract cookie
198196
cookie = '"{}"'.format(response.text)
@@ -357,7 +355,7 @@ def get_images(self, coordinates, radius,
357355
images.append(fn.get_fits())
358356
except requests.exceptions.HTTPError as err:
359357
# Catch HTTPError if user is unauthorized to access file
360-
logger.debug(
358+
log.debug(
361359
"{} - Problem retrieving the file: {}".
362360
format(str(err), str(err.url)))
363361
pass
@@ -811,7 +809,7 @@ def get_access_url(service, capability=None):
811809
response = requests.get(conf.CADC_REGISTRY_URL)
812810
response.raise_for_status()
813811
except requests.exceptions.HTTPError as err:
814-
logger.debug(
812+
log.debug(
815813
"ERROR getting the CADC registry: {}".format(str(err)))
816814
raise err
817815
for line in response.text.splitlines():
@@ -835,7 +833,7 @@ def get_access_url(service, capability=None):
835833
response2 = requests.get(caps_url)
836834
response2.raise_for_status()
837835
except Exception as e:
838-
logger.debug(
836+
log.debug(
839837
"ERROR getting the service capabilities: {}".format(str(e)))
840838
raise e
841839

astroquery/casda/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import astropy.coordinates as coord
1313
from astropy.table import Table
1414
from astropy.io.votable import parse
15-
from astropy import log
15+
from astroquery import log
1616

1717
# 3. local imports - use relative imports
1818
# commonly required local imports shown below as example

astroquery/casda/tests/test_casda.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import astropy.units as u
1212
from astropy.table import Table, Column
1313
from astropy.io.votable import parse
14-
from astropy import log
14+
from astroquery import log
1515

1616
from astroquery.casda import Casda
1717

astroquery/cosmosim/core.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Astropy imports
2020
from astropy.table import Table
2121
import astropy.io.votable as votable
22-
from astropy import log as logging
22+
from astroquery import log
2323

2424
# Astroquery imports
2525
from ..query import QueryWithLogin
@@ -72,8 +72,8 @@ def _login(self, username=None, password=None, store_password=False,
7272

7373
# login after login (interactive)
7474
if hasattr(self, 'username'):
75-
logging.warning("Attempting to login while another user ({0}) "
76-
"is already logged in.".format(self.username))
75+
log.warning("Attempting to login while another user ({0}) "
76+
"is already logged in.".format(self.username))
7777
self.check_login_status()
7878
return
7979

@@ -144,7 +144,7 @@ def logout(self, deletepw=False):
144144
del self.username
145145
del self.password
146146
else:
147-
logging.error("You must log in before attempting to logout.")
147+
log.error("You must log in before attempting to logout.")
148148

149149
def check_login_status(self):
150150
"""
@@ -220,8 +220,8 @@ def run_sql_query(self, query_string, tablename=None, queue=None,
220220
gen_tablename = "{0}".format(phase)
221221
else:
222222
gen_tablename = str(soup.find(id="table").string)
223-
logging.warning("Table name {0} is already taken."
224-
.format(tablename))
223+
log.warning("Table name {0} is already taken."
224+
.format(tablename))
225225
warnings.warn("Generated table name: {0}".format(gen_tablename))
226226
elif tablename is None:
227227
result = self._request('POST', CosmoSim.QUERY_URL,
@@ -293,7 +293,7 @@ def check_job_status(self, jobid=None):
293293
auth=(self.username, self.password), data={'print': 'b'},
294294
cache=False)
295295

296-
logging.info("Job {}: {}".format(jobid, response.content))
296+
log.info("Job {}: {}".format(jobid, response.content))
297297
return response.content
298298

299299
def check_all_jobs(self, phase=None, regex=None, sortby=None):
@@ -586,7 +586,7 @@ def completed_job_info(self, jobid=None, output=False):
586586
elif len(dictkeys) == 1:
587587
print(self.response_dict_current[dictkeys[0]]['content'])
588588
else:
589-
logging.error('No completed jobs found.')
589+
log.error('No completed jobs found.')
590590
return
591591
else:
592592
return
@@ -680,7 +680,7 @@ def general_job_info(self, jobid=None, output=False):
680680
auth=(self.username, self.password), cache=False)]
681681

682682
if response_list[0].ok is False:
683-
logging.error('Must provide a valid jobid.')
683+
log.error('Must provide a valid jobid.')
684684
return
685685
else:
686686
self.response_dict_current = {}
@@ -940,7 +940,7 @@ def explore_db(self, db=None, table=None, col=None):
940940
for key in self.db_dict[db].keys()]
941941
size2 = len(max(slist, key=np.size))
942942
except (KeyError, NameError):
943-
logging.error("Must first specify a valid database.")
943+
log.error("Must first specify a valid database.")
944944
return
945945
# if col is specified, table must be specified, and I need to
946946
# check the max size of any given column in the structure
@@ -954,13 +954,13 @@ def explore_db(self, db=None, table=None, col=None):
954954
[table]['columns']
955955
[col].keys())))
956956
except(KeyError, NameError):
957-
logging.error("Must first specify a valid column "
958-
"of the `{0}` table within the `{1}`"
959-
" db".format(table, db))
957+
log.error("Must first specify a valid column "
958+
"of the `{0}` table within the `{1}`"
959+
" db".format(table, db))
960960
return
961961
except (KeyError, NameError):
962-
logging.error("Must first specify a valid table within "
963-
"the `{0}` db.".format(db))
962+
log.error("Must first specify a valid table within "
963+
"the `{0}` db.".format(db))
964964
return
965965

966966
t['Projects'] = (['--> @ {}:'.format(db)] +
@@ -1202,7 +1202,7 @@ def _check_phase(self, jobid):
12021202
time.sleep(1)
12031203

12041204
if jobid not in self.job_dict.keys():
1205-
logging.error("Job not present in job dictionary.")
1205+
log.error("Job not present in job dictionary.")
12061206
return
12071207

12081208
else:
@@ -1291,7 +1291,7 @@ def _initialize_alerting(self, jobid, mail=None, text=None):
12911291
self._smspw = password_from_keyring
12921292

12931293
if not password_from_keyring:
1294-
logging.warning("CosmoSim SMS alerting has not been initialized.")
1294+
log.warning("CosmoSim SMS alerting has not been initialized.")
12951295
warnings.warn("Initializing SMS alerting.")
12961296
keyring.set_password("astroquery:cosmosim.SMSAlert",
12971297
self._smsaddress, "LambdaCDM")

astroquery/esa/hubble/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import os
2929

3030
from . import conf
31-
from astropy import log
31+
from astroquery import log
3232

3333
__all__ = ['ESAHubble', 'ESAHubbleClass']
3434

0 commit comments

Comments
 (0)