Log from multiple python files into single log file in Python

Viewed 7404

I have multiple python files, I need to log from multiple python files into a single common log file. I dont want to use the log configuration file for formatting or json dictionary. Each of the python files are imported into a master python file kickstart.py and they are all called from kickstart.py. I want to log all messages from various python modules called from master python script into a single log file. Please suggest methods do it. i went through python cookbook but that was not helpful.I am very new to this.

Thanks

2 Answers

@Chen's answer is right. I'm providing a specific use case as follow:

Say you have two python files py1.py, py2.py that you want to import to your master file master.py.

#py1
import logging
def fun1():
    LOGGER = logging.getLogger(__name__)
    LOGGER.info('fun1 runs')

#py2
import logging
def fun2():
    LOGGER = logging.getLogger(__name__)
    LOGGER.info('fun2 runs')

#master.py
import py1
import py2
import logging
def main():
    logging.basicConfig(filename='log.log',level=logging.INFO)
    LOGGER = logging.getLogger("main")
    py1.fun1()
    py2.fun2()
    LOGGER.info('Master runs')

if __name__ == "__main__":
    main()

Then executing master.py will generate one single log.log file that contains logging from all modules (py1.py and py2.py).

Related