Skip to content

Commit

Permalink
cli flag should globally override loggers, fix #173
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed Sep 17, 2018
1 parent 118c9fd commit cd2a0b0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
10 changes: 5 additions & 5 deletions ocrd/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from ocrd.constants import VERSION as OCRD_VERSION
from ocrd.resolver import Resolver
from ocrd.processor.base import run_processor
from ocrd.utils import logging
from ocrd.utils import setOverrideLogLevel

def _set_root_logger_version(ctx, param, value):
logging.getLogger('').setLevel(logging.getLevelName(value))
def _set_root_logger_version(ctx, param, value): # pylint: disable=unused-argument
setOverrideLogLevel(value)

def ocrd_cli_wrap_processor(processorClass, ocrd_tool=None, mets=None, working_dir=None, dump_json=False, version=False, **kwargs):
if dump_json:
Expand Down Expand Up @@ -49,8 +49,8 @@ def cli(mets_url):
click.option('-p', '--parameter', type=click.Path()),
click.option('-J', '--dump-json', help="Dump tool description as JSON and exit", is_flag=True, default=False),
click.option('-l', '--log-level', help="Log level",
type=click.Choice(['OFF', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE']),
default='INFO', callback=_set_root_logger_version),
type=click.Choice(['OFF', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE']),
default='INFO', callback=_set_root_logger_version),
click.option('-V', '--version', help="Show version", is_flag=True, default=False)
]
for param in params:
Expand Down
34 changes: 30 additions & 4 deletions ocrd/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
These files will be executed in the context of ocrd/ocrd_logging.py, with `logging` global set.
"""
# pylint: disable=no-member

from __future__ import absolute_import

import logging
import os

__all__ = ['logging', 'getLogger', 'getLevelName']
__all__ = ['logging', 'getLogger', 'getLevelName', 'setOverrideLogLevel']

_overrideLogLevel = None

_ocrdLevel2pythonLevel = {
'TRACE': 'DEBUG',
'OFF': 'NOTSET',
'CRITICAL': 'ERROR',
'OFF': 'CRITICAL',
'FATAL': 'ERROR',
}

Expand All @@ -32,8 +35,31 @@ def getLevelName(lvl):
lvl = _ocrdLevel2pythonLevel.get(lvl, lvl)
return logging.getLevelName(lvl)

def setOverrideLogLevel(lvl):
"""
Override all logger filter levels to include lvl and above.
- Set root logger level
- iterates all existing loggers and sets their log level to ``NOTSET``.
Args:
lvl (string): Log level name.
"""
lvl = getLevelName(lvl)
_overrideLogLevel = lvl
logging.getLogger('').setLevel(lvl)
for loggerName in logging.Logger.manager.loggerDict:
logger = logging.Logger.manager.loggerDict[loggerName]
if isinstance(logger, logging.PlaceHolder):
continue
logger.setLevel(logging.NOTSET)

def getLogger(*args, **kwargs):
return logging.getLogger(*args, **kwargs)
logger = logging.getLogger(*args, **kwargs)
if _overrideLogLevel is not None:
logger.setLevel(logging.NOTSET)
return logger

# Default logging config

Expand Down
2 changes: 1 addition & 1 deletion ocrd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import re
import sys

from ocrd.logging import getLogger, logging
from ocrd.logging import *

def points_from_xywh(box):
"""
Expand Down

0 comments on commit cd2a0b0

Please sign in to comment.