From 21fd6bb415732caad86601d5c106576c8680a69e Mon Sep 17 00:00:00 2001 From: Sean Breckenridge Date: Wed, 6 Sep 2023 16:35:26 -0700 Subject: [PATCH] my.core.logging: compatibility with HPI_LOGS re-adds a removed check for HPI_LOGS, add some docs fix the checks for browserexport/takeout logs to use the computed level from my.core.logging --- doc/MODULE_DESIGN.org | 14 ++++++++++++++ doc/SETUP.org | 4 ++++ my/browser/active_browser.py | 3 +-- my/browser/common.py | 15 ++++++++------- my/browser/export.py | 3 +-- my/core/logging.py | 6 ++++++ my/google/takeout/parser.py | 9 +++------ 7 files changed, 37 insertions(+), 17 deletions(-) diff --git a/doc/MODULE_DESIGN.org b/doc/MODULE_DESIGN.org index d57f8fb8..ae5d4603 100644 --- a/doc/MODULE_DESIGN.org +++ b/doc/MODULE_DESIGN.org @@ -233,3 +233,17 @@ The main goals are: It could be argued that namespace packages and editable installs are a bit complex for a new user to get the hang of, and this is true. But fortunately ~import_source~ means any user just using HPI only needs to follow the instructions when a warning is printed, or peruse the docs here a bit -- there's no need to clone or create your own override to just use the ~all.py~ file. There's no requirement to use this for individual modules, it just seems to be the best solution we've arrived at so far + +* Logging + +The ~my.core~ module exports a ~make_logger~ function which works nicely with +~cachew~ and gives you colored logs. You can use it like this: + +#+begin_src python + from my.core import make_logger + + logger = make_logger(__name__) + + # or to set a custom level + # logger = make_logger(__name__, level='warning') +#+end_src diff --git a/doc/SETUP.org b/doc/SETUP.org index 904331f1..c603febc 100644 --- a/doc/SETUP.org +++ b/doc/SETUP.org @@ -194,6 +194,10 @@ If you only have a few modules set up, lots of them will error for you, which is If you're having issues with ~cachew~ or want to show logs to troubleshoot what may be happening, you can pass the debug flag (e.g., ~hpi --debug doctor my.module_name~) or set the ~HPI_LOGS~ environment variable (e.g., ~HPI_LOGS=debug hpi query my.module_name~) to print all logs, including the ~cachew~ dependencies. ~HPI_LOGS~ could also be used to silence ~info~ logs, like ~HPI_LOGS=warning hpi ...~ +If you want to enable logs for a particular module, you can use the +~LOGGING_LEVEL_~ prefix and then the module name with underscores, like +~LOGGING_LEVEL_my_hypothesis=debug hpi query my.hypothesis~ + If you want ~HPI~ to autocomplete the module names for you, this comes with shell completion, see [[../misc/completion/][misc/completion]] If you have any ideas on how to improve it, please let me know! diff --git a/my/browser/active_browser.py b/my/browser/active_browser.py index 4dc52e47..c25c64dc 100644 --- a/my/browser/active_browser.py +++ b/my/browser/active_browser.py @@ -26,8 +26,7 @@ class config(user_config.active_browser): from sqlite_backup import sqlite_backup from .common import _patch_browserexport_logs - -_patch_browserexport_logs() +_patch_browserexport_logs(__name__) def inputs() -> Sequence[Path]: diff --git a/my/browser/common.py b/my/browser/common.py index 9427f614..84445cc3 100644 --- a/my/browser/common.py +++ b/my/browser/common.py @@ -1,11 +1,12 @@ -import os +from my.core import make_logger from my.core.util import __NOT_HPI_MODULE__ -def _patch_browserexport_logs(): - # patch browserexport logs if HPI_LOGS is present - if "HPI_LOGS" in os.environ: - from browserexport.log import setup as setup_browserexport_logger - from my.core.logging import mklevel +def _patch_browserexport_logs(module_name: str): + # get the logger for the module this is being called from + module_logger = make_logger(module_name) - setup_browserexport_logger(mklevel(os.environ["HPI_LOGS"])) + # grab the computed level (respects HPI_LOGS/LOGGING_LEVEL_ prefixes) and set it on the browserexport logger + from browserexport.log import setup as setup_browserexport_logger + + setup_browserexport_logger(module_logger.level) diff --git a/my/browser/export.py b/my/browser/export.py index 3185d534..e9d6252c 100644 --- a/my/browser/export.py +++ b/my/browser/export.py @@ -26,8 +26,7 @@ class config(user_config.export): logger = LazyLogger(__name__, level="warning") - -_patch_browserexport_logs() +_patch_browserexport_logs(__name__) # all of my backed up databases diff --git a/my/core/logging.py b/my/core/logging.py index 43f07679..14cf684b 100644 --- a/my/core/logging.py +++ b/my/core/logging.py @@ -81,6 +81,12 @@ def get_env_level(name: str) -> Level | None: lvl = os.environ.get(PREFIX + name, None) or os.environ.get(PREFIX + name.replace('.', '_'), None) if lvl is not None: return mklevel(lvl) + # if HPI_LOGS is set, use that. This should override anything the module may set as its default + # this is also set when the user passes the --debug flag in the CLI + # + # check after LOGGING_LEVEL_ prefix since that is more specific + if 'HPI_LOGS' in os.environ: + return mklevel(os.environ['HPI_LOGS']) return None diff --git a/my/google/takeout/parser.py b/my/google/takeout/parser.py index 09cbe57f..9a90c8f2 100644 --- a/my/google/takeout/parser.py +++ b/my/google/takeout/parser.py @@ -52,12 +52,9 @@ class google(user_config): logger = LazyLogger(__name__, level="warning") -# patch TAKEOUT_LOGS to match HPI_LOGS -if "HPI_LOGS" in os.environ: - from google_takeout_parser.log import setup as setup_takeout_logger - from my.core.logging import mklevel - - setup_takeout_logger(mklevel(os.environ["HPI_LOGS"])) +# patch the takeout parser logger to match the computed loglevel +from google_takeout_parser.log import setup as setup_takeout_logger +setup_takeout_logger(logger.level) DISABLE_TAKEOUT_CACHE = "DISABLE_TAKEOUT_CACHE" in os.environ