Reputation: 149
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
Upvotes: 10
Views: 9185
Reputation: 41
@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
).
Upvotes: 2
Reputation: 11280
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
Upvotes: 8