forked from flobz/psa_car_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylogger.py
44 lines (34 loc) · 1.62 KB
/
mylogger.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
import logging
from logging.handlers import RotatingFileHandler
LOG_FILE = 'activity.log'
DEBUG_LEVELV_NUM = 9
logging.addLevelName(DEBUG_LEVELV_NUM, "DEBUGV")
class CustomLogger(logging.Logger):
# pylint: disable=too-many-arguments,unused-argument,arguments-differ
def _log(self, level, msg, args, exc_info=None, extra=None, stack_info=False, exc_info_debug=False, **kwargs):
if exc_info_debug and self.isEnabledFor(logging.DEBUG):
exc_info = True
super()._log(level, msg, args, exc_info, extra, stack_info)
def __new_style_log(self, level, msg, args, exc_info=None, extra=None, stack_info=False, **kwargs):
if kwargs.pop('style', "%") == "{": # optional
msg = msg.format(*args)
args = []
self._log(level, msg, args, exc_info, extra, stack_info)
def debugv(self, msg, *args, **kwargs):
if self.isEnabledFor(DEBUG_LEVELV_NUM):
self.__new_style_log(DEBUG_LEVELV_NUM, msg, args, **kwargs)
logging.setLoggerClass(CustomLogger)
# pylint: disable=invalid-name
logger = logging.getLogger("log")
def my_logger(file=LOG_FILE, handler_level=logging.INFO):
global logger
logger.setLevel(handler_level)
formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
file_handler = RotatingFileHandler(file, 'a', 1000000, 1, encoding='utf8')
file_handler.setLevel(handler_level)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(handler_level)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)