Example #1
0
    def test_trap_output(self):
        class BorkedOutput(outputs.ListOutput):
            def _write(self, x):
                raise RuntimeError("BORK")

        out = BorkedOutput(close_atexit=False)

        sio = StringIO()

        def cleanup(stderr, output):
            sys.stderr = stderr
            sio.close()
            self.log.output = output
            out.close()

        self.addCleanup(cleanup, sys.stderr, self.log.output)
        sys.stderr = sio
        self.log.output = out

        self.log.fields().info("hi")

        assert "BORK" in sio.getvalue()
        assert "Offending message: <twiggy.message.Message object" in sio.getvalue()
        assert "Error in twiggy internal log! Something is serioulsy broken." in sio.getvalue()
        assert "Traceback" in sio.getvalue()
Example #2
0
 def test_stream_output(self):
     sio = StringIO()
     o = outputs.StreamOutput(formats.shell_format, sio)
     o.output(m)
     o.close()
     assert sio.getvalue(
     ) == "DEBUG:jose:shirt=42|Hello Mister Funnypants\n"
Example #3
0
    def test_integration(self):
        everything = twiggy.outputs.StreamOutput(
            stream=StringIO(), format=twiggy.formats.line_format)
        out1 = twiggy.outputs.StreamOutput(stream=StringIO(),
                                           format=twiggy.formats.line_format)
        out2 = twiggy.outputs.StreamOutput(stream=StringIO(),
                                           format=twiggy.formats.line_format)

        twiggy.add_emitters(
            ('*', twiggy.levels.DEBUG, None, everything),
            ('first', twiggy.levels.INFO, None, out1),
            ('second', twiggy.levels.DEBUG,
             twiggy.filters.glob_names('second.*'), out2),
            ('first-filter', twiggy.levels.DEBUG, ".*pants.*", out1))

        def something():
            return "something cool"

        twiggy.log.debug("oh hi")
        twiggy.log.name("second").info("do you like cheese?")
        twiggy.log.name("second.child").fields(cheese="hate").warning("No")
        twiggy.log.name("first").error("Can you do {0}", something)
        twiggy.log.name("bob").debug("I wear pants")

        try:
            raise RuntimeError("Oh Noes!")
        except:
            twiggy.log.trace().critical("Went boom")

        print("***************** everything **********************")
        print(everything.stream.getvalue(), end=' ')
        print("****************** out 1 **************************")
        print(out1.stream.getvalue(), end=' ')
        print("****************** out 2 **************************")
        print(out2.stream.getvalue(), end=' ')
        print("***************************************************")

        # XXX this should really be done with a regex, but I'm feeling lazy
        assert out1.stream.getvalue().startswith( \
"""2010-10-28T02:15:57Z:INFO:second|do you like cheese?
2010-10-28T02:15:57Z:WARNING:second.child:cheese=hate|No
2010-10-28T02:15:57Z:ERROR:first|Can you do something cool
2010-10-28T02:15:57Z:DEBUG:bob|I wear pants
2010-10-28T02:15:57Z:CRITICAL|Went boom
TRACE Traceback (most recent call last):
""")
        #"""TRACE   File "/home/pfein/Projects/python-twiggy/tests/test_integration.py", line 39, in test_integration
        assert out1.stream.getvalue().endswith( \
"""TRACE     raise RuntimeError("Oh Noes!")
TRACE RuntimeError: Oh Noes!
""")


        assert out2.stream.getvalue() == \
"""2010-10-28T02:15:57Z:WARNING:second.child:cheese=hate|No
Example #4
0
    def test_trap_msg(self):
        sio = StringIO()

        def cleanup(stderr):
            sys.stderr = stderr
            sio.close()

        self.addCleanup(cleanup, sys.stderr)
        sys.stderr = sio

        def go_boom():
            raise RuntimeError("BOOM")

        self.log.fields(func=go_boom).info("hi")

        assert "BOOM" in sio.getvalue()
        assert "Offending message: None" in sio.getvalue()
        assert "Error in twiggy internal log! Something is serioulsy broken." in sio.getvalue()
        assert "Traceback" in sio.getvalue()
Example #5
0
    def test_trap_output(self):
        class BorkedOutput(outputs.ListOutput):
            def _write(self, x):
                raise RuntimeError("BORK")

        out = BorkedOutput(close_atexit=False)

        sio = StringIO()

        def cleanup(stderr, output):
            sys.stderr = stderr
            sio.close()
            self.log.output = output
            out.close()

        self.addCleanup(cleanup, sys.stderr, self.log.output)
        sys.stderr = sio
        self.log.output = out

        self.log.fields().info('hi')

        assert "BORK" in sio.getvalue()
        assert "Offending message: <twiggy.message.Message object" in sio.getvalue(
        )
        assert "Error in twiggy internal log! Something is serioulsy broken." in sio.getvalue(
        )
        assert "Traceback" in sio.getvalue()
Example #6
0
    def test_trap_msg(self):
        sio = StringIO()

        def cleanup(stderr):
            sys.stderr = stderr
            sio.close()

        self.addCleanup(cleanup, sys.stderr)
        sys.stderr = sio

        def go_boom():
            raise RuntimeError("BOOM")

        self.log.fields(func=go_boom).info('hi')

        assert "BOOM" in sio.getvalue()
        assert "Offending message: None" in sio.getvalue()
        assert "Error in twiggy internal log! Something is serioulsy broken." in sio.getvalue(
        )
        assert "Traceback" in sio.getvalue()
 def test_stream_output(self):
     sio = StringIO()
     o = outputs.StreamOutput(formats.shell_format, sio)
     o.output(m)
     o.close()
     assert sio.getvalue() == "DEBUG:jose:shirt=42|Hello Mister Funnypants\n"