diff --git a/README.md b/README.md index 1a8bd0c..ae4d88d 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,8 @@ Example .env: BIBLE_API_KEY="" BIBLE_BIBLE_NAME="New King James Version" BIBLE_BOOK_NAME='Genesis' -BIBLE_ENDPOINT='https://rest.api.bible' BIBLE_THEME="onyx-light" +BIBLE_LOG_LEVEL="info" ``` > Note, although cache expiry days is configurable it should NOT be set above 30 days, as per the terms and conditions of the API. See [Acceptable Use][acceptable-use] diff --git a/src/bible/cli/cli.py b/src/bible/cli/cli.py index f2c5670..1bc0641 100644 --- a/src/bible/cli/cli.py +++ b/src/bible/cli/cli.py @@ -5,6 +5,7 @@ from loguru import logger from typing_extensions import override from bible.api import BibleAPI +from bible.logging import configure_logging from bible.settings import settings from bible.sqlite import cache_json, get_cached, init_db @@ -25,6 +26,11 @@ class BibleCli(Command): help='The name of the Bible to retrieve books from', group='Connection', ) + log_level: str = arg( + settings.LOG_LEVEL, + help='The log level for the Bible CLI', + group='Connection', + ) @final @classmethod @@ -34,6 +40,8 @@ class BibleCli(Command): @override async def pre_run_hook(self): """Hook to run before the main command execution.""" + + configure_logging(self.log_level) init_db() key = 'bibles:list' diff --git a/src/bible/logging.py b/src/bible/logging.py new file mode 100644 index 0000000..099b4c7 --- /dev/null +++ b/src/bible/logging.py @@ -0,0 +1,15 @@ +import sys + +from loguru import logger + +VALID_LEVELS = {'TRACE', 'DEBUG', 'INFO', 'SUCCESS', 'WARNING', 'ERROR', 'CRITICAL'} + + +def configure_logging(level: str): + if level.upper() not in VALID_LEVELS: + raise ValueError( + f'Invalid log level: {level}. Valid levels are: {", ".join(VALID_LEVELS)}' + ) + + logger.remove() + logger.add(sys.stderr, level=level.upper()) diff --git a/src/bible/settings.py b/src/bible/settings.py index 5903456..7e314a0 100644 --- a/src/bible/settings.py +++ b/src/bible/settings.py @@ -8,6 +8,7 @@ class Settings(BaseSettings): API_KEY: str CACHE_EXPIRY_DAYS: int = 30 THEME: str = 'dark' + LOG_LEVEL: str = 'info' model_config = SettingsConfigDict( env_file='.env', diff --git a/src/bible/tui/tui.py b/src/bible/tui/tui.py index f74eae8..4375de5 100644 --- a/src/bible/tui/tui.py +++ b/src/bible/tui/tui.py @@ -9,6 +9,7 @@ from textual.theme import Theme from textual.widgets import Footer, Header, Input, Link, Static from bible.api import BibleAPI +from bible.logging import configure_logging from bible.render import generate_summary, render_chapter, render_verse from bible.settings import settings from bible.sqlite import cache_json, get_cached, init_db, load_json @@ -305,10 +306,18 @@ class BibleTUI(App): def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description='Bible TUI — interact with Bible.API') parser.add_argument('--theme', type=str, help='Specify the theme (light or dark)') + parser.add_argument( + '--log-level', + type=str, + help='Specify the log level (e.g., info, debug, warning)', + ) return parser.parse_args() def main(): args = parse_args() + + configure_logging(args.log_level or settings.LOG_LEVEL) + app = BibleTUI(theme_override=args.theme) app.run()