17 printLevel = logging.INFO + 5
19 printMethodName =
'print'
21 def logForLevel(self, message, *args, **kwargs):
22 if self.isEnabledFor(logging.PRINT):
23 self._log(logging.PRINT, message, args, **kwargs)
24 def logToRoot(message, *args, **kwargs):
25 logging.log(logging.PRINT, message, *args, **kwargs)
27 logging.addLevelName(printLevel, printName)
28 setattr(logging, printName, printLevel)
29 setattr(logging.getLoggerClass(), printMethodName, logForLevel)
30 setattr(logging, printMethodName, logToRoot)
31 logging.addLevelName(logging.INFO + 1,
'PRINT')
34 class CustomFormatter(logging.Formatter):
35 """Custom formatter."""
37 self._default_fmt = fmt
38 super().
__init__(fmt, datefmt=
"%m-%d %H:%M")
41 if record.levelno == logging.PRINT:
42 self._style._fmt =
"%(message)s"
44 self._style._fmt = self._default_fmt
45 return super().
format(record)
47 fileFormatter = CustomFormatter(
"%(asctime)s %(levelname)-8s %(message)s")
48 fileHandler = logging.FileHandler(f
"./{name}.log", mode=
"w")
49 fileHandler.setFormatter(fileFormatter)
51 streamFormatter = CustomFormatter(
"%(levelname)-8s %(message)s")
52 streamHandler = logging.StreamHandler(stdout)
53 streamHandler.setFormatter(streamFormatter)
55 logger = logging.getLogger()
56 logger.addHandler(fileHandler)
57 logger.addHandler(streamHandler)
58 logger.setLevel(logging.INFO)