I’m trying to add some logging to unit tests so it’s not ideal to add a logger to every module or file. I just need to know when a warning from some called function pops up in a sub module/test. These warnings would get printed to the console as well if that helps.
My file structure looks something like this:
test runner
- create logger
- call sub module 1
sub module 1
- tests
- calls sub module 2
sub module 2
- deprecation warning
Currently I have a logger like this, but it can only catch warnings that are in the same file:
logging.basicConfig(filename=logfile, level=logging.WARNING, format=format)
logger_file_handler = logging.RotatingFileHandler(logfile)
logger_file_handler.setLevel(logging.INFO)
logging.captureWarnings(True)
root_logger = logging.getLogger()
root_logger.addHandler(logger_file_handler)
root_logger.setLevel(logging.INFO)
warnings.warn(u'Warning test')
I’ve seen some things saying I need to create a logger in each file for the warnings to bubble up to the root logger. Is that the only way?
Kim TheBookAntari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.