logging module and doctest

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Jefferson

    #1

    logging module and doctest

    I've written a logging.filter and would like to use doctest on it
    (using a StreamHandler for stdout), but this doesn't seem possible.
    Output from the logger seems to disappear (running the doctest strings
    through the interpreter as-is yields expected results). I assume this
    is because doctest does something with logging. Is there any way to
    make these work together?

    Gary

  • Peter Otten

    #2
    Re: logging module and doctest

    Gary Jefferson wrote:
    I've written a logging.filter and would like to use doctest on it
    (using a StreamHandler for stdout), but this doesn't seem possible.
    Output from the logger seems to disappear (running the doctest strings
    through the interpreter as-is yields expected results). I assume this
    is because doctest does something with logging.
    It redirects stdout to a StringIO subclass to capture the output.
    Is there any way to make these work together?
    Using the StreamHandler with something like

    class WrapStdOut(obje ct):
    def __getattr__(sel f, name):
    return getattr(sys.std out, name)

    instead of sys.stdout directly should work.

    Peter

    Comment

    • Peter Otten

      #3
      Re: logging module and doctest

      Peter Otten wrote:
      Gary Jefferson wrote:
      >
      >I've written a logging.filter and would like to use doctest on it
      >(using a StreamHandler for stdout), but this doesn't seem possible.
      >Output from the logger seems to disappear (running the doctest strings
      >through the interpreter as-is yields expected results). I assume this
      >is because doctest does something with logging.
      >
      It redirects stdout to a StringIO subclass to capture the output.
      >
      >Is there any way to make these work together?
      >
      Using the StreamHandler with something like
      >
      class WrapStdOut(obje ct):
      def __getattr__(sel f, name):
      return getattr(sys.std out, name)
      >
      instead of sys.stdout directly should work.
      Or create the StreamHandler inside your doctest where sys.stdout is already
      redirected.

      Peter

      Comment

      • Gary Jefferson

        #4
        Re: logging module and doctest

        Peter Otten wrote:
        Peter Otten wrote:
        >
        Gary Jefferson wrote:
        I've written a logging.filter and would like to use doctest on it
        (using a StreamHandler for stdout), but this doesn't seem possible.
        Output from the logger seems to disappear (running the doctest strings
        through the interpreter as-is yields expected results). I assume this
        is because doctest does something with logging.
        It redirects stdout to a StringIO subclass to capture the output.
        Is there any way to make these work together?
        Using the StreamHandler with something like

        class WrapStdOut(obje ct):
        def __getattr__(sel f, name):
        return getattr(sys.std out, name)

        instead of sys.stdout directly should work.
        >
        Or create the StreamHandler inside your doctest where sys.stdout is already
        redirected.
        >
        Peter
        I was creating the StreamHandler inside doctest. Turns out, however,
        that I hadn't imported 'sys', and so it didn't work. Its a little
        strange that this didn't result in an error, but there you have it.

        Thanks for the tip that led me to discover this.

        Gary

        Comment

        Working...