test-executor.py
import CustomLogger
from CustomExecutor import iterative_executor
def main():
logging = CustomLogger.Logger('sample-run', {
'cnt' : 10
})
logging.info("Starting run")
iterative_executor(1)
logging.info("Done with execution")
if __name__ == '__main__':
main()
CustomExecutor.py
import CustomLogger
# from CustomLogger import Logger as logging
logging = CustomLogger.Logger('sample-run', {
'cnt' : 10
})
def iterative_executor(iter_cnt=2):
for i in range(iter_cnt):
logging.info(f"Current iteration cnt {i}")
CustomLogger.py
import json
import logging
import os
class StreamLogger(logging.Logger):
def __init__(self, name, k_dict={}, level=logging.DEBUG):
super().__init__(name, level)
streamHandler = logging.StreamHandler()
formatter = logging.Formatter('[%(asctime)s] [%(funcName)s] %(message)s')
streamHandler.setFormatter(formatter)
self.addHandler(streamHandler)
class Logger(logging.Logger):
def __init__(self, name, k_dict, level=logging.DEBUG):
super().__init__(name, level)
self.k_dict = k_dict
self.json_logging = False
if 'TRANSFORM_TO_JSON' in os.environ:
self.json_logging = True
self.stream_logger = StreamLogger(name, k_dict)
def info(self, msg, *args, **kwargs):
if self.json_logging:
self.stream_logger.info(self._transform_to_json(msg), *args, **kwargs)
else:
self.stream_logger.info(msg, *args, **kwargs)
def json_transform(self, msg):
msg_dict = self.k_dict
msg_dict['message'] = msg
return json.dumps(msg_dict)
Code above with CustomLogger to emit a trace id with each and every log message.
It works, if I instantiate the logger as
logging = CustomLogger.Logger('sample-run', {
'cnt' : 10
})
in every file. I don’t want to do that as the tracker / trace id need to be set once as part of instantiating logging via test-executor.py and not in any other code. I want to set the context of track / trace id via test-executor.py and to consume everywhere the custom logger is being used. CustomLogger is more complicated, for the sake of presenting the question, I have made it simple.
In the CustomExecutor.py which is invoked from test-executor.py, if I set it as,
from CustomLogger import Logger as logging
it doesn’t work as it expects the logger to be initialized.
I want the CustomLogger to work out of the box similar to default logging, which has to be instantiated only once.
Any pointers on this?