当前位置: 首页 > 后端技术 > Python

Python3多彩日志包

时间:2023-03-26 00:17:57 Python

installpipinstallcolorful-logger使用1个默认logger可以直接使用默认logger实例输出日志,默认日志级别为warning:fromcolorful_logger.loggerimportloggerwithlogger:logger.debug("This是一条调试消息。”)logger.info(“这是一条信息消息。”)logger.warning(“这是一条警告消息。”)logger.error(“这是一条错误消息。”)logger.critical("Thisisacriticalmessage.")logger.fatal("Thisisafatalmessage.")可以看到,logger需要在with语句中执行,因为这个包使用了QueueListener来调用日志输出。在使用logger输出日志之前,需要调用start方法,使用后需要调用stop方法。我把这两个方法都封装到了with语句中。非特殊场景不需要单独调用start和stop方法。如果调用了start方法,一定要在调用log后执行stop方法。自定义logger还可以自定义名称、日志级别、是否在终端显示、是否将日志保存到文件:fromcolorful_logger.loggerimportget_logger,DEBUGlogger=get_logger(name="sample_logger",level=DEBUG,file_path="./test.log")withget_logger(name="sample_logger",level=DEBUG,file_path="./test.log",file_colorful=True)作为记录器:logger.debug("这是一条调试消息。")logger.info("Thisisainfomessage.")logger.warning("Thisisawarningmessage.")logger.error("Thisisaerrormessage.")logger.critical("Thisisacriticalmessage.")logger.fatal("Thisisafatalmessage.")在with语句之外输出日志可能会出现意想不到的情况,达不到预期的结果。日志文件./test.log内容(例,与上图信息不一致):?[35m[DEBUG]?[0m?[34m2021-06-0520:13:26?[0m?[36msample_logger-?[0m这是一条调试消息。[32m[INFO][0m][34m2021-06-0520:13:26][0m[36msample_logger-[0m这是一条信息消息。[33m[WARN][0m[34m2021-06-0520:13:26][0m[36msample_logger-[0m这是一条警告消息。[31m[错误][0m[34m2021-06-0520:13:26?[0m?[33mtest.py:11?[0m?[36msample_logger-?[0m这是一条错误消息。?[31m[致命]?[0m?[34m2021-06-0520:13:26?[0m?[33米测试。py:12?[0m?[36msample_logger-?[0m这是一条关键消息。?[31m[致命]?[0m?[34m2021-06-0520:13:26?[0m?[33mtest.py:13】[0m?[36msample_logger-?[0m这是一条致命消息。默认情况下,输出到文件的日志不是彩色日志。如果您需要将彩色日志保存在文件中,只需将file_colorful参数设置为True。在这个例子中,彩色日志被保存。彩色日志文件的作用只有一个,就是在终端查看实时日志:tail-ftest.log#或cattest.log,这种方式查看的日志是彩色的。FATAL或者CRITICAL是影响程序运行的严重错误,而且这个方法和python默认日志管理器中的其他方法没有区别,让我百思不得其解。在这个包中,我在fatal方法中加入了sys.exit(1)用于退出程序。如果不想在程序出现严重错误时退出程序,可以调用critical方法。get_logger方法:defget_logger(name:Optional[str]=None,level:int=logging.WARNING,show:bool=True,file_path:Optional[str]=None,file_colorful:bool=False,)->Logger:...namelogger实例名,可以命名不同实例对象调用时的日志loglevellevelloglevel显示是否在终端显示。如果你想使用这个颜色日志包,你通常希望在终端上显示它。file_path是否保存到文件中。默认为None,不为None时,会保存到对应的文件file_colorful保存到文件的日志是否为彩色,默认为False,3个子logger以python默认日志格式保存。定义了一个logger之后,你想要使用这个logger除了name输出日志之外的所有参数。这时候就需要使用child_logger方法来生成一个childlogger。子logger需要在父logger的with语句中执行:fromcolorful_loggerimportget_logger,child_loggerfromcolorful_logger.loggerimportDEBUG#parentloggerlogger=get_logger(name="sample_logger",level=DEBUG,file_path="./test.log")withlogger:logger.error("parenterror")l1=child_logger("l1",logger)l1.error("l1error")l1.fatal("l1fatal")子logger是一样的作为父logger除了名字,不会输出第三方库的日志。在parentlogger的with语句中执行childlogger并不一定意味着一定要在with语句中直接调用。可以在with语句中的一个函数中执行,如:#main.pyfromcolorful_loggerimportget_loggerfromcolorful_logger.loggerimportDEBUGfromother_fileimporttest#parentloggerlogger=get_logger(name="sample_logger",level=DEBUG,file_path="./test.log")withlogger:test()#other_file.pytest_logger=child_logger("test_logger",logger)deftest():test_logger.error("测试错误")