def get_handler(url, log_format, logging_level, debug_mode): token = secrets.get('LOGZIO_TOKEN') handler = LogzioHandler(token=token, url=url, debug=debug_mode) handler.setLevel(logging.getLevelName(logging_level)) formatter = logging.Formatter(log_format) handler.setFormatter(formatter) return handler
def add_logzio_handler(self, **kwargs): if LOGZIO_INSTALLED: if not self._env('HANDLERS_LOGZIO_TOKEN'): self.logger.warn('Logzio handler token not set') name = self._env('HANDLERS_LOGZIO_NAME', default='logzio') level = self._env('HANDLERS_LOGZIO_LEVEL', default='info') formatter = self._env('HANDLERS_LOGZIO_FORMATTER', default='json') handler = LogzioHandler(self._env('HANDLERS_LOGZIO_TOKEN')) self.add_handler(handler=handler, name=name, formatter=formatter, level=level) else: self.logger.error( 'It seems that the logzio handler is not installed. ' 'You can install it by running `pip install ' 'wryte[logzio]`')
def setUp(self): self.handler = LogzioHandler('moo')
class TestLogzioHandler(TestCase): def setUp(self): self.handler = LogzioHandler('moo') def test_json(self): formatter = logging.Formatter( '{ "appname":"%(name)s", "functionName":"%(funcName)s", \"lineNo":"%(lineno)d", "severity":"%(levelname)s", "message":"%(message)s"}' ) self.handler.setFormatter(formatter) record = logging.LogRecord(name='my-logger', level=0, pathname='handler_test.py', lineno=10, msg="this is a test: moo.", args=[], exc_info=None, func='test_json') formatted_message = self.handler.format_message(record) formatted_message["@timestamp"] = None self.assertDictEqual( formatted_message, { '@timestamp': None, 'appname': 'my-logger', 'functionName': 'test_json', 'lineNo': '10', 'line_number': 10, 'log_level': 'NOTSET', 'logger': 'my-logger', 'message': 'this is a test: moo.', 'path_name': 'handler_test.py', 'severity': 'NOTSET', 'type': 'python' }) def test_string(self): record = logging.LogRecord(name='my-logger', level=0, pathname='handler_test.py', lineno=10, msg="this is a test: moo.", args=[], exc_info=None, func='test_json') formatted_message = self.handler.format_message(record) formatted_message["@timestamp"] = None self.assertDictEqual( formatted_message, { '@timestamp': None, 'line_number': 10, 'log_level': 'NOTSET', 'logger': 'my-logger', 'message': 'this is a test: moo.', 'path_name': 'handler_test.py', 'type': 'python' }) def test_exc(self): try: raise ValueError("oops.") except: exc_info = sys.exc_info() record = logging.LogRecord(name='my-logger', level=0, pathname='handler_test.py', lineno=10, msg="this is a test: moo.", args=[], exc_info=exc_info, func='test_json') formatted_message = self.handler.format_message(record) formatted_message["@timestamp"] = None formatted_message["exception"] = formatted_message[ "exception"].replace(os.path.abspath(__file__), "") formatted_message["exception"] = re.sub(r", line \d+", "", formatted_message["exception"]) self.assertDictEqual( { '@timestamp': None, 'line_number': 10, 'log_level': 'NOTSET', 'logger': 'my-logger', 'message': 'this is a test: moo.', 'exception': 'Traceback (most recent call last):\n\n File "", in test_exc\n raise ValueError("oops.")\n\nValueError: oops.\n', 'path_name': 'handler_test.py', 'type': 'python' }, formatted_message)
def setUp(self): self.handler = LogzioHandler('moo') self.handler.drain_messages = dummy_drain_messages
class TestLogzioHandler(TestCase): def setUp(self): self.handler = LogzioHandler('moo') def test_json(self): formatter = logging.Formatter( '{ "appname":"%(name)s", "functionName":"%(funcName)s", \"lineNo":"%(lineno)d", "severity":"%(levelname)s", "message":"%(message)s"}') self.handler.setFormatter(formatter) record = logging.LogRecord( name='my-logger', level=0, pathname='handler_test.py', lineno=10, msg="this is a test: moo.", args=[], exc_info=None, func='test_json' ) formatted_message = self.handler.format_message(record) formatted_message["@timestamp"] = None self.assertDictEqual( formatted_message, { '@timestamp': None, 'appname': 'my-logger', 'functionName': 'test_json', 'lineNo': '10', 'line_number': 10, 'log_level': 'NOTSET', 'logger': 'my-logger', 'message': 'this is a test: moo.', 'path_name': 'handler_test.py', 'severity': 'NOTSET', 'type': 'python' } ) def test_string(self): record = logging.LogRecord( name='my-logger', level=0, pathname='handler_test.py', lineno=10, msg="this is a test: moo.", args=[], exc_info=None, func='test_json' ) formatted_message = self.handler.format_message(record) formatted_message["@timestamp"] = None self.assertDictEqual( formatted_message, { '@timestamp': None, 'line_number': 10, 'log_level': 'NOTSET', 'logger': 'my-logger', 'message': 'this is a test: moo.', 'path_name': 'handler_test.py', 'type': 'python' } ) def test_extra_formatting(self): record = logging.LogRecord( name='my-logger', level=0, pathname='handler_test.py', lineno=10, msg="this is a test: moo.", args=[], exc_info=None, func='test_json' ) record.__dict__["extra_key"] = "extra_value" record.__dict__["module"] = "testing" formatted_message = self.handler.format_message(record) formatted_message["@timestamp"] = None self.assertDictEqual( formatted_message, { '@timestamp': None, 'line_number': 10, 'log_level': 'NOTSET', 'logger': 'my-logger', 'message': 'this is a test: moo.', 'path_name': 'handler_test.py', 'type': 'python', 'extra_key': 'extra_value' } ) def test_format_string_message(self): record = logging.LogRecord( name='my-logger', level=0, pathname='handler_test.py', lineno=10, msg="this is a test: %s.", args=('moo',), exc_info=None, func='test_json' ) formatted_message = self.handler.format_message(record) formatted_message["@timestamp"] = None self.assertDictEqual( formatted_message, { '@timestamp': None, 'line_number': 10, 'log_level': 'NOTSET', 'logger': 'my-logger', 'message': 'this is a test: moo.', 'path_name': 'handler_test.py', 'type': 'python' } ) def test_exc(self): try: raise ValueError("oops.") except: exc_info = sys.exc_info() record = logging.LogRecord( name='my-logger', level=0, pathname='handler_test.py', lineno=10, msg="this is a test: moo.", args=[], exc_info=exc_info, func='test_json' ) formatted_message = self.handler.format_message(record) formatted_message["@timestamp"] = None formatted_message["exception"] = formatted_message["exception"].replace(os.path.abspath(__file__), "") formatted_message["exception"] = re.sub(r", line \d+", "", formatted_message["exception"]) self.assertDictEqual( { '@timestamp': None, 'line_number': 10, 'log_level': 'NOTSET', 'logger': 'my-logger', 'message': 'this is a test: moo.', 'exception': 'Traceback (most recent call last):\n\n File "", in test_exc\n raise ValueError("oops.")\n\nValueError: oops.\n', 'path_name': 'handler_test.py', 'type': 'python' }, formatted_message )