Package: daiolog
Do asyncio loggingJSON logging in a separate thread for asyncio projectsBasic Usageimportosimportloggingimportlogging.configfromdaiologimportQueueListenerLOGGING_CONFIG={'version':1,'disable_existing_loggers':True,'handlers':{'default':{'level':'INFO','class':'daiolog.QueueHandler',},},'loggers':{'':{# root logger'handlers':['default'],'level':'WARNING','propagate':False},'my.packg':{'handlers':['default'],'level':'INFO','propagate':False},}}logging.config.dictConfig(LOGGING_CONFIG)defmain():logger=logging.getLogger('my.packg')logger.info('Start main',extra={'pid':os.getpid()})...logger.info('Finish main',extra={'pid':os.getpid()})if__name__=='__main__':QueueListener().start()main()QueueListener().stop()# {"logger_name": "my.packg", "level": "INFO", "timestamp": "2023-01-16T09:21:43.511+00:00", "message": "Start main", "pathname": "__main__.py", "module": "__main__", "function": "main", "line": 35, "traceback": null, "extra": {"pid": 60720}}# {"logger_name": "my.packg", "level": "INFO", "timestamp": "2023-01-16T09:21:43.512+00:00", "message": "Finish main", "pathname": "__main__.py", "module": "__main__", "function": "main", "line": 37, "traceback": null, "extra": {"pid": 60720}}Usage with decoratorWith dict configimportosimportloggingimportlogging.configimportdaiologLOGGING_CONFIG={'version':1,'disable_existing_loggers':True,'handlers':{'default':{'level':'INFO','class':'daiolog.QueueHandler',},},'loggers':{'':{# root logger'handlers':['default'],'level':'WARNING','propagate':False},'my.packg':{'handlers':['default'],'level':'INFO','propagate':False},}}@daiolog.entrypoint(LOGGING_CONFIG)defmain():logger=logging.getLogger('my.packg')logger.info('Start main',extra={'pid':os.getpid()})...logger.info('Finish main',extra={'pid':os.getpid()})if__name__=='__main__':main()# {"logger_name": "my.packg", "level": "INFO", "timestamp": "2023-01-16T09:21:43.511+00:00", "message": "Start main", "pathname": "__main__.py", "module": "__main__", "function": "main", "line": 35, "traceback": null, "extra": {"pid": 60720}}# {"logger_name": "my.packg", "level": "INFO", "timestamp": "2023-01-16T09:21:43.512+00:00", "message": "Finish main", "pathname": "__main__.py", "module": "__main__", "function": "main", "line": 37, "traceback": null, "extra": {"pid": 60720}}With file configimportosimportloggingimportlogging.configimportdaiolog@daiolog.entrypoint('./logging.conf')defmain():logger=logging.getLogger('my.packg')logger.info('Start main',extra={'pid':os.getpid()})...logger.info('Finish main',extra={'pid':os.getpid()})if__name__=='__main__':main()# {"logger_name": "my.packg", "level": "INFO", "timestamp": "2023-01-16T09:21:43.511+00:00", "message": "Start main", "pathname": "__main__.py", "module": "__main__", "function": "main", "line": 35, "traceback": null, "extra": {"pid": 60720}}# {"logger_name": "my.packg", "level": "INFO", "timestamp": "2023-01-16T09:21:43.512+00:00", "message": "Finish main", "pathname": "__main__.py", "module": "__main__", "function": "main", "line": 37, "traceback": null, "extra": {"pid": 60720}}With functionimportosimportloggingimportlogging.configimportdaiologdefget_logging_config():returnos.environ.get('LOGGING_FILE_CONFIG','./logging.conf')@daiolog.entrypoint(get_logging_config)defmain():logger=logging.getLogger('my.packg')logger.info('Start main',extra={'pid':os.getpid()})...logger.info('Finish main',extra={'pid':os.getpid()})if__name__=='__main__':main()# {"logger_name": "my.packg", "level": "INFO", "timestamp": "2023-01-16T09:21:43.511+00:00", "message": "Start main", "pathname": "__main__.py", "module": "__main__", "function": "main", "line": 35, "traceback": null, "extra": {"pid": 60720}}# {"logger_name": "my.packg", "level": "INFO", "timestamp": "2023-01-16T09:21:43.512+00:00", "message": "Finish main", "pathname": "__main__.py", "module": "__main__", "function": "main", "line": 37, "traceback": null, "extra": {"pid": 60720}}Release Notes1.1.0Add entrypoint function decorator(daiolog.entrypoint) for config logging and start/stopQueueListener