-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogboot.py
50 lines (35 loc) · 1.16 KB
/
logboot.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
36
37
38
39
40
41
42
43
44
45
46
47
import logging
from logging import Handler
import sys
from colorama import Style
def __config_logging():
root = logging.getLogger()
if len(root.handlers) > 0:
print("pass")
return
root.setLevel(logging.DEBUG)
config_formatter()
root.info("logging configured!")
def config_formatter(prefix="", color=None):
root = logging.getLogger()
root.handlers.clear()
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
handler.setFormatter(CustomFormatter(prefix, color))
root.addHandler(handler)
def none_to_empty(anything):
if anything:
return anything
else:
return ""
class CustomFormatter(logging.Formatter):
"""Logging Formatter to add colors and count warning / errors"""
fmt = '%(asctime)s - %(process)d - %(threadName)s - %(message)s'
def __init__(self, prefix="", color=None):
if len(prefix):
prefix += " "
if color:
super().__init__(f'{color}{none_to_empty(prefix)}{CustomFormatter.fmt}{Style.RESET_ALL}')
else:
super().__init__(f'{none_to_empty(prefix)}{CustomFormatter.fmt}')
__config_logging()