def test_base_udp_decoding(self): """Assert json decoding of UDP message.""" test_str = u'test-runner: simple message' for version in list(self.logstash_versions.keys()): test_logger = logging.getLogger('python-logstash-logger') test_logger.setLevel(logging.INFO) for handler in test_logger.handlers: test_logger.removeHandler(handler) test_logger.addHandler(logstash.UDPLogstashHandler(self.host, self.port, version=version)) test_logger.error(test_str) recv_str = self.server.RequestHandlerClass.recv_queue.get() try: recv_dict = json.loads(recv_str) except ValueError as e: self.fail("String '%s' could not be parsed as json." %recv_str) err_msg=("Parsed json: '%s' did not have matching message '%s'." % (recv_str, test_str)) version_keys = self.logstash_versions[version] try: print(list(recv_dict.keys())) recv_dict[version_keys['msg_str']] except KeyError: self.fail("Could not find '%s' in '%s'." % (version_keys['msg_str'], recv_str)) #TODO: determine if UDP just never calls the other version of LogstashFormatterVersion! self.assertEqual(recv_dict[version_keys['msg_str']], test_str, msg=err_msg)
def main(): """ Execution entry point """ try: environment = "test" logdir = "/var/tmp/program/" logfile = "%s.log" % (environment) # First, get a logger object for logging - without that the script can't execute cause we need to keep activity log. # (OPTIONAL) We also pass a logstash handler to ship logs to a logstash instance on UDP_IP:UDP_PORT - otherwise don't pass a handler. logger, file_handler = create_logger( environment, logfile, logdir, logstash.UDPLogstashHandler(UDP_IP, UDP_PORT, message_type="test-ops", version=1)) # At this point the logger has been created successfully so we can continue except OSError as e: print("\n\n[ERROR] An error occurred. Error:\n\t (%s)\n" % (str(sys.exc_info()[1]))) sys.exit(1) except Exception as e: print("\n\n[ERROR] An error occurred. Error:\n\t (%s)\n" % (str(sys.exc_info()[1]))) sys.exit(1)
def __init__(self, endpoint): self.name = endpoint['name'] self.host_url = endpoint['host_url'] self.host_port = endpoint['host_port'] self.version = endpoint['version'] self.logger = logging.getLogger('python-logstash-logger') self.logger.setLevel(logging.INFO) if endpoint['protocol'] == 'TCP': self.logger.addHandler( logstash.TCPLogstashHandler(self.host_url, self.host_port, version=self.version)) else: self.logger.addHandler( logstash.UDPLogstashHandler(self.host_url, self.host_port, version=self.version))
def _setup_handler(self): logstash_handler = None if self.handler == 'AMQP': logstash_handler = logstash.AMQPLogstashHandler( version=1, host=self.host, durable=self.durable, username=self.username, password=self.password, exchange=self.exchange) elif self.handler == 'TCP': logstash_handler = logstash.TCPLogstashHandler(self.host, self.port, version=1) elif self.handler == "UDP": logstash_handler = logstash.UDPLogstashHandler(self.host, self.port, version=1) self.attack_logger = logging.getLogger('python-logstash-handler') self.attack_logger.setLevel(logging.INFO) self.attack_logger.addHandler(logstash_handler)
def _initialise() -> logging.Logger: """JIT initialiser. """ global _logger global _writers # Set logger. _logger = logging.getLogger('STESTS') _logger.setLevel(logging.INFO) _logger.addHandler( logstash.UDPLogstashHandler(EnvVars.HOST, EnvVars.PORT, version=EnvVars.VERSION)) # Set writer map. _writers = { Level.DEBUG.name: _logger.debug, Level.INFO.name: _logger.info, Level.WARN.name: _logger.warning, Level.ERROR.name: _logger.error, Level.CRITICAL.name: _logger.critical, Level.FATAL.name: _logger.critical, }
""" Helper function and check of the arguments supplied """ print("#################################################################") print("# #") print("# iQAS: an integration platform for QoO Assessment as a Service #") print("# Module: Virtual App Consumer #") print("# (C) 2017 Antoine Auger #") print("# #") print( "#################################################################\n") if __name__ == "__main__": host = '10.161.3.181' test_logger = logging.getLogger('python-logstash-logger') test_logger.setLevel(logging.INFO) test_logger.addHandler(logstash.UDPLogstashHandler(host, 54321, version=1)) # Loading the configuration with open('../etc/app.config') as config_file: config = json.load(config_file) if len(sys.argv) < 3 or len(sys.argv) > 4: usage() print('ERROR: Wrong number of parameters') exit() else: topics_to_subscribe = None if len(sys.argv) == 4: # The Request is given as a single string config['application_id'] = str(sys.argv[2]) config['use_iqas'] = True
import logging import logstash l = logging.getLogger() h = logstash.UDPLogstashHandler(host='logstash', port=5044, tags={'type': 'log'}) l.addHandler(h) l.setLevel(logging.INFO) l.info('Hello World!')
import logging import logstash import sys host = '127.0.0.1' test_logger = logging.getLogger('python-logstash-logger') test_logger.setLevel(logging.INFO) test_logger.addHandler(logstash.UDPLogstashHandler(host, 24224, version=1)) test_logger.error('python-logstash: test logstash error message.') test_logger.info('python-logstash: test logstash info message.') test_logger.warning('python-logstash: test logstash warning message.') # add extra field to logstash message extra = { 'test_string': 'python version: ' + repr(sys.version_info), # 'test_boolean': True, 'test_dict': {'a': 1, 'b': 'c'}, 'test_float': 1.23, # 'test_integer': 123, #'test_list': [1, 2, '3'], } test_logger.info('python-logstash: test extra fields', extra=extra)