Passed
Push — master ( 357bc6...5bc456 )
by Konstantin
36s queued 13s
created

ocrd.cli.log._bind_log_command()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 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