Esempio n. 1
0
    def test_formatEventWeirdFormat(self):
        """
        Formatting an event with a bogus format.
        """
        event = dict(log_format=object(), foo=1, bar=2)
        result = formatEvent(event)

        self.assertIn("Log format must be unicode or bytes", result)
        self.assertIn(repr(event), result)
Esempio n. 2
0
    def test_formatUnformattableEvent(self):
        """
        Formatting an event that's just plain out to get us.
        """
        event = dict(log_format="{evil()}", evil=lambda: 1/0)
        result = formatEvent(event)

        self.assertIn("Unable to format event", result)
        self.assertIn(repr(event), result)
Esempio n. 3
0
    def test_formatEventNoFormat(self):
        """
        Formatting an event with no format.
        """
        event = dict(foo=1, bar=2)
        result = formatEvent(event)

        self.assertIn("Unable to format event", result)
        self.assertIn(repr(event), result)
Esempio n. 4
0
    def test_formatEventNoFormat(self):
        """
        Formatting an event with no format.
        """
        event = dict(foo=1, bar=2)
        result = formatEvent(event)

        self.assertIn("Unable to format event", result)
        self.assertIn(repr(event), result)
Esempio n. 5
0
    def test_formatUnformattableEvent(self):
        """
        Formatting an event that's just plain out to get us.
        """
        event = dict(log_format="{evil()}", evil=lambda: 1 / 0)
        result = formatEvent(event)

        self.assertIn("Unable to format event", result)
        self.assertIn(repr(event), result)
Esempio n. 6
0
    def test_formatEventWeirdFormat(self):
        """
        Formatting an event with a bogus format.
        """
        event = dict(log_format=object(), foo=1, bar=2)
        result = formatEvent(event)

        self.assertIn("Log format must be unicode or bytes", result)
        self.assertIn(repr(event), result)
Esempio n. 7
0
 def test_formatUnformattableEventWithUnformattableValue(self):
     """
     Formatting an unformattable event that has an unformattable value.
     """
     event = dict(
         log_format="{evil()}",
         evil=lambda: 1/0,
         gurk=Unformattable(),
     )
     result = formatEvent(event)
     self.assertIn("MESSAGE LOST: unformattable object logged:", result)
     self.assertIn("Recoverable data:", result)
     self.assertIn("Exception during formatting:", result)
Esempio n. 8
0
 def test_formatUnformattableEventWithUnformattableKey(self):
     """
     Formatting an unformattable event that has an unformattable key.
     """
     event = {
         "log_format": "{evil()}",
         "evil": lambda: 1/0,
         Unformattable(): "gurk",
     }
     result = formatEvent(event)
     self.assertIn("MESSAGE LOST: unformattable object logged:", result)
     self.assertIn("Recoverable data:", result)
     self.assertIn("Exception during formatting:", result)
Esempio n. 9
0
 def test_formatUnformattableEventWithUnformattableValue(self):
     """
     Formatting an unformattable event that has an unformattable value.
     """
     event = dict(
         log_format="{evil()}",
         evil=lambda: 1 / 0,
         gurk=Unformattable(),
     )
     result = formatEvent(event)
     self.assertIn("MESSAGE LOST: unformattable object logged:", result)
     self.assertIn("Recoverable data:", result)
     self.assertIn("Exception during formatting:", result)
Esempio n. 10
0
 def test_formatUnformattableEventWithUnformattableKey(self):
     """
     Formatting an unformattable event that has an unformattable key.
     """
     event = {
         "log_format": "{evil()}",
         "evil": lambda: 1 / 0,
         Unformattable(): "gurk",
     }
     result = formatEvent(event)
     self.assertIn("MESSAGE LOST: unformattable object logged:", result)
     self.assertIn("Recoverable data:", result)
     self.assertIn("Exception during formatting:", result)
Esempio n. 11
0
    def test_sourceAvailableForFormatting(self):
        """
        On instances that have a L{Logger} class attribute, the C{log_source}
        key is available to format strings.
        """
        obj = LogComposedObject("hello")
        log = obj.log
        log.error("Hello, {log_source}.")

        self.assertIn("log_source", log.event)
        self.assertEquals(log.event["log_source"], obj)

        stuff = formatEvent(log.event)
        self.assertIn("Hello, <LogComposedObject hello>.", stuff)
Esempio n. 12
0
    def test_sourceAvailableForFormatting(self):
        """
        On instances that have a L{Logger} class attribute, the C{log_source}
        key is available to format strings.
        """
        obj = LogComposedObject("hello")
        log = obj.log
        log.error("Hello, {log_source}.")

        self.assertIn("log_source", log.event)
        self.assertEquals(log.event["log_source"], obj)

        stuff = formatEvent(log.event)
        self.assertIn("Hello, <LogComposedObject hello>.", stuff)
Esempio n. 13
0
    def test_basic_Logger(self):
        """
        Test that log levels and messages are emitted correctly for
        Logger.
        """
        # FIXME: Need a basic test like this for logger attached to a class.
        # At least: source should not be None in that case.

        log = TestLogger()

        for level in LogLevel.iterconstants():
            format = "This is a {level_name} message"
            message = format.format(level_name=level.name)

            method = getattr(log, level.name)
            method(format, junk=message, level_name=level.name)

            # Ensure that test_emit got called with expected arguments
            self.assertEquals(log.emitted["level"], level)
            self.assertEquals(log.emitted["format"], format)
            self.assertEquals(log.emitted["kwargs"]["junk"], message)

            if level >= logLevelForNamespace(log.namespace):
                self.assertTrue(hasattr(log, "event"), "No event observed.")
                self.assertEquals(log.event["log_format"], format)
                self.assertEquals(log.event["log_level"], level)
                self.assertEquals(log.event["log_namespace"], __name__)
                self.assertEquals(log.event["log_source"], None)

                self.assertEquals(log.event["logLevel"],
                                  pythonLogLevelMapping[level])

                self.assertEquals(log.event["junk"], message)

                # FIXME: this checks the end of message because we do
                # formatting in emit()
                self.assertEquals(
                    formatEvent(log.event),
                    message
                )
            else:
                self.assertFalse(hasattr(log, "event"))
Esempio n. 14
0
    def test_basic_Logger(self):
        """
        Test that log levels and messages are emitted correctly for
        Logger.
        """
        # FIXME: Need a basic test like this for logger attached to a class.
        # At least: source should not be None in that case.

        log = TestLogger()

        for level in LogLevel.iterconstants():
            format = "This is a {level_name} message"
            message = format.format(level_name=level.name)

            method = getattr(log, level.name)
            method(format, junk=message, level_name=level.name)

            # Ensure that test_emit got called with expected arguments
            self.assertEquals(log.emitted["level"], level)
            self.assertEquals(log.emitted["format"], format)
            self.assertEquals(log.emitted["kwargs"]["junk"], message)

            if level >= logLevelForNamespace(log.namespace):
                self.assertTrue(hasattr(log, "event"), "No event observed.")
                self.assertEquals(log.event["log_format"], format)
                self.assertEquals(log.event["log_level"], level)
                self.assertEquals(log.event["log_namespace"], __name__)
                self.assertEquals(log.event["log_source"], None)

                self.assertEquals(log.event["logLevel"],
                                  pythonLogLevelMapping[level])

                self.assertEquals(log.event["junk"], message)

                # FIXME: this checks the end of message because we do
                # formatting in emit()
                self.assertEquals(formatEvent(log.event), message)
            else:
                self.assertFalse(hasattr(log, "event"))
Esempio n. 15
0
 def onlyUpgradeEvents(eventDict):
     text = formatEvent(eventDict)
     output.write(logDateString() + " " + text + "\n")
     output.flush()
Esempio n. 16
0
 def format(log_format, **event):
     event["log_format"] = log_format
     result = formatEvent(event)
     self.assertIdentical(type(result), unicode)
     return result
Esempio n. 17
0
 def onlyUpgradeEvents(eventDict):
     text = formatEvent(eventDict)
     output.write(logDateString() + " " + text + "\n")
     output.flush()
Esempio n. 18
0
 def format(log_format, **event):
     event["log_format"] = log_format
     result = formatEvent(event)
     self.assertIdentical(type(result), unicode)
     return result