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()