Skip to content

Commit

Permalink
logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
oelarnes committed Jan 5, 2025
1 parent 37d2458 commit 547df26
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
4 changes: 3 additions & 1 deletion spells/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import logging

from spells.columns import ColSpec
from spells.enums import ColType, ColName
from spells.draft_data import summon, view_select, get_names
from spells.log import setup_logging

setup_logging()

__all__ = ["summon", "view_select", "get_names", "ColSpec", "ColType", "ColName"]
__all__ = ["summon", "view_select", "get_names", "ColSpec", "ColType", "ColName", "logging"]
7 changes: 4 additions & 3 deletions spells/draft_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,11 @@ def _fetch_or_cache(

if read_cache:
if cache.cache_exists(set_code, key):
logging.debug(f"Cache {key} found")
logging.info(f"Cache {key} found")
return cache.read_cache(set_code, key)

logging.debug(f"Cache not found, calculating with signature {cache_args}")
logging.info("Cache not found, calculating")
logging.debug(f"Signature:\n{cache_args}")
df = calc_fn()

if write_cache:
Expand Down Expand Up @@ -490,7 +491,7 @@ def summon(

concat_dfs = []
for code in codes:
logging.debug(f"Calculating agg df for {code}")
logging.info(f"Calculating agg df for {code}")
if isinstance(card_context, pl.DataFrame):
set_card_context = card_context.filter(pl.col("expansion") == code)
elif isinstance(card_context, dict):
Expand Down
18 changes: 9 additions & 9 deletions spells/log.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from contextlib import contextmanager
from functools import wraps
import logging
import logging.handlers
from pathlib import Path
Expand Down Expand Up @@ -42,8 +43,9 @@ def rotate_logs():


@contextmanager
def add_console_logging():
def console_logging(log_level):
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(log_level)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
Expand All @@ -57,12 +59,10 @@ def add_console_logging():
logger.removeHandler(console_handler)


def make_verbose(callable: Callable) -> Callable:
def wrapped(*args, verbose=False, **kwargs):
if verbose:
with add_console_logging():
return callable(*args, **kwargs)
else:
return callable(*args, **kwargs)

def make_verbose(func: Callable) -> Callable:
@wraps(func)
def wrapped(*args, logging: int=logging.ERROR, **kwargs):
with console_logging(logging):
return func(*args, **kwargs)
return wrapped

0 comments on commit 547df26

Please sign in to comment.