Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log from multiple python files into single log file in Python

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

like image 636
Nomadic_coder Avatar asked Aug 15 '17 21:08

Nomadic_coder


People also ask

What is Python logging propagate?

Python Logger A logger has three main fields: Propagate: Decides whether a log should be propagated to the logger's parent. By default, its value is True. A level: Like the log handler level, the logger level is used to filter out “less important” logs.


2 Answers

You can do that using the logging module. Define this function in one of your base modules, and call it in every module you would like to log something.

import logging
def get_logger(name):
    log_format = '%(asctime)s  %(name)8s  %(levelname)5s  %(message)s'
    logging.basicConfig(level=logging.DEBUG,
                        format=log_format,
                        filename='dev.log',
                        filemode='w')
    console = logging.StreamHandler()
    console.setLevel(logging.DEBUG)
    console.setFormatter(logging.Formatter(log_format))
    logging.getLogger(name).addHandler(console)
    return logging.getLogger(name)

Then from every module you want to log from, call this function with the logger name you want, and all your modules would print to the same log file defined by the filename argument.

<base.py>
logger = get_logger('base')
logger.info('testing logger from module base')

<module2.py>
logger = get_logger('module2')
logger.info('testing logger from module module2')

<dev.log>
2017-08-11 00:34:00,361     base   INFO  testing logger from module base
2017-08-11 00:34:00,361     module2   INFO  testing logger from module module2

logging.basicConfig()

like image 62
Chen A. Avatar answered Oct 23 '22 01:10

Chen A.


@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).

like image 30
Yuchen Peng Avatar answered Oct 23 '22 00:10

Yuchen Peng