
from twisted.trial import unittest

import logging


# logging doesn't have a "don't log anything at all EVARRRR setting,
# but since the highest value is 50, 1000000 should do ;)
NEVER = 1000000

logging.getLogger().addHandler(logging.StreamHandler())
logging.getLogger().setLevel(NEVER)


class TestCase(unittest.TestCase):
    pass


from twisted.trial import unittest

import logging


# logging doesn't have a "don't log anything at all EVARRRR setting,
# but since the highest value is 50, 1000000 should do ;)
NEVER = 1000000

logging.getLogger().addHandler(logging.StreamHandler())
logging.getLogger().setLevel(NEVER)


class TestCase(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(TestCase, self).__init__(*args, **kwargs)

        level = getattr(self, "loglevel", NEVER)

        orig_setUp = self.setUp

        def setUp():
            old_level = logging.getLogger().level

            if old_level != level:
                orig_tearDown = self.tearDown

                def tearDown():
                    ret = orig_tearDown()
                    logging.getLogger().setLevel(old_level)
                    return ret
                self.tearDown = tearDown

            logging.getLogger().setLevel(level)
            return orig_setUp()
        self.setUp = setUp

Allow a TestCase to set a 'loglevel' attribute, which overrides the logging level while that testcase runs
Allow a TestCase to set a 'loglevel' attribute, which overrides the logging level while that testcase runs

  
  from twisted.trial import unittest
  
  import logging
  
  
  # logging doesn't have a "don't log anything at all EVARRRR setting,
  # but since the highest value is 50, 1000000 should do ;)
  NEVER = 1000000
  
  logging.getLogger().addHandler(logging.StreamHandler())
  logging.getLogger().setLevel(NEVER)
  
  
  class TestCase(unittest.TestCase):
-     pass
+     def __init__(self, *args, **kwargs):
+         super(TestCase, self).__init__(*args, **kwargs)
  
+         level = getattr(self, "loglevel", NEVER)
+ 
+         orig_setUp = self.setUp
+ 
+         def setUp():
+             old_level = logging.getLogger().level
+ 
+             if old_level != level:
+                 orig_tearDown = self.tearDown
+ 
+                 def tearDown():
+                     ret = orig_tearDown()
+                     logging.getLogger().setLevel(old_level)
+                     return ret
+                 self.tearDown = tearDown
+ 
+             logging.getLogger().setLevel(level)
+             return orig_setUp()
+         self.setUp = setUp
+ 
Allow a TestCase to set a 'loglevel' attribute, which overrides the logging level while that testcase runs
## Code Before:

from twisted.trial import unittest

import logging


# logging doesn't have a "don't log anything at all EVARRRR setting,
# but since the highest value is 50, 1000000 should do ;)
NEVER = 1000000

logging.getLogger().addHandler(logging.StreamHandler())
logging.getLogger().setLevel(NEVER)


class TestCase(unittest.TestCase):
    pass

## Instruction:
Allow a TestCase to set a 'loglevel' attribute, which overrides the logging level while that testcase runs
## Code After:

from twisted.trial import unittest

import logging


# logging doesn't have a "don't log anything at all EVARRRR setting,
# but since the highest value is 50, 1000000 should do ;)
NEVER = 1000000

logging.getLogger().addHandler(logging.StreamHandler())
logging.getLogger().setLevel(NEVER)


class TestCase(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(TestCase, self).__init__(*args, **kwargs)

        level = getattr(self, "loglevel", NEVER)

        orig_setUp = self.setUp

        def setUp():
            old_level = logging.getLogger().level

            if old_level != level:
                orig_tearDown = self.tearDown

                def tearDown():
                    ret = orig_tearDown()
                    logging.getLogger().setLevel(old_level)
                    return ret
                self.tearDown = tearDown

            logging.getLogger().setLevel(level)
            return orig_setUp()
        self.setUp = setUp
