Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fcwu
GitHub Repository: fcwu/docker-ubuntu-vnc-desktop
Path: blob/develop/rootfs/usr/local/lib/web/backend/log/config.py
387 views
1
#!/usr/bin/env python
2
import sys
3
import logging
4
import logging.handlers
5
6
7
# The terminal has 8 colors with codes from 0 to 7
8
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
9
10
# These are the sequences need to get colored ouput
11
RESET_SEQ = "\033[0m"
12
COLOR_SEQ = "\033[1;%dm"
13
BOLD_SEQ = "\033[1m"
14
15
# The background is set with 40 plus the number of the color,
16
# and the foreground with 30
17
COLORS = {
18
'WARNING': COLOR_SEQ % (30 + YELLOW) + 'WARN ' + RESET_SEQ,
19
'INFO': COLOR_SEQ % (30 + WHITE) + 'INFO ' + RESET_SEQ,
20
'DEBUG': COLOR_SEQ % (30 + BLUE) + 'DEBUG' + RESET_SEQ,
21
'CRITICAL': COLOR_SEQ % (30 + YELLOW) + 'CRITI' + RESET_SEQ,
22
'ERROR': COLOR_SEQ % (30 + RED) + 'ERROR' + RESET_SEQ,
23
}
24
25
26
class ColoredFormatter(logging.Formatter):
27
def __init__(self, msg, use_color=True):
28
logging.Formatter.__init__(self, msg)
29
self.use_color = use_color
30
31
def format(self, record):
32
if self.use_color:
33
record.levelname = COLORS.get(record.levelname, record.levelname)
34
return logging.Formatter.format(self, record)
35
36
37
class LoggingConfiguration(object):
38
COLOR_FORMAT = "%(asctime)s" + \
39
" %(levelname)s %(message)s " + \
40
"(" + BOLD_SEQ + "%(filename)s" + RESET_SEQ + ":%(lineno)d)"
41
NO_COLOR_FORMAT = "%(asctime)s %(levelname)s " + \
42
"%(message)s " + \
43
"(%(filename)s:%(lineno)d)"
44
FILE_FORMAT = "%(asctime)s %(levelname)s " + \
45
"%(message)s "
46
47
@classmethod
48
def set(cls, log_level, log_filename, append=None, **kwargs):
49
""" Configure a rotating file logging
50
"""
51
logger = logging.getLogger()
52
logger.setLevel(log_level)
53
54
COLOR_FORMAT = cls.COLOR_FORMAT
55
NO_COLOR_FORMAT = cls.NO_COLOR_FORMAT
56
FILE_FORMAT = cls.FILE_FORMAT
57
if 'name' in kwargs:
58
COLOR_FORMAT = COLOR_FORMAT.replace('%(threadName)-22s',
59
'%-22s' % (kwargs['name']))
60
NO_COLOR_FORMAT = NO_COLOR_FORMAT.replace(
61
'%(threadName)-22s', '%-22s' % (kwargs['name']))
62
FILE_FORMAT = FILE_FORMAT.replace(
63
'%(threadName)-22s', '%s' % (kwargs['name']))
64
65
# Log to rotating file
66
try:
67
fh = logging.handlers.RotatingFileHandler(
68
log_filename,
69
mode='a+',
70
backupCount=3
71
)
72
fh.setFormatter(ColoredFormatter(FILE_FORMAT, False))
73
fh.setLevel(log_level)
74
logger.addHandler(fh)
75
if not append:
76
# Create a new log file on every new
77
fh.doRollover()
78
except IOError as e:
79
print('ignore to log to {}: {}'.format(log_filename, e))
80
81
# Log to sys.stderr using log level passed through command line
82
if log_level != logging.NOTSET:
83
log_handler = logging.StreamHandler(sys.stdout)
84
if sys.platform.find('linux') >= 0:
85
formatter = ColoredFormatter(COLOR_FORMAT)
86
else:
87
formatter = ColoredFormatter(NO_COLOR_FORMAT, False)
88
log_handler.setFormatter(formatter)
89
log_handler.setLevel(log_level)
90
logger.addHandler(log_handler)
91
92