Refactor configure_logging

This commit is contained in:
James R. Barlow 2019-05-20 14:54:22 -07:00
parent 7ee0c52a57
commit 2fdaa76a0d
4 changed files with 24 additions and 19 deletions

View File

@ -30,9 +30,7 @@
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
# 'sphinx.ext.mathjax',
]
extensions = ['sphinx.ext.napoleon']
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

View File

@ -37,7 +37,10 @@ def run(args=None):
if hasattr(os, 'nice'):
os.nice(5)
configure_logging(options, manage_root_logger=True)
verbosity = options.verbose
if options.quiet:
verbosity = -1
configure_logging(verbosity, manage_root_logger=True)
result = api_run(options=options)
return result

View File

@ -46,11 +46,11 @@ class TqdmConsole:
self.file.flush()
def configure_logging(options, progress_bar_friendly=True, manage_root_logger=False):
def configure_logging(verbosity, progress_bar_friendly=True, manage_root_logger=False):
"""Set up logging
Library users may wish to use this function if they want their log output to be
similar to ocrmypdf's when run as a command. If not use, the external application
similar to ocrmypdf command line interface. If not used, the external application
should configure logging on its own.
ocrmypdf will perform all of its logging under the `"ocrmypdf"` logging namespace.
@ -61,11 +61,15 @@ def configure_logging(options, progress_bar_friendly=True, manage_root_logger=Fa
Library users may perform additional configuration afterwards.
Args:
options: OCRmyPDF options
progress_bar_friendly (bool): install the TqdmConsole log handler, which is
verbosity: Verbosity level.
* `-1`: Quiet
* `0`: Default
* `1`: Output ocrmypdf debug messages
* `2`: More detailed debugging from ocrmypdf and dependent modules
progress_bar_friendly (bool): Install the TqdmConsole log handler, which is
compatible with the tqdm progress bar; without this log messages will
overwrite the progress bar
manage_root_logger (bool): configure the process's root logger, to ensure
manage_root_logger (bool): Configure the process's root logger, to ensure
all log output is sent through
"""
@ -75,23 +79,27 @@ def configure_logging(options, progress_bar_friendly=True, manage_root_logger=Fa
if progress_bar_friendly:
console = logging.StreamHandler(stream=TqdmConsole(sys.stderr))
if options.quiet:
if verbosity < 0:
console.setLevel(logging.ERROR)
elif options.verbose >= 2:
elif verbosity >= 1:
console.setLevel(logging.DEBUG)
else:
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(levelname)7s - %(message)s')
if options.verbose >= 2:
if verbosity >= 1:
log.setLevel(logging.DEBUG)
if verbosity >= 2:
formatter = logging.Formatter('%(name)s - %(levelname)7s - %(message)s')
console.setFormatter(formatter)
log.addHandler(console)
pdfminer_log = logging.getLogger('pdfminer')
pdfminer_log.setLevel(logging.ERROR)
if verbosity <= 1:
pdfminer_log = logging.getLogger('pdfminer')
pdfminer_log.setLevel(logging.ERROR)
pil_log = logging.getLogger('PIL')
pil_log.setLevel(logging.INFO)
def create_options(*, input_file, output_file, **kwargs):

View File

@ -20,10 +20,6 @@ import argparse
from . import PROGRAM_NAME, VERSION
# -------------
# Parser
def numeric(basetype, min_=None, max_=None):
"""Validator for numeric params"""
min_ = basetype(min_) if min_ is not None else None