Skip to content

Commit

Permalink
logging: extract get_logging_config_files function, use to skip tests…
Browse files Browse the repository at this point in the history
… if logging.conf present
  • Loading branch information
kba committed Jul 16, 2024
1 parent 9bf0e7d commit 11b76c9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/ocrd_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
disableLogging,
getLevelName,
getLogger,
get_logging_config_files,
initLogging,
setOverrideLogLevel,
)
Expand Down
22 changes: 14 additions & 8 deletions src/ocrd_utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ def setOverrideLogLevel(lvl, silent=not config.OCRD_LOGGING_DEBUG):
print(f'[LOGGING] Overriding ocrd log level to {lvl}', file=sys.stderr)
ocrd_logger.setLevel(lvl)

def get_logging_config_files():
"""
Return a list of all ``ocrd_logging.conf`` files found in CWD, HOME or /etc.
"""
CONFIG_PATHS = [
Path.cwd(),
Path.home(),
Path('/etc'),
]
return [f for f \
in [p / 'ocrd_logging.conf' for p in CONFIG_PATHS] \
if f.exists()]

def initLogging(builtin_only=False, force_reinit=False, silent=not config.OCRD_LOGGING_DEBUG):
"""
Reset ``ocrd`` logger, read logging configuration if exists, otherwise use basicConfig
Expand Down Expand Up @@ -164,14 +177,7 @@ def initLogging(builtin_only=False, force_reinit=False, silent=not config.OCRD_L

config_file = None
if not builtin_only:
CONFIG_PATHS = [
Path.cwd(),
Path.home(),
Path('/etc'),
]
config_file = [f for f \
in [p / 'ocrd_logging.conf' for p in CONFIG_PATHS] \
if f.exists()]
config_file = get_logging_config_files()
if config_file:
if len(config_file) > 1 and not silent:
print(f"[LOGGING] Multiple logging configuration files found at {config_file}, using first one", file=sys.stderr)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
ocrd_loglevel,
ocrd_cli_wrap_processor,
) # pylint: disable=protected-access
from ocrd_utils import pushd_popd, VERSION as OCRD_VERSION, disableLogging, initLogging
from ocrd_utils import pushd_popd, VERSION as OCRD_VERSION, disableLogging, initLogging, get_logging_config_files

@click.command()
@ocrd_cli_options
Expand Down Expand Up @@ -60,6 +60,8 @@ def test_loglevel_invalid(self):
assert "'foo' is not one of" in err

def test_loglevel_override(self):
if get_logging_config_files():
pytest.skip(f"ocrd_logging.conf found at {get_logging_config_files()}, skipping logging test")
import logging
disableLogging()
assert logging.getLogger('ocrd').getEffectiveLevel() == logging.WARNING
Expand Down

0 comments on commit 11b76c9

Please sign in to comment.