-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
35 lines (28 loc) · 859 Bytes
/
log.py
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
import logging
from prompt_toolkit import print_formatted_text, ANSI
logger = None
class PrintHandler(logging.Handler):
def emit(self, record):
print_formatted_text(ANSI(self.format(record)))
def setup(name: str, debug: bool = False):
"""
Setup a logger with the given name.
"""
global logger
wzlogger = logging.getLogger('werkzeug')
logger = logging.getLogger(name)
if debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)