Skip to content

Commit

Permalink
my.core.logging: compatibility with HPI_LOGS
Browse files Browse the repository at this point in the history
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
  • Loading branch information
purarue committed Sep 6, 2023
1 parent c283e54 commit 21fd6bb
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 17 deletions.
14 changes: 14 additions & 0 deletions doc/MODULE_DESIGN.org
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions doc/SETUP.org
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
3 changes: 1 addition & 2 deletions my/browser/active_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
15 changes: 8 additions & 7 deletions my/browser/common.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 1 addition & 2 deletions my/browser/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions my/core/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
9 changes: 3 additions & 6 deletions my/google/takeout/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 21fd6bb

Please # to comment.