<br>I have posted a log before, and add another template.<br><br>logging.Formatter('%(asctime)s - %(name)s - File "%(pathname)s", line %(lineno)d, in<%(funcName)s> - %(levelname)s - %(message)s', "%Y-%m-%d %H:%M:%S"), # A template that mimics traceback exceptions and can jump to the place where the log is printed<br><br><br>This template simulates the format of the traceback string, because the traceback string is supported by ides such as pycahrm as a jump, and the log template is made according to that format, then both the pycharm console and the log file can easily jump to the specified line of the specified file .<br><br><br>Install using pip:
pip install -i https://pypi.org/simple/ --upgrade multiprocessing_log_manager You don't need to specify the installation source, and the images of Ali and Douban update the official pypi package regularly.
<em id="__mceDel"><br>Template 3<br>The console log is like this, you can directly click on the link to automatically jump to the specified line of the specified file.<br><br> <img src="//i2.wp.com/img-blog.csdnimg.cn/img_convert/633374bd0522affa610ec561096f4fc4.png" alt="" style="outline: none;"> </em>
Template 4, more friendly to pycahrm<br><br> <img src="//i2.wp.com/img-blog.csdnimg.cn/img_convert/7e7e40c9e7b25f1fce8217331c64bfaf.png" alt="" style="outline: none;">
Using template 4, the linux system will be automatically recognized, because pycahrm has some unsightly background coloring, so use the above style on win<br><br>When the python program judges that it is running on the linux system, the log will automatically switch templates, the style is as follows<br><br>
<br>Emit a separately optimized handler for streamhandler<br><br>
class ColorHandler(logging.Handler): """Colored logs, display different colors according to different levels of logs""" def __init__(self, filter_logger_name_list=None): logging.Handler.__init__(self) self. formatter_new = logging. Formatter( '%(asctime)s - %(name)s - "%(filename)s" - %(funcName)s - %(lineno)d - %(levelname)s - %(message)s', "%Y-%m-%d %H:%M:%S") # Optimize the display and jump of the console log separately, and use a special color for a certain part of the string, mainly for the fourth template, so as to avoid \033 in filehandler and mongohandler @classmethod def _my_align(cls, string, length): if len(string) > length *2: return string custom_length = 0 for w in string: custom_length += 1 if cls._is_ascii_word(w) else 2 if custom_length < length: place_length = length - custom_length string + = ' ' * place_length return string @staticmethod def _is_ascii_word(w): if ord(w) < 128: return True def emit(self, record): """ 30 40 black 31 41 red 32 42 green 33 43 yellow 34 44 blue 35 45 fuchsia 36 46 Cyan blue 37 47 white :type record:logging.LogRecord :return: """ if self. formatter is formatter_dict[4] or self. formatter is self. formatter_new: self. formatter = self. formatter_new if os.name == 'nt': self.__emit_for_fomatter4_pycahrm(record) # If the fourth template is used, and pycahrm runs the py file else: self.__emit_for_fomatter4_linux(record) # If the fourth template is used, the py file is run on linux else: self.__emit(record) # If the first 2 3 templates are used def __emit_for_fomatter4_linux(self, record): try: msg = self. format(record) file_formatter = ' ' * 10 + '\033[7mFile "%s", line %d\033[0m' % (record.pathname, record.lineno) if record.levelno == 10: print('\033[0;32m%s' % self._my_align(msg, 150) + file_formatter) elif record.levelno == 20: print('\033[0;96m%s' % self._my_align(msg, 150) + file_formatter) elif record.levelno == 30: print('\033[0;33m%s' % self._my_align(msg, 150) + file_formatter) elif record.levelno == 40: print('\033[0;35m%s' % self._my_align(msg, 150) + file_formatter) elif record.levelno == 50: print('\033[0;31m%s' % self._my_align(msg, 150) + file_formatter) except (KeyboardInterrupt, SystemExit): raise except: #NOQA self. handleError(record) def __emit_for_fomatter4_pycahrm(self, record): # \033[0;93;107mFile "%(pathname)s", line %(lineno)d, in %(funcName)s\033[0m try: msg = self. format(record) for_linux_formatter = ' ' * 10 + '\033[7m;File "%s", line %d\033[0m' % (record.pathname, record.lineno) file_formatter = ' ' * 10 + '\033[0;93;107mFile "%s", line %d\033[0m' % (record.pathname, record.lineno) if record.levelno == 10: print('\033[0;32m%s\033[0m' % self._my_align(msg, 200) + file_formatter) # green elif record.levelno == 20: print('\033[0;34m%s\033[0m' % self._my_align(msg, 200) + file_formatter) # blue elif record.levelno == 30: print('\033[0;92m%s\033[0m' % self._my_align(msg, 200) + file_formatter) # blue elif record.levelno == 40: print('\033[0;35m%s\033[0m' % self._my_align(msg, 200) + file_formatter) # fuchsia elif record.levelno == 50: print('\033[0;31m%s\033[0m' % self._my_align(msg, 200) + file_formatter) # blood red except (KeyboardInterrupt, SystemExit): raise except: #NOQA self. handleError(record) def __emit(self, record): try: msg = self. format(record) if record.levelno == 10: print('\033[0;32m%s\033[0m' % msg) # green elif record.levelno == 20: print('\033[0;34m%s\033[0m' % msg) # blue elif record.levelno == 30: print('\033[0;92m%s\033[0m' % msg) # blue elif record.levelno == 40: print('\033[0;35m%s\033[0m' % msg) # fuchsia elif record.levelno == 50: print('\033[0;31m%s\033[0m' % msg) # blood red except (KeyboardInterrupt, SystemExit): raise except: #NOQA self. handleError(record)