def run(self): client = Client.from_config(self.config) instances = {} for address in self.args['<address>']: service = client.container.lookup(address) for instance in service: if instance.log_endpoint: instances[instance.log_endpoint] = instance, address ctx = zmq.Context.instance() sock = ctx.socket(zmq.SUB) sock.setsockopt_string(zmq.SUBSCRIBE, u'') for instance, service_type in six.itervalues(instances): sock.connect(instance.log_endpoint) level = get_loglevel(self.args['--level']) logger = logging.getLogger('iris-tail-cli') logger.setLevel(level) console = logging.StreamHandler() console.setLevel(level) console.setFormatter(logging.Formatter('[%(service_type)s][%(identity)s] [%(levelname)s] %(message)s')) logger.addHandler(console) try: while True: topic, endpoint, msg = sock.recv_multipart() level = getattr(logging, topic) instance, service_type = instances[endpoint] logger.log(level, msg, extra={ 'identity': instance.identity[:10], 'service_type': service_type, }) except KeyboardInterrupt: pass
def test_get_loglevel(self): self.assertEqual(get_loglevel('DEBUG'), logging.DEBUG) self.assertEqual(get_loglevel('debug'), logging.DEBUG) self.assertEqual(get_loglevel('Debug'), logging.DEBUG) self.assertEqual(get_loglevel('INFO'), logging.INFO) self.assertEqual(get_loglevel('info'), logging.INFO) self.assertEqual(get_loglevel('ERROR'), logging.ERROR) self.assertEqual(get_loglevel('error'), logging.ERROR) self.assertEqual(get_loglevel('CRITICAL'), logging.CRITICAL) self.assertEqual(get_loglevel('critical'), logging.CRITICAL) self.assertRaises(ValueError, get_loglevel, 'FOO') self.assertRaises(ValueError, get_loglevel, '*')