Skip to content

Commit 9b00342

Browse files
chinandrewAnanya-Joshi
authored andcommitted
Switch CDC Covidnet to use structed logger
1 parent 000dc8b commit 9b00342

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

cdc_covidnet/delphi_cdc_covidnet/covidnet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77

88
import json
9-
import logging
9+
from logging import Logger
1010
import os
1111
from typing import Tuple, List
1212
from multiprocessing import cpu_count, Pool
@@ -100,7 +100,7 @@ def download_hosp_data(
100100

101101
@staticmethod
102102
def download_all_hosp_data(
103-
mappings_file: str, cache_path: str, parallel: bool = False
103+
mappings_file: str, cache_path: str, logger: Logger, parallel: bool = False
104104
) -> List[str]:
105105
"""
106106
Download hospitalization data for all states listed in the mappings JSON file to disk.
@@ -146,7 +146,7 @@ def download_all_hosp_data(
146146
else:
147147
for args in state_args:
148148
CovidNet.download_hosp_data(*args)
149-
logging.debug("Downloading for nid=%s, cid=%s", args[0], args[1])
149+
logger.debug("Downloading for nid=%s, cid=%s", args[0], args[1])
150150

151151
return state_files
152152

cdc_covidnet/delphi_cdc_covidnet/run.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
This module should contain a function called `run_module`, that is executed
55
when the module is run with `python -m delphi_cdc_covidnet`.
66
"""
7-
import logging
87
from datetime import datetime
98
from os import remove
109
from os.path import join
1110
from typing import Dict, Any
1211

12+
from delphi_utils import get_structured_logger
13+
1314
from .covidnet import CovidNet
1415
from .update_sensor import update_sensor
1516

@@ -32,7 +33,9 @@ def run_module(params: Dict[str, Dict[str, Any]]):
3233
- "wip_signal": list of str or bool, to be passed to delphi_utils.add_prefix.
3334
- "input_cache_dir": str, directory to download source files.
3435
"""
35-
logging.basicConfig(level=logging.DEBUG)
36+
logger = get_structured_logger(
37+
__name__, filename=params["common"].get("log_filename"),
38+
log_exceptions=params["common"].get("log_exceptions", True))
3639

3740
start_date = datetime.strptime(params["indicator"]["start_date"], "%Y-%m-%d")
3841

@@ -42,23 +45,24 @@ def run_module(params: Dict[str, Dict[str, Any]]):
4245
else:
4346
end_date = datetime.strptime(params["indicator"]["end_date"], "%Y-%m-%d")
4447

45-
logging.info("start date:\t%s", start_date.date())
46-
logging.info("end date:\t%s", end_date.date())
48+
logger.info("start date:\t%s", start_date.date())
49+
logger.info("end date:\t%s", end_date.date())
4750

48-
logging.info("outpath:\t%s", params["common"]["export_dir"])
49-
logging.info("parallel:\t%s", params["indicator"]["parallel"])
51+
logger.info("outpath:\t%s", params["common"]["export_dir"])
52+
logger.info("parallel:\t%s", params["indicator"]["parallel"])
5053

5154
# Only geo is state, and no weekday adjustment for now
5255
# COVID-NET data is by weeks anyway, not daily
53-
logging.info("starting state, no adj")
56+
logger.info("starting state, no adj")
5457

5558
# Download latest COVID-NET files into the cache directory first
5659
mappings_file = join(params["indicator"]["input_cache_dir"], "init.json")
5760
CovidNet.download_mappings(outfile=mappings_file)
5861
_, mmwr_info, _ = CovidNet.read_mappings(mappings_file)
5962
state_files = CovidNet.download_all_hosp_data(
6063
mappings_file, params["indicator"]["input_cache_dir"],
61-
parallel=params["indicator"]["parallel"])
64+
parallel=params["indicator"]["parallel"],
65+
logger=logger)
6266

6367
update_sensor(
6468
state_files,
@@ -73,4 +77,4 @@ def run_module(params: Dict[str, Dict[str, Any]]):
7377
for state_file in state_files:
7478
remove(state_file)
7579

76-
logging.info("finished all")
80+
logger.info("finished all")

cdc_covidnet/tests/test_covidnet.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import logging
23
from os.path import join, exists
34
from tempfile import TemporaryDirectory
45

@@ -7,6 +8,7 @@
78
from delphi_cdc_covidnet.api_config import APIConfig
89
from delphi_cdc_covidnet.covidnet import CovidNet
910

11+
TEST_LOGGER = logging.getLogger()
1012

1113
class TestCovidNet:
1214

@@ -65,14 +67,14 @@ def test_hosp_data(self):
6567

6668
# Non-parallel
6769
state_files = CovidNet.download_all_hosp_data(
68-
init_file, temp_dir, parallel=False)
70+
init_file, temp_dir, TEST_LOGGER, parallel=False)
6971
assert len(state_files) == num_states
7072
for state_file in state_files:
7173
assert exists(state_file)
7274

7375
# Parallel
7476
state_files_par = CovidNet.download_all_hosp_data(
75-
init_file, temp_dir, parallel=True)
77+
init_file, temp_dir, TEST_LOGGER, parallel=True)
7678
assert set(state_files) == set(state_files_par)
7779
assert len(state_files_par) == num_states
7880
for state_file in state_files_par:

0 commit comments

Comments
 (0)