def compress(data, compresslevel=9): ''' Returns the data compressed at gzip level compression. ''' buf = StringIO() with open_fileobj(buf, 'wb', compresslevel) as ogz: ogz.write(data) compressed = buf.getvalue() return compressed
def compress(data, compresslevel=9): ''' Returns the data compressed at gzip level compression. ''' buf = StringIO() with open_fileobj(buf, 'wb', compresslevel) as ogz: ogz.write(data) compressed = buf.getvalue() return compressed
def test_exc_info_on_loglevel(self): def raise_exception_on_purpose(): 1 / 0 # pylint: disable=pointless-statement log = saltlog.SaltLoggingClass(__name__) # Only stream2 should contain the traceback stream1 = StringIO() stream2 = StringIO() handler1 = StreamHandler(stream1) handler2 = StreamHandler(stream2) handler1.setLevel(logging.INFO) handler2.setLevel(logging.DEBUG) log.addHandler(handler1) log.addHandler(handler2) try: raise_exception_on_purpose() except ZeroDivisionError as exc: log.error('Exception raised on purpose caught: ZeroDivisionError', exc_info_on_loglevel=logging.DEBUG) try: self.assertIn( 'Exception raised on purpose caught: ZeroDivisionError', stream1.getvalue()) self.assertNotIn('Traceback (most recent call last)', stream1.getvalue()) self.assertIn( 'Exception raised on purpose caught: ZeroDivisionError', stream2.getvalue()) self.assertIn('Traceback (most recent call last)', stream2.getvalue()) finally: log.removeHandler(handler1) log.removeHandler(handler2) # Both streams should contain the traceback stream1 = StringIO() stream2 = StringIO() handler1 = StreamHandler(stream1) handler2 = StreamHandler(stream2) handler1.setLevel(logging.INFO) handler2.setLevel(logging.DEBUG) log.addHandler(handler1) log.addHandler(handler2) try: raise_exception_on_purpose() except ZeroDivisionError as exc: log.error('Exception raised on purpose caught: ZeroDivisionError', exc_info_on_loglevel=logging.INFO) try: self.assertIn( 'Exception raised on purpose caught: ZeroDivisionError', stream1.getvalue()) self.assertIn('Traceback (most recent call last)', stream1.getvalue()) self.assertIn( 'Exception raised on purpose caught: ZeroDivisionError', stream2.getvalue()) self.assertIn('Traceback (most recent call last)', stream2.getvalue()) finally: log.removeHandler(handler1) log.removeHandler(handler2) # No streams should contain the traceback stream1 = StringIO() stream2 = StringIO() handler1 = StreamHandler(stream1) handler2 = StreamHandler(stream2) handler1.setLevel(logging.ERROR) handler2.setLevel(logging.INFO) log.addHandler(handler1) log.addHandler(handler2) try: raise_exception_on_purpose() except ZeroDivisionError as exc: log.error('Exception raised on purpose caught: ZeroDivisionError', exc_info_on_loglevel=logging.DEBUG) try: self.assertIn( 'Exception raised on purpose caught: ZeroDivisionError', stream1.getvalue()) self.assertNotIn('Traceback (most recent call last)', stream1.getvalue()) self.assertIn( 'Exception raised on purpose caught: ZeroDivisionError', stream2.getvalue()) self.assertNotIn('Traceback (most recent call last)', stream2.getvalue()) finally: log.removeHandler(handler1) log.removeHandler(handler2)
def test_exc_info_on_loglevel(self): def raise_exception_on_purpose(): 1/0 # pylint: disable=pointless-statement log = saltlog.SaltLoggingClass(__name__) # Only stream2 should contain the traceback stream1 = StringIO() stream2 = StringIO() handler1 = StreamHandler(stream1) handler2 = StreamHandler(stream2) handler1.setLevel(logging.INFO) handler2.setLevel(logging.DEBUG) log.addHandler(handler1) log.addHandler(handler2) try: raise_exception_on_purpose() except ZeroDivisionError as exc: log.error('Exception raised on purpose caught: ZeroDivisionError', exc_info_on_loglevel=logging.DEBUG) try: self.assertIn( 'Exception raised on purpose caught: ZeroDivisionError', stream1.getvalue() ) self.assertNotIn('Traceback (most recent call last)', stream1.getvalue()) self.assertIn( 'Exception raised on purpose caught: ZeroDivisionError', stream2.getvalue() ) self.assertIn('Traceback (most recent call last)', stream2.getvalue()) finally: log.removeHandler(handler1) log.removeHandler(handler2) # Both streams should contain the traceback stream1 = StringIO() stream2 = StringIO() handler1 = StreamHandler(stream1) handler2 = StreamHandler(stream2) handler1.setLevel(logging.INFO) handler2.setLevel(logging.DEBUG) log.addHandler(handler1) log.addHandler(handler2) try: raise_exception_on_purpose() except ZeroDivisionError as exc: log.error('Exception raised on purpose caught: ZeroDivisionError', exc_info_on_loglevel=logging.INFO) try: self.assertIn( 'Exception raised on purpose caught: ZeroDivisionError', stream1.getvalue() ) self.assertIn('Traceback (most recent call last)', stream1.getvalue()) self.assertIn( 'Exception raised on purpose caught: ZeroDivisionError', stream2.getvalue() ) self.assertIn('Traceback (most recent call last)', stream2.getvalue()) finally: log.removeHandler(handler1) log.removeHandler(handler2) # No streams should contain the traceback stream1 = StringIO() stream2 = StringIO() handler1 = StreamHandler(stream1) handler2 = StreamHandler(stream2) handler1.setLevel(logging.ERROR) handler2.setLevel(logging.INFO) log.addHandler(handler1) log.addHandler(handler2) try: raise_exception_on_purpose() except ZeroDivisionError as exc: log.error('Exception raised on purpose caught: ZeroDivisionError', exc_info_on_loglevel=logging.DEBUG) try: self.assertIn( 'Exception raised on purpose caught: ZeroDivisionError', stream1.getvalue() ) self.assertNotIn('Traceback (most recent call last)', stream1.getvalue()) self.assertIn( 'Exception raised on purpose caught: ZeroDivisionError', stream2.getvalue() ) self.assertNotIn('Traceback (most recent call last)', stream2.getvalue()) finally: log.removeHandler(handler1) log.removeHandler(handler2)