Hierarchical logging help to group logs that are related to the same code flow.
Log style can be styled in Yaml format or in Line format.
fromnrt_logging.loggerimportNrtLoggerfromnrt_logging.logger_managerimportlogger_managerfromnrt_logging.logger_stream_handlersimportManualDepthEnumNAME_1='TEST1'NAME_2='TEST2'classChild:
__logger: NrtLoggerdef__init__(self):
self.__logger=logger_manager.get_logger(NAME_1)
defchild_1(self):
self.__logger.info('Child 1')
self.child_2()
defchild_2(self):
self.__logger.info('Child 2')
classParent:
MSG_1='MSG_1'MSG_2='MSG_2'MSG_3='MSG_3'INCREASE_MSG='INCREASE_MSG'DECREASE_MSG='DECREASE_MSG'__logger: NrtLogger__child: Childdef__init__(self):
self.__logger=logger_manager.get_logger(NAME_1)
self.__child=Child()
defa1(self):
self.__logger.warn(self.MSG_1)
self.__child.child_1()
defa2_manual(self):
self.__logger.info(self.MSG_2)
self.__logger.increase_depth()
self.__logger.info(self.INCREASE_MSG)
self.__logger.decrease_depth()
self.__logger.info(self.DECREASE_MSG)
self.__logger.error(self.MSG_1)
self.a1()
defa3_manual(self):
self.__logger.info(self.MSG_2)
self.__logger.increase_depth()
self.__logger.info(self.INCREASE_MSG)
self.__logger.decrease_depth()
self.__logger.info(self.DECREASE_MSG)
self.__logger.error(self.MSG_3)
defa4_manual(self):
self.__logger.info(self.MSG_1)
self.__logger.info(self.INCREASE_MSG, ManualDepthEnum.INCREASE)
self.__logger.info(self.DECREASE_MSG, ManualDepthEnum.DECREASE)
self.__logger.error(self.MSG_2)fromexamples.demo_classes.demo_classesimportNAME_1, Parentfromnrt_logging.logger_managerimportlogger_managerfromnrt_logging.logger_stream_handlersimport \
ConsoleStreamHandler, LogStyleEnumdeflogging_style(log_style: LogStyleEnum):
sh=ConsoleStreamHandler()
sh.style=log_stylelogger=logger_manager.get_logger(NAME_1)
logger.add_stream_handler(sh)
p=Parent()
p.a1()
deflogging_line_style():
logging_style(LogStyleEnum.LINE)
deflogging_yaml_style():
logging_style(LogStyleEnum.YAML)
logging_yaml_style()Output
---
date: 2022-10-31 17:59:04.653084log_level: WARNpath: demo_classes.py.Parentmethod: a1line_number: 38message: MSG_1children:
- date: 2022-10-31 17:59:04.655071log_level: INFOpath: demo_classes.py.Childmethod: child_1line_number: 16message: Child 1children:
- date: 2022-10-31 17:59:04.656137log_level: INFOpath: demo_classes.py.Childmethod: child_2line_number: 20message: Child 2- log: 2022-10-31 18:16:54.033735 [WARN] [demo_classes.py.Parent.a1:38] MSG_1children:
- log: 2022-10-31 18:16:54.034660 [INFO] [demo_classes.py.Child.child_1:16] Child 1children:
- log: 2022-10-31 18:16:54.036723 [INFO] [demo_classes.py.Child.child_2:20] Child 2fromnrt_logging.log_levelimportLogLevelEnumfromnrt_logging.logger_managerimportlogger_managerfromnrt_logging.logger_stream_handlersimport \
ConsoleStreamHandler, LogStyleEnumsh=ConsoleStreamHandler()
sh.log_level=LogLevelEnum.TRACEsh.style=LogStyleEnum.LINElogger=logger_manager.get_logger('NAME_1')
logger.add_stream_handler(sh)
logger.info('main level log')
logger.increase_depth()
logger.info('child 1')
logger.increase_depth()
logger.info('child 1_1')
logger.decrease_depth()
logger.info('child 2')
logger.decrease_depth()
logger.info('continue main level')Output
- log: 2022-10-31 18:18:34.520544 [INFO] [manual_hierarchy_line_logging_1.py.<module>:13] main level logchildren:
- log: 2022-10-31 18:18:34.522606 [INFO] [manual_hierarchy_line_logging_1.py.<module>:15] child 1children:
- log: 2022-10-31 18:18:34.523784 [INFO] [manual_hierarchy_line_logging_1.py.<module>:17] child 1_1
- log: 2022-10-31 18:18:34.524810 [INFO] [manual_hierarchy_line_logging_1.py.<module>:19] child 2
- log: 2022-10-31 18:18:34.525864 [INFO] [manual_hierarchy_line_logging_1.py.<module>:21] continue main levellog_manager config file in YAML style.
Configure loggers and stream handlers.
parameters are inherited. Parameters that are deeper in YAML file will be taken.
log_level: WARNdate_format: '%Y-%m-%d %H:%M:%S'loggers:
- name: TEST1style: yamllog_line_template: '[$log_level$] <$date$> $message$'stream_handlers:
- type: consolestyle: line
- type: filefile_path: logs/log_test_1.txtlog_level: DEBUGstyle: linedate_format: '%Y'log_line_template: 'Test1 $date$ $message$'
- name: TEST2style: yamlstream_handlers:
- type: filefile_path: logs/log_test_2.txtlog_level: ERRORdate_format: '%Y'log_yaml_elements:
['log_level', 'date', 'message']fromnrt_logging.logger_managerimportlogger_managerCONFIG_FILE_PATH='./config/config1.yaml'logger_manager.set_config(file_path=CONFIG_FILE_PATH)