I'd like to add additional logging to pytest framework test cases. Currently my idea is like this: Logger clas with the following configuration (let's say default)
import logging
class Logger:
logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
In the conftest I am creating a fixture which is actually an instance of the Logger:
#conftest.py
@pytest.fixture
def trace():
trace = Logger()
return trace
Then I am passing this trace fixture to every test where logging is needed.
trace.logger.info("Processing data")
value = input_data.data["value1"]
It does work but I am not sure if there is something better to have one common logger for every test case. Currently it is also needed to pass this fixture to any test I want to add more traces.