def test_init_iface(self): logger = self.logger ctx = self.context handler = handlers.PUBHandler(self.iface) self.assertFalse(handler.ctx is ctx) self.sockets.append(handler.socket) # handler.ctx.term() handler = handlers.PUBHandler(self.iface, self.context) self.sockets.append(handler.socket) self.assertTrue(handler.ctx is ctx) handler.setLevel(logging.DEBUG) handler.root_topic = self.topic logger.addHandler(handler) sub = ctx.socket(zmq.SUB) self.sockets.append(sub) sub.setsockopt(zmq.SUBSCRIBE, self.topic) sub.connect(self.iface) import time time.sleep(0.25) msg1 = 'message' logger.info(msg1) (topic, msg2) = sub.recv_multipart() self.assertEquals(topic, asbytes('zmq.INFO')) self.assertEquals(msg2, asbytes(msg1 + '\n')) logger.removeHandler(handler)
def connect_handler(self): logger = self.logger pub, sub = self.create_bound_pair(zmq.PUB, zmq.SUB) handler = handlers.PUBHandler(pub) handler.setLevel(logging.DEBUG) handler.root_topic = self.topic logger.addHandler(handler) sub.setsockopt(zmq.SUBSCRIBE, self.topic) time.sleep(0.1) return logger, handler, sub
def connect_logger(logname, context, iface, root="ip", loglevel=logging.DEBUG): logger = logging.getLogger(logname) if any([isinstance(h, handlers.PUBHandler) for h in logger.handlers]): # don't add a second PUBHandler return loglevel = integer_loglevel(loglevel) lsock = context.socket(zmq.PUB) lsock.connect(iface) handler = handlers.PUBHandler(lsock) handler.setLevel(loglevel) handler.root_topic = root logger.addHandler(handler) logger.setLevel(loglevel)
def test_init_socket(self): pub,sub = self.create_bound_pair(zmq.PUB, zmq.SUB) logger = self.logger handler = handlers.PUBHandler(pub) handler.setLevel(logging.DEBUG) handler.root_topic = self.topic logger.addHandler(handler) self.assertTrue(handler.socket is pub) self.assertTrue(handler.ctx is pub.context) self.assertTrue(handler.ctx is self.context) sub.setsockopt(zmq.SUBSCRIBE, self.topic) import time; time.sleep(0.1) msg1 = 'message' logger.info(msg1) (topic, msg2) = sub.recv_multipart() self.assertEquals(topic, asbytes('zmq.INFO')) self.assertEquals(msg2, asbytes(msg1+'\n')) logger.removeHandler(handler)
def _setup_logging(self): """ Setup the logging module for the process. A custom logging handler is created which publishes the log messages on the port specified in *self.ports['log']* """ # Numeric logging level numeric_level = getattr(logging, self.setup['session']['loglevel'].upper(), None) if not isinstance(numeric_level, int): raise ValueError('Invalid log level: {}'.format( self.setup['session']['loglevel'].capitalize())) # Set level logging.getLogger().setLevel(level=numeric_level) # Create logging publisher first handler = handlers.PUBHandler(self.sockets['log']) logging.getLogger().addHandler(handler) # Allow connections to be made sleep(1)
def _setup_logging(self): """Setup logging""" # Numeric logging level numeric_level = getattr(logging, self.setup['session']['loglevel'].upper(), None) if not isinstance(numeric_level, int): raise ValueError('Invalid log level: {}'.format( self.setup['session']['loglevel'])) # Set level logging.getLogger().setLevel(level=numeric_level) # Publish log log_pub = self.context.socket(zmq.PUB) log_pub.bind(self._tcp_addr(self.setup['port']['log'])) # Create logging publisher first handler = handlers.PUBHandler(log_pub) logging.getLogger().addHandler(handler) # Allow connections to be made time.sleep(1)