Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove custom datalad log handlers while enable library mode
ATM, if there is a root level logger set by Python application, which is a logical thing to do for an application, then datalad log messages end up duplicated -- once they are provided by the root level log handler and then by our very own one. E.g. this script import logging ## The simplest and documented way to enable logging for a Python app logging.basicConfig(level=logging.INFO, format='%(name)s : %(asctime)s - %(levelname)s - %(message)s') # Alternative way to enable some root level logging # which surprised me #logging.info('This is an info message') import datalad.support.gitrepo as r r.lgr.info("datalad logger talking") datalad_lgr = logging.getLogger("datalad") print(f"logging has handlers: {logging.getLogger().handlers}") print(f"DataLad has handlers: {datalad_lgr.handlers}" would produce following output: ❯ python simplest_app2.py [INFO ] datalad logger talking datalad.gitrepo : 2023-10-24 16:27:02,703 - INFO - datalad logger talking logging has handlers: [<StreamHandler <stderr> (NOTSET)>] DataLad has handlers: [<ProgressHandler (NOTSET)>] so you see our log line duplicated! With the proposed change, we will explicitly remove all assigned handlers when enabling library mode, and if we add to above script the datalad.enable_librarymode() we get ❯ python simplest_app2.py datalad.gitrepo : 2023-10-24 16:29:25,478 - INFO - datalad logger talking logging has handlers: [<StreamHandler <stderr> (NOTSET)>] DataLad has handlers: [] We also have to respect config variable so we could handle properly the DATALAD_RUNTIME_LIBRARYMODE environment variable.
- Loading branch information