Skip to content

Commit

Permalink
update logging to redirect everything to logfile, if provided
Browse files Browse the repository at this point in the history
  • Loading branch information
scottstanie committed Oct 10, 2023
1 parent 32984d4 commit baaadf8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/disp_s1/_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import logging
from logging import Formatter
from pathlib import Path

from dolphin._types import Filename


def setup_file_logging(filename: Filename) -> None:
"""Redirect all logging to a file."""
logger = logging.getLogger()
# In addition to stderr, log to a file if requested
Path(filename).parent.mkdir(parents=True, exist_ok=True)
file_handler = logging.FileHandler(filename)
file_handler.setLevel(logging.DEBUG)
formatter = Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
7 changes: 5 additions & 2 deletions src/disp_s1/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from dolphin.workflows._utils import _create_burst_cfg, _remove_dir_if_empty
from dolphin.workflows.config import Workflow

from disp_s1 import product
from disp_s1 import _log, product
from disp_s1.pge_runconfig import RunConfig


Expand All @@ -39,7 +39,10 @@ def run(
PGE-specific metadata for the output product.
"""
# Set the logging level for all `dolphin.` modules
logger = get_log(name="disp_s1", debug=debug, filename=cfg.log_file)
logger = get_log(name="disp_s1", debug=debug)
if cfg.log_file:
_log.setup_file_logging(cfg.log_file)

logger.debug(pformat(cfg.model_dump()))
cfg.create_dir_tree(debug=debug)

Expand Down
8 changes: 5 additions & 3 deletions src/disp_s1/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def __dir__(self):
return list(self._attr_cache.keys())


def create_explorer_widget(hf: h5py.File, load_less_than: float = 1e3):
def create_explorer_widget(
hf: h5py.File, load_less_than: float = 1e3
) -> widgets.Widget:
"""Make a widget in Jupyter to explore a h5py file.
Examples
Expand All @@ -145,7 +147,7 @@ def create_explorer_widget(hf: h5py.File, load_less_than: float = 1e3):
>>> create_explorer_widget(hf)
"""

def _make_thumbnail(image):
def _make_thumbnail(image) -> widgets.Image:
# Create a thumbnail of the dataset
fig, ax = plt.subplots(figsize=(5, 5))
ax.imshow(image, cmap="gray", vmax=np.nanpercentile(image, 99))
Expand All @@ -157,7 +159,7 @@ def _make_thumbnail(image):
# Display the thumbnail in an Image widget
return widgets.Image(value=buf.read(), format="png")

def _add_widgets(item, level: int = 0):
def _add_widgets(item, level: int = 0) -> widgets.Widget:
"""Recursively add widgets to the accordion widget."""
if isinstance(item, h5py.Group):
# Add a new accordion widget for the group
Expand Down

0 comments on commit baaadf8

Please sign in to comment.