Package: colorful-logger
:warning: This README is translated by Google. If there is a grammatical error, please open an issue to correct it!python-loggerColorful logger for python3How to useInstallpipinstallcolorful-loggerUsage1 default loggerYou can directly use the default logger, the colored logs will be printed on the terminal, and the default logger level iswarning.fromcolorful_loggerimportloggerwithlogger:logger.debug("default logger")logger.info("default logger")logger.warning("default logger")logger.error("default logger")As you can see,loggerneeds to be executed in thewithstatement, because this package usesQueueListenerto call log output. You need to call thestartmethod before usingloggerto output the log, and you need to call thestopafter the end of use. I encapsulated these two methods in thewithstatement. In non-special scenarios, there is no need to call thestartandstopmethods separately.2 custom loggerYou can also change the log level, save the log to a file, change the logger name, and the log may not be output to the terminal.fromcolorful_loggerimportget_logger,DEBUGdefdemo_logger(to_file=False):file="test_%d.log"l1=get_logger("demo",DEBUG,add_file_path=False,disable_line_number_filter=False,file_path=file%1ifto_fileelseNone,)withl1:l1.debug("without file path")l1.info("without file path")l1.warning("without file path")l1.error("without file path")l2=get_logger("demo",DEBUG,add_file_path=True,disable_line_number_filter=False,file_path=file%2ifto_fileelseNone,)withl2:l2.debug("with file path")l2.info("with file path")l2.warning("with file path")l2.error("with file path")l3=get_logger(None,DEBUG,add_file_path=True,disable_line_number_filter=True,file_path=file%3ifto_fileelseNone,)withl3:l3.debug("without name, and with path")l3.info("without name, and with path")l3.warning("without name, and with path")l3.error("without name, and with path")l4=get_logger(None,DEBUG,add_file_path=False,disable_line_number_filter=True,file_path=file%4ifto_fileelseNone,)withl4:l4.debug("without name and path")l4.info("without name and path")l4.warning("without name and path")l4.error("without name and path")There may be unexpected situations when outputting logs outside of thewithstatement, which may not achieve the expected results.The content of the log file./test.log(example, inconsistent with the information in the above figure):[90m10:09:33.146[0m [35mDEB[0m [36mdemo[0m[1m:26[0m [96m-[0m without file path
[90m10:09:33.146[0m [32mINF[0m [36mdemo[0m [96m-[0m without file path
[90m10:09:33.146[0m [33mWAR[0m [36mdemo[0m [96m-[0m without file path
[90m10:09:33.146[0m [91mERR[0m [36mdemo[0m[1m:29[0m [96m-[0m without file pathThe log output to the file is not a color log by default.If you need to save the color log in a file, set thefile_colorfulparameter toTrue. In this example, the color log is saved.The color log file has only one function, which is to view the real-time log in the terminal:Unixtail-ftest.log# 或cattest.logWindowsGet-Content-Pathtest.log3 child loggerAfter defining alogger, I want to use all the parameters of thisloggerexceptnameto output the log. At this time, you need to use thechild_loggermethod to generate a child logger. The child logger needs to be in thewithof the parent logger Execute in the statement:fromcolorful_loggerimportget_logger,DEBUG# parent loggerlogger=get_logger(name="sample_logger",level=DEBUG,file_path="./test.log")withlogger:logger.error("parent error")l1=logger.child("l1")l1.error("l1 error")l1.fatal("l1 fatal")The child logger is the same except that the name is different from the parent logger, and it will not output the log of the third-party library.The execution of the child logger in thewithstatement of the parent logger does not mean that it must be called directly in thewithstatement. It can be executed in a function in thewithstatement, such as:# log.pyfromcolorful_loggerimportget_logger,DEBUGlogger=get_logger(name="sample_logger",level=DEBUG,file_path="./test.log")# main.pyfromlogimportloggerfromother_fileimporttestwithlogger:test()# other_file.pytest_logger=logger.child("test_logger")deftest():test_logger.error("test error")