Skip to content

Commit

Permalink
feat: enable logging in annotator CLI (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson authored Sep 11, 2024
1 parent 36e8976 commit 10157c3
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/ga4gh/vrs/extras/vcf_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
$ vrs-annotate vcf input.vcf.gz --vcf_out output.vcf.gz --vrs_pickle_out vrs_objects.pkl
"""
from collections.abc import Callable
import pathlib
import logging
import pickle
Expand Down Expand Up @@ -36,10 +37,55 @@ class SeqRepoProxyType(str, Enum):
@click.group()
def _cli() -> None:
"""Annotate input files with VRS variation objects."""
logging.basicConfig(
filename="vrs-annotate.log",
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)


class _LogLevel(str, Enum):
"""Define legal values for `--log_level` option."""
DEBUG = "debug"
INFO = "info"
WARNING = "warning"
ERROR = "error"
CRITICAL = "critical"


def _log_level_option(func: Callable) -> Callable:
"""Provide reusable log level CLI option decorator.
Adds a `--log_level` CLI option to any decorated command. Doesn't pass on any
values, just sets the logging level for this module.
:param func: incoming click command
:return: same command, wrapped with log level option
"""
def _set_log_level(ctx: dict, param: str, value: _LogLevel) -> None: # pylint: disable=unused-argument
level_map = {
_LogLevel.DEBUG: logging.DEBUG,
_LogLevel.INFO: logging.INFO,
_LogLevel.WARNING: logging.WARNING,
_LogLevel.ERROR: logging.ERROR,
_LogLevel.CRITICAL: logging.CRITICAL,
}
logging.getLogger(__name__).setLevel(level_map[value])

wrapped_func = click.option(
"--log_level",
type=click.Choice([v.value for v in _LogLevel.__members__.values()]),
default="info",
help="Set the logging level.",
callback=_set_log_level,
expose_value=False,
is_eager=True
)(func)
return wrapped_func


@_cli.command(name="vcf")
@_log_level_option
@click.argument(
"vcf_in",
nargs=1,
Expand Down

0 comments on commit 10157c3

Please sign in to comment.