1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# coding: utf-8
"""
Logging utility module.
Module aims to configure logging (verbosity, format, etc…)
"""
import typing
import logging
import coloredlogs
def configure(verbosity_count: int = 0,
quiet_count: int = 0,
colored: bool = True) -> None:
"""
Configure main logger according to given verbosity configuration.
:param verbosity_count: Set number of `-v` command line switch.
:param quiet_count: Set number of `-q` command line switch.
:param colored: Define if log output should be colored or not.
Default is to set verbosity on INFO level
"""
log = logging.getLogger()
# Compute verbosity: 10 is for DEBUG and 50 for CRITICAL
# Default is 20 (for INFO). Each verbosity count (-v) will decrease
# level by 10 and each quiet count (-q) will increase level by 10.
level = 20 - (verbosity_count * 10) + (quiet_count * 10)
level = max(0, min(60, level))
log.setLevel(level)
log.handlers = []
formatter_class: typing.Type[logging.Formatter]
if colored:
formatter_class = coloredlogs.ColoredFormatter
else:
formatter_class = logging.Formatter
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
handler.setFormatter(
formatter_class('%(levelname)s\t%(message)s')
)
log.addHandler(handler)
|