Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logging yet again #599

Merged
merged 18 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions ocrd/ocrd/cli/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
"""
import click
from ocrd_utils import initLogging, getLogger, getLevelName

initLogging()
import logging

class LogCtx():

def __init__(self, name):
self.logger = getLogger(name)
self.name = name

def log(self, lvl, *args, **kwargs):
self.logger.log(getLevelName(lvl), *args, **kwargs)
logger = getLogger(self.name)
logger.log(getLevelName(lvl), *args, **kwargs)

pass_log = click.make_pass_decorator(LogCtx)

Expand All @@ -23,6 +23,7 @@ def log_cli(ctx, name, *args, **kwargs):
"""
Logging
"""
initLogging()
ctx.obj = LogCtx(name)

def _bind_log_command(lvl):
Expand All @@ -36,5 +37,5 @@ def _log_wrapper(ctx, msgs):
ctx.log(lvl.upper(), msg)
return _log_wrapper

for lvl in ['trace', 'debug', 'info', 'warning', 'error', 'critical']:
log_cli.command(lvl, help="Log a %s message" % lvl.upper())(_bind_log_command(lvl))
for _lvl in ['trace', 'debug', 'info', 'warning', 'error', 'critical']:
log_cli.command(_lvl, help="Log a %s message" % _lvl.upper())(_bind_log_command(_lvl))
6 changes: 2 additions & 4 deletions ocrd/ocrd/cli/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
"""
import click

from ocrd_utils import initLogging, getLogger
from ocrd_utils import getLogger, initLogging
from ocrd.task_sequence import run_tasks

from ..decorators import ocrd_loglevel

initLogging()

# ----------------------------------------------------------------------
# ocrd process
# ----------------------------------------------------------------------
Expand All @@ -23,7 +21,7 @@ def process_cli(log_level, mets, page_id, tasks, overwrite):
"""
Process a series of tasks
"""
initLogging()
log = getLogger('ocrd.cli.process')

run_tasks(mets, log_level, page_id, tasks, overwrite)
log.info("Finished")
8 changes: 2 additions & 6 deletions ocrd/ocrd/cli/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
from ocrd import Resolver, Workspace
from ocrd.task_sequence import ProcessorTask, validate_tasks

from ocrd_utils import (
parse_json_string_or_file,
initLogging
)
from ocrd_utils import initLogging, parse_json_string_or_file
from ocrd_validators import (
OcrdToolValidator,
OcrdZipValidator,
Expand All @@ -19,8 +16,6 @@
WorkspaceValidator,
)

initLogging()

def _inform_of_result(report):
if not report.is_valid:
print(report.to_xml())
Expand All @@ -32,6 +27,7 @@ def validate_cli():
"""
All the validation in one CLI
"""
initLogging()

@validate_cli.command('tool-json')
@click.argument('ocrd_tool', required=False, nargs=1)
Expand Down
13 changes: 6 additions & 7 deletions ocrd/ocrd/cli/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@
import click

from ocrd import Resolver, Workspace, WorkspaceValidator, WorkspaceBackupManager
from ocrd_utils import initLogging, getLogger, pushd_popd, EXT_TO_MIME
from ocrd_utils import getLogger, initLogging, pushd_popd, EXT_TO_MIME
from . import command_with_replaced_help

initLogging()
log = getLogger('ocrd.cli.workspace')

class WorkspaceCtx():

def __init__(self, directory, mets_url, mets_basename, automatic_backup):
self.log = getLogger('ocrd.cli.workspace')
if mets_basename and mets_url:
raise ValueError("Use either --mets or --mets-basename, not both")
if mets_basename and not mets_url:
log.warning(DeprecationWarning("--mets-basename is deprecated. Use --mets/--directory instead"))
self.log.warning(DeprecationWarning("--mets-basename is deprecated. Use --mets/--directory instead"))
mets_basename = mets_basename if mets_basename else 'mets.xml'
if directory and mets_url:
directory = abspath(directory)
Expand Down Expand Up @@ -57,6 +56,7 @@ def workspace_cli(ctx, directory, mets, mets_basename, backup):
"""
Working with workspace
"""
initLogging()
ctx.obj = WorkspaceCtx(directory, mets_url=mets, mets_basename=mets_basename, automatic_backup=backup)

# ----------------------------------------------------------------------
Expand Down Expand Up @@ -212,7 +212,7 @@ def workspace_add_file(ctx, file_grp, file_id, mimetype, page_id, ignore, check_
# ocrd workspace add-bulk
# ----------------------------------------------------------------------

# pylint: disable=bad-whitespace, broad-except
# pylint: disable=broad-except
@workspace_cli.command('bulk-add')
@click.option('-r', '--regex', help="Regular expression matching the FILE_GLOB filesystem paths to define named captures usable in the other parameters", required=True)
@click.option('-m', '--mimetype', help="Media type of the file. If not provided, guess from filename", required=False)
Expand Down Expand Up @@ -317,7 +317,6 @@ def workspace_cli_bulk_add(ctx, regex, mimetype, page_id, file_id, url, file_grp
@click.option('-m', '--mimetype', help="Media type to look for", metavar='FILTER')
@click.option('-g', '--page-id', help="Page ID", metavar='FILTER')
@click.option('-i', '--file-id', help="ID", metavar='FILTER')
# pylint: disable=bad-continuation
@click.option('-k', '--output-field', help="Output field. Repeat for multiple fields, will be joined with tab",
default=['url'],
multiple=True,
Expand Down Expand Up @@ -438,7 +437,7 @@ def prune_files(ctx, file_grp, mimetype, page_id, file_id):
if not f.local_filename or not exists(f.local_filename):
workspace.mets.remove_file(f.ID)
except Exception as e:
log.exception("Error removing %f: %s", f, e)
ctx.log.exception("Error removing %f: %s", f, e)
raise(e)
workspace.save_mets()

Expand Down
3 changes: 1 addition & 2 deletions ocrd/ocrd/cli/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
from ..workspace import Workspace
from ..workspace_bagger import WorkspaceBagger

initLogging()

@click.group("zip")
def zip_cli():
"""
Bag/Spill/Validate OCRD-ZIP bags
"""
initLogging()

# ----------------------------------------------------------------------
# ocrd zip bag
Expand Down
4 changes: 2 additions & 2 deletions ocrd/ocrd/decorators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ..resolver import Resolver
from ..processor.base import run_processor

from .loglevel_option import loglevel_option, ocrd_loglevel
from .loglevel_option import ocrd_loglevel
from .parameter_option import parameter_option, parameter_override_option
from .ocrd_cli_options import ocrd_cli_options

Expand All @@ -37,8 +37,8 @@ def ocrd_cli_wrap_processor(
processorClass(workspace=None, dump_json=dump_json, show_help=help, show_version=version)
sys.exit()
else:
LOG = getLogger('ocrd_cli_wrap_processor')
initLogging()
LOG = getLogger('ocrd_cli_wrap_processor')
# LOG.info('kwargs=%s' % kwargs)
# Merge parameter overrides and parameters
if 'parameter_override' in kwargs:
Expand Down
6 changes: 3 additions & 3 deletions ocrd/ocrd/decorators/loglevel_option.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import click
from ocrd_utils.logging import setOverrideLogLevel

__all__ = ['loglevel_option', 'ocrd_loglevel']
__all__ = ['ocrd_loglevel']

def _set_root_logger_version(ctx, param, value): # pylint: disable=unused-argument
def _setOverrideLogLevel(ctx, param, value): # pylint: disable=unused-argument
setOverrideLogLevel(value)
return value

Expand All @@ -12,7 +12,7 @@ def _set_root_logger_version(ctx, param, value): # pylint: disable=unused-arg
'OFF', 'ERROR', 'WARN',
'INFO', 'DEBUG', 'TRACE'
]),
default=None, callback=_set_root_logger_version)
default=None, callback=_setOverrideLogLevel)

def ocrd_loglevel(f):
"""
Expand Down
4 changes: 1 addition & 3 deletions ocrd/ocrd/processor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

import os
import json
from ocrd_utils import getLogger, VERSION as OCRD_VERSION, MIMETYPE_PAGE
from ocrd_utils import VERSION as OCRD_VERSION, MIMETYPE_PAGE
from ocrd_validators import ParameterValidator
from ocrd_models.ocrd_page import MetadataItemType, LabelType, LabelsType

# XXX imports must remain for backwards-compatibilty
from .helpers import run_cli, run_processor, generate_processor_help # pylint: disable=unused-import

log = getLogger('ocrd.processor')

class Processor():
"""
A processor runs an algorithm based on the workspace, the mets.xml in the
Expand Down
3 changes: 1 addition & 2 deletions ocrd/ocrd/processor/builtin/dummy_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

OCRD_TOOL = parse_json_string_with_comments(resource_string(__name__, 'dummy/ocrd-tool.json').decode('utf8'))

LOG = getLogger('ocrd.dummy')

class DummyProcessor(Processor):
"""
Bare-bones processor that copies mets:file from input group to output group.
"""

def process(self):
LOG = getLogger('ocrd.dummy')
assert_file_grp_cardinality(self.input_file_grp, 1)
assert_file_grp_cardinality(self.output_file_grp, 1)
for input_file in self.input_files:
Expand Down
4 changes: 2 additions & 2 deletions ocrd/ocrd/processor/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
'run_processor'
]

log = getLogger('ocrd.processor')

def _get_workspace(workspace=None, resolver=None, mets_url=None, working_dir=None):
if workspace is None:
if resolver is None:
Expand Down Expand Up @@ -51,6 +49,7 @@ def run_processor(
mets_url,
working_dir
)
log = getLogger('ocrd.processor.helpers.run_processor')
log.debug("Running processor %s", processorClass)
processor = processorClass(
workspace,
Expand Down Expand Up @@ -116,6 +115,7 @@ def run_cli(
args += ['--parameter', parameter]
if overwrite:
args += ['--overwrite']
log = getLogger('ocrd.processor.helpers.run_cli')
log.debug("Running subprocess '%s'", ' '.join(args))
result = run(args, check=False, stdout=PIPE, stderr=PIPE)
return result.returncode, result.stdout.decode('utf-8'), result.stderr.decode('utf-8')
Expand Down
4 changes: 2 additions & 2 deletions ocrd/ocrd/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
from ocrd_models.constants import NAMESPACES as NS
from ocrd_models.utils import handle_oai_response

log = getLogger('ocrd.resolver')

class Resolver():
"""
Handle Uploads, Downloads, Repository access and manage temporary directories
Expand Down Expand Up @@ -121,6 +119,7 @@ def workspace_from_url(self, mets_url, dst_dir=None, clobber_mets=False, mets_ba
Returns:
Workspace
"""
log = getLogger('ocrd.resolver.workspace_from_url')

if mets_url is None:
raise ValueError("Must pass 'mets_url' workspace_from_url")
Expand Down Expand Up @@ -168,6 +167,7 @@ def workspace_from_nothing(self, directory, mets_basename='mets.xml', clobber_me
"""
Create an empty workspace.
"""
log = getLogger('ocrd.resolver.workspace_from_nothing')
if directory is None:
directory = tempfile.mkdtemp(prefix=TMP_PREFIX)
Path(directory).mkdir(parents=True, exist_ok=True)
Expand Down
Loading