Passed
Pull Request — master (#500)
by Konstantin
01:35
created

ocrd.cli.log   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 22
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A LogCtx.__init__() 0 2 1
A LogCtx.log() 0 2 1

2 Functions

Rating   Name   Duplication   Size   Complexity  
A _bind_log_command() 0 6 1
A log_cli() 0 8 1
1
"""
2
Logging CLI
3
"""
4
import click
5
from ocrd_utils import getLogger, getLevelName
6
7
class LogCtx():
8
9
    def __init__(self, name):
10
        self.logger = getLogger(name)
11
12
    def log(self, lvl, *args, **kwargs):
13
        self.logger.log(getLevelName(lvl), *args, **kwargs)
14
15
pass_log = click.make_pass_decorator(LogCtx)
16
17
@click.group("log")
18
@click.option('-n', '--name', envvar='OCRD_TOOL_NAME', default='', metavar='LOGGER_NAME', help='Name of the logger', show_default=True)
19
@click.pass_context
20
def log_cli(ctx, name, *args, **kwargs):
21
    """
22
    Logging
23
    """
24
    ctx.obj = LogCtx(name)
25
26
def _bind_log_command(lvl):
27
    @click.argument('msgs', nargs=-1)
28
    @pass_log
29
    def _log_wrapper(ctx, msgs):
30
        ctx.log(lvl.upper(), *list(msgs))
31
    return _log_wrapper
32
33
for lvl in ['trace', 'debug', 'info', 'warning', 'error', 'critical']:
34
    log_cli.command(lvl, help="Log a %s message" % lvl.upper())(_bind_log_command(lvl))
35