-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlogger.py
More file actions
50 lines (37 loc) · 1.28 KB
/
logger.py
File metadata and controls
50 lines (37 loc) · 1.28 KB
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
49
50
import click
def log(tag, color, message, details=None, pad=True, start='', end='\n'):
'''
Log a message to stdout
:param tag: (str) The tag, ex. `info` or `warn`
:param color: (str) The color for the tag (passed to colorama)
:param message: (str) The log message string
:param details: (dict) Extra details to show {'label': 'value'}
:param pad: (bool) Whether the extra detail lines should be padded
'''
formatted_tag = click.style(tag, fg=color, bold=True)
print(f'{start}{formatted_tag} {message}', end=end)
if details:
format_label = lambda text: click.style(text, fg='white', dim=True, bold=True)
for label, msg in details.items():
padding = len(tag) * ' ' + ' ' if pad else ''
print(padding + format_label(label), msg)
def log_trace(*args, **kwargs):
'''
Log a trace message (see log())
'''
log('trace', 'white', *args, **kwargs)
def log_info(*args, **kwargs):
'''
Log an info message (see log())
'''
log('info', 'green', *args, **kwargs)
def log_warn(*args, **kwargs):
'''
Log a warning (see log())
'''
log('warn', 'yellow', *args, **kwargs)
def log_err(*args, **kwargs):
'''
Log an error (see log())
'''
log('err', 'red', *args, **kwargs)