Skip to content

Commit

Permalink
_log: add option to redirect other loggers to same json file (#358)
Browse files Browse the repository at this point in the history
* `_log`: add option to redirect other loggers to same json file

* add `extra` options for logging to json
  • Loading branch information
scottstanie authored Jul 22, 2024
1 parent 2c20bcb commit cfc5f0c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/dolphin/_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,25 @@
__all__ = ["log_runtime"]


def setup_logging(debug: bool = False, filename: PathOrStr | None = None):
def setup_logging(
*,
logger_name: str = "dolphin",
debug: bool = False,
filename: PathOrStr | None = None,
):
config_file = Path(__file__).parent / Path("log-config.json")
with open(config_file) as f_in:
config = json.load(f_in)

if logger_name not in config["loggers"]:
config["loggers"][logger_name] = {"level": "INFO", "handlers": ["stderr"]}

if debug:
config["loggers"]["dolphin"]["level"] = "DEBUG"
config["loggers"][logger_name]["level"] = "DEBUG"

if filename:
config["loggers"]["dolphin"]["handlers"].append("file")
if "file" not in config["loggers"][logger_name]["handlers"]:
config["loggers"][logger_name]["handlers"].append("file")
config["handlers"]["file"]["filename"] = os.fspath(filename)
Path(filename).parent.mkdir(exist_ok=True, parents=True)

Expand All @@ -57,6 +66,7 @@ def setup_logging(debug: bool = False, filename: PathOrStr | None = None):
config["handlers"].pop("file")

logging.config.dictConfig(config)

# Temp work around for tqdm on py312
if sys.version_info.major == 3 and sys.version_info.minor == 12:
os.environ["TQDM_DISABLE"] = "1"
Expand Down
11 changes: 11 additions & 0 deletions src/dolphin/workflows/unwrapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import logging
import time
from pathlib import Path
from typing import Sequence

Expand Down Expand Up @@ -56,6 +57,7 @@ def run(
list of Paths to connected component files created.
"""
t0 = time.perf_counter()
if len(ifg_file_list) != len(cor_file_list):
msg = f"{len(ifg_file_list) = } != {len(cor_file_list) = }"
raise ValueError(msg)
Expand Down Expand Up @@ -93,6 +95,15 @@ def run(
create_overviews(unwrapped_paths, image_type=ImageType.UNWRAPPED)
create_overviews(conncomp_paths, image_type=ImageType.CONNCOMP)

# Dump the used options for JSON parsing
logger.info(
"unwrapping complete",
extra={
"elapsed": time.perf_counter() - t0,
"unwrap_options": unwrap_options.model_dump(mode="json"),
},
)

return (unwrapped_paths, conncomp_paths)


Expand Down
10 changes: 10 additions & 0 deletions src/dolphin/workflows/wrapped_phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime
import logging
import time
from pathlib import Path
from typing import Optional, Sequence, cast

Expand Down Expand Up @@ -53,6 +54,7 @@ def run(
Path to the created SHP counts file.
"""
t0 = time.perf_counter()
setup_logging(debug=debug, filename=cfg.log_file)
if tqdm_kwargs is None:
tqdm_kwargs = {}
Expand Down Expand Up @@ -181,6 +183,14 @@ def run(
**kwargs,
)
)
# Dump the used options for JSON parsing
logger.info(
"wrapped_phase complete",
extra={
"elapsed": time.perf_counter() - t0,
"phase_linking_options": cfg.phase_linking.model_dump(mode="json"),
},
)

# ###################################################
# Form interferograms from estimated wrapped phase
Expand Down

0 comments on commit cfc5f0c

Please sign in to comment.