Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure logging handlers so all loggers show up in openHTF logger and test record #337

Open
wallacbe opened this issue Jun 22, 2016 · 2 comments

Comments

@wallacbe
Copy link
Collaborator

Configure logging handlers so all loggers show up in openHTF logger and test record

@fahhem
Copy link
Collaborator

fahhem commented Feb 25, 2017

I do this by first creating the htf.Test() object, getting its uid, getting the logger for that uid, then proxying logs into that logger like so:

class PassAlongHandler(logging.Handler):

  def __init__(self, handler):
    self._handler = handler
    super(PassAlongHandler, self).__init__()

  def handle(self, record):
    self._handler.handle(record)


def ProxyLogger(src, dest):
  src.handlers = [PassAlongHandler(dest)]


def my_setup_logs(test_uid):
  testrun_logger = logs.get_record_logger_for(test_uid)
  testrun_base_logger = logging.getLogger(logs.RECORD_LOGGER)
  # Replace openhtf's root handler with a pass-along to the test record.
  openhtf_logger = logging.getLogger('openhtf')
  ProxyLogger(openhtf_logger, testrun_logger)

I do that for the 'openhtf' logger, as well as my org's base logger. This does assume only 1 test running in the process at a time, but that's okay for us for now.

@silver-kuusik
Copy link

silver-kuusik commented Feb 4, 2021

Hey @fahhem thanks for the info on the UID. I am still a bit unsure when can I get a test UID. When I do htf.Test().uid I get None. Am I missing something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants