forked from Fabbi/autoshift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
70 lines (58 loc) · 2.07 KB
/
common.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#############################################################################
#
# Copyright (C) 2018 Fabian Schweinfurth
# Contact: autoshift <at> derfabbi.de
#
# This file is part of autoshift
#
# autoshift is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# autoshift is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with autoshift. If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
import logging
from logging import CRITICAL, DEBUG, ERROR, INFO, NOTSET, WARNING
from os import path
FILEPATH = path.realpath(__file__)
DIRNAME = path.dirname(FILEPATH)
def initLogger():
colors = {
NOTSET: 36,
DEBUG: 33,
INFO: 34,
WARNING: 35,
ERROR: 31,
CRITICAL: "5;31"
}
logger = None
logger = logging.getLogger("autoshift")
def rec_filter(record):
record.module_lineno = ""
if record.levelno == DEBUG:
record.module_lineno = f"\033[1;36m{record.module}:{record.lineno} - "
record.color = colors[record.levelno]
record.spaces = " " * (8 - len(record.levelname))
return True
h = logging.StreamHandler()
bcolor="\033[1;36m"
h.setFormatter(
logging.Formatter(f"{bcolor}%(asctime)s "
f"[\033[1;%(color)sm%(levelname)s\033[0m{bcolor}] "
"\033[0m%(spaces)s"
"%(module_lineno)s"
"\033[0m%(message)s"))
h.addFilter(rec_filter)
logger.handlers = []
logger.addHandler(h)
logger.setLevel(INFO)
return logger
_L = initLogger()