Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging #42

Merged
merged 3 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions skycalc_ipy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

from .ui import SkyCalc

import logging
logger = logging.getLogger("astar." + __name__)
logger.addHandler(logging.NullHandler())

from importlib import metadata

__version__ = metadata.version(__package__)
40 changes: 20 additions & 20 deletions skycalc_ipy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@


CACHE_DIR_FALLBACK = ".astar/skycalc_ipy"
logger = logging.getLogger("astar." + __name__)


def get_cache_dir() -> Path:
Expand Down Expand Up @@ -65,12 +66,12 @@
response = client.post(self.url, json=self.params)
response.raise_for_status()
except httpx.RequestError as err:
logging.exception("An error occurred while requesting %s.",
err.request.url)
logger.exception("An error occurred while requesting %s.",

Check warning on line 69 in skycalc_ipy/core.py

View check run for this annotation

Codecov / codecov/patch

skycalc_ipy/core.py#L69

Added line #L69 was not covered by tests
err.request.url)
raise err
except httpx.HTTPStatusError as err:
logging.error("Error response %s while requesting %s.",
err.response.status_code, err.request.url)
logger.error("Error response %s while requesting %s.",

Check warning on line 73 in skycalc_ipy/core.py

View check run for this annotation

Codecov / codecov/patch

skycalc_ipy/core.py#L73

Added line #L73 was not covered by tests
err.response.status_code, err.request.url)
raise err
return response

Expand Down Expand Up @@ -181,11 +182,10 @@
try:
self.params[f"coord_{which}"] = float(indic[which])
except KeyError as err:
logging.exception("%s coordinate not given for the Almanac.",
which)
logger.exception("%s coordinate not given for the Almanac.", which)

Check warning on line 185 in skycalc_ipy/core.py

View check run for this annotation

Codecov / codecov/patch

skycalc_ipy/core.py#L185

Added line #L185 was not covered by tests
raise err
except ValueError as err:
logging.exception("Wrong %s format for the Almanac.", which)
logger.exception("Wrong %s format for the Almanac.", which)

Check warning on line 188 in skycalc_ipy/core.py

View check run for this annotation

Codecov / codecov/patch

skycalc_ipy/core.py#L188

Added line #L188 was not covered by tests
raise err

def _get_jsondata(self, file_path: Path):
Expand Down Expand Up @@ -238,8 +238,8 @@
try:
almdata[key] = jsondata[subsection][value]
except (KeyError, ValueError):
logging.warning("Key '%s/%s' not found in Almanac response.",
subsection, value)
logger.warning("Key '%s/%s' not found in Almanac response.",

Check warning on line 241 in skycalc_ipy/core.py

View check run for this annotation

Codecov / codecov/patch

skycalc_ipy/core.py#L241

Added line #L241 was not covered by tests
subsection, value)

return almdata

Expand Down Expand Up @@ -413,7 +413,7 @@
# identical requests.
self.data[0].header["DATE"] = "2017-01-07T00:00:00"
except Exception as err:
logging.exception(
logger.exception(

Check warning on line 416 in skycalc_ipy/core.py

View check run for this annotation

Codecov / codecov/patch

skycalc_ipy/core.py#L416

Added line #L416 was not covered by tests
"Exception raised trying to get FITS data from %s", url)
raise err

Expand All @@ -422,7 +422,7 @@
try:
self.data.writeto(local_filename, **kwargs)
except (IOError, FileNotFoundError):
logging.exception("Exception raised trying to write fits file.")
logger.exception("Exception raised trying to write fits file.")

Check warning on line 425 in skycalc_ipy/core.py

View check run for this annotation

Codecov / codecov/patch

skycalc_ipy/core.py#L425

Added line #L425 was not covered by tests

def getdata(self):
"""Deprecated feature, just use the .data attribute."""
Expand All @@ -439,22 +439,22 @@
params={"d": tmpdir})
deleter_response = response.text.strip().lower()
if deleter_response != "ok":
logging.error("Could not delete server tmpdir %s: %s",
tmpdir, deleter_response)
logger.error("Could not delete server tmpdir %s: %s",

Check warning on line 442 in skycalc_ipy/core.py

View check run for this annotation

Codecov / codecov/patch

skycalc_ipy/core.py#L442

Added line #L442 was not covered by tests
tmpdir, deleter_response)
except httpx.HTTPError:
logging.exception("Exception raised trying to delete tmp dir %s",
tmpdir)
logger.exception("Exception raised trying to delete tmp dir %s",

Check warning on line 445 in skycalc_ipy/core.py

View check run for this annotation

Codecov / codecov/patch

skycalc_ipy/core.py#L445

Added line #L445 was not covered by tests
tmpdir)

def _update_params(self, updated: Mapping) -> None:
par_keys = self.params.keys()
new_keys = updated.keys()
self.params.update((key, updated[key]) for key in par_keys & new_keys)
logging.debug("Ignoring invalid keywords: %s", new_keys - par_keys)
logger.debug("Ignoring invalid keywords: %s", new_keys - par_keys)

def __call__(self, **kwargs):
"""Send server request."""
if kwargs:
logging.info("Setting new parameters: %s", kwargs)
logger.info("Setting new parameters: %s", kwargs)

self._update_params(kwargs)
self.fix_observatory()
Expand All @@ -474,7 +474,7 @@
status = res["status"]
tmpdir = res["tmpdir"]
except (KeyError, ValueError) as err:
logging.exception(
logger.exception(

Check warning on line 477 in skycalc_ipy/core.py

View check run for this annotation

Codecov / codecov/patch

skycalc_ipy/core.py#L477

Added line #L477 was not covered by tests
"Exception raised trying to decode server response.")
raise err

Expand All @@ -486,7 +486,7 @@
self._retrieve_data(
self.BASE_URL + self.data_url + tmpdir + "/skytable.fits")
except httpx.HTTPError as err:
logging.exception("Could not retrieve FITS data from server.")
logger.exception("Could not retrieve FITS data from server.")

Check warning on line 489 in skycalc_ipy/core.py

View check run for this annotation

Codecov / codecov/patch

skycalc_ipy/core.py#L489

Added line #L489 was not covered by tests
raise err

try:
Expand All @@ -500,7 +500,7 @@
self._delete_server_tmpdir(tmpdir)

else: # print why validation failed
logging.error("Parameter validation error: %s", res["error"])
logger.error("Parameter validation error: %s", res["error"])

Check warning on line 503 in skycalc_ipy/core.py

View check run for this annotation

Codecov / codecov/patch

skycalc_ipy/core.py#L503

Added line #L503 was not covered by tests

def call(self):
"""Deprecated feature, just call the instance."""
Expand Down
9 changes: 5 additions & 4 deletions skycalc_ipy/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
}

PATH_HERE = Path(__file__).parent
logger = logging.getLogger("astar." + __name__)


class SkyCalc:
Expand Down Expand Up @@ -84,10 +85,10 @@ def validate_params(self):
invalid_keys.append(key)

if invalid_keys:
logging.warning("The following entries are invalid:")
logger.warning("The following entries are invalid:")
for key in invalid_keys:
logging.warning("'%s' : %s : %s", key,
self.values[key], self.comments[key])
logger.warning("'%s' : %s : %s", key,
self.values[key], self.comments[key])

return not invalid_keys

Expand Down Expand Up @@ -211,7 +212,7 @@ def get_sky_spectrum(self, return_type="table", filename=None):
points=tbl["lam"].data * tbl["lam"].unit,
lookup_table=tbl["flux"].data * funit,
)
logging.warning(
logger.warning(
"Synphot doesn't accept surface brightnesses \n"
"The resulting spectrum should be multiplied by arcsec-2"
)
Expand Down