I have written a Python package that uses the logging
module, and makes extensive use of a third-party C++ library with a Python wrapper. I have been able to print messages within my own package to the console, as well as write them to a file (and with different logging levels for each handler). However, I would like to include messages printed by the third-party library in my log file so that I see the order in which they occur. Here is my MWE:
import logging
# Assume no direct access to this function. (e.g. c++ library)
def third_party_function():
print("Inside of 'third_party_function'.")
def my_helper():
logger.debug("Inside of 'my_helper', before third party call.")
third_party_function()
logger.warning("Finished with third party call.")
root_logger = logging.getLogger()
root_logger.setLevel(logging.NOTSET)
logger = logging.getLogger("mylogger")
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.WARNING)
file_handler = logging.FileHandler(filename="progress.out")
file_handler.setLevel(logging.NOTSET)
logger.addHandler(stream_handler)
logger.addHandler(file_handler)
my_helper()
As it stands, the output to the screen is:
Inside of 'third_party_function'.
Finished with third party call.
and the file progress.out
contains
Inside of 'my_helper', before third party call.
Finished with third party call.
However, the desired progress.out
file is
Inside of 'my_helper', before third party call.
Inside of 'third_party_function'.
Finished with third party call.
There doesn't seem to be a logger that belongs to this third-party library since it is not written in Python.
I am hoping to avoid setting sys.stdout
to a file (as found here) since I would like to be consistent and use the logging
module throughout. Another answer for that same question defines a custom class, but this still doesn't seem to catch the third-party message.