def __init__(self, uri=None, level=NOTSET, filter=None, bubble=False, context=None, multi=False): """ Safe zmq handler constructor that calls the ZeroMQHandler constructor and does some extra initializations. """ # The current `SafeZMQHandler` uses the `ZeroMQHandler` constructor # which creates a socket each time. # The purpose of the `self._sockets` attribute is to prevent cases in # which we use the same logger in different threads. For instance when # we (in the same file) `deferToThread` a method/function, we are using # the same logger/socket without calling get_logger again. # If we want to reuse the socket, we need to rewrite this constructor # instead of calling the ZeroMQHandler's one. # The best approach may be to inherit directly from `logbook.Handler`. args = (self, uri, level, filter, bubble, context, multi) ZeroMQHandler.__init__(*args) current_id = self._get_caller_id() # we store the socket created on the parent self._sockets = {current_id: self.socket} # store the settings for new socket creation self._multi = multi self._uri = uri
def create_zeromq_handler(uri="tcp://127.0.0.1:5000", level=None, filter=None, bubble=True, context=None, multi=True): return ZeroMQHandler(uri, level, filter, bubble, context, multi)
def initLog(logfile, logzmqhost): """ 用于子进程中的服务的日志 :param logfile: :param logzmqhost: :return: """ global logFileHandler, streamHandler logFileHandler = FileHandler(logfile, bubble=True, level='NOTICE') # 子进程中的屏幕输出需要通过 ZMQ 来提交,ServerEngine 中订阅并显示 if __debug__: print("日志zmq接口 {}".format(logzmqhost)) streamHandler = ZeroMQHandler("tcp://{}".format(logzmqhost), level="DEBUG", bubble=True) if __debug__: streamHandler.applicationbound()
def handlers_subscriber(multi): from logbook.queues import ZeroMQHandler, ZeroMQSubscriber # Get an unused port tempsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tempsock.bind(('127.0.0.1', 0)) host, unused_port = tempsock.getsockname() tempsock.close() # Retrieve the ZeroMQ handler and subscriber uri = 'tcp://%s:%d' % (host, unused_port) if multi: handlers = [ZeroMQHandler(uri, multi=True) for _ in range(3)] else: handlers = [ZeroMQHandler(uri)] subscriber = ZeroMQSubscriber(uri, multi=multi) # Enough time to start time.sleep(0.1) return handlers, subscriber
def get_nestedlog(level='DEBUG', filename='quantrade.log', uri=None): # Default uri: tcp://127.0.0.1:5540 if uri is not None: log_setup = NestedSetup([ ZeroMQHandler(uri), ]) else: log_setup = NestedSetup([ logbook.NullHandler(level=logbook.DEBUG, bubble=True), logbook.StreamHandler(sys.stdout, level=logbook.INFO, format_string=log_format), logbook.StreamHandler(sys.stderr, level=logbook.ERROR, format_string=log_format), logbook.FileHandler('{}/{}'.format(log_destination, filename), level=level), ]) return log_setup
parser.add_argument('--log-uri', help="A ZMQ socket where all the logs will be sent") parser.add_argument('--debug', action='store_true', help="Enable debugging logging") args = parser.parse_args() entries_file = args.entries log_uri = args.log_uri dispatcher = None if not args.log_uri: log_uri, dispatcher = get_logs_dispatcher(uri=log_uri, debug=args.debug) with ZeroMQHandler(log_uri, multi=True): logger = Logger("Onitu") ioloop.install() loop = ioloop.IOLoop.instance() arbiter = circus.get_arbiter([ { 'cmd': 'redis-server', 'args': 'redis/redis.conf', 'copy_env': True, 'priority': 1, }, { 'cmd': sys.executable, 'args': ['-m', 'onitu.referee', log_uri],
import sys from logbook.queues import ZeroMQHandler from local_storage import start if __name__ == '__main__': with ZeroMQHandler(sys.argv[2], multi=True).applicationbound(): start(sys.argv[1])
def test_missing_zeromq(): from logbook.queues import ZeroMQHandler, ZeroMQSubscriber with pytest.raises(RuntimeError): ZeroMQHandler('tcp://127.0.0.1:42000') with pytest.raises(RuntimeError): ZeroMQSubscriber('tcp://127.0.0.1:42000')
setup = NestedSetup([ #StderrHandler(format_string=u'[{record.time:%Y-%m-%d %H:%M}] {record.channel} - {record.level_name}: {record.message} \t({record.extra[ip]})'), StreamHandler(sys.stdout, format_string=log_format), # then write messages that are at least warnings to to a logfile FileHandler(os.environ['QTRADE_LOG'], level='WARNING'), Processor(inject_information) ]) color_setup = NestedSetup([ StreamHandler(sys.stdout, format_string=log_format), ColorizedStderrHandler(format_string=log_format, level='NOTICE'), Processor(inject_information) ]) remote_setup = NestedSetup([ ZeroMQHandler('tcp://127.0.0.1:5540'), Processor(inject_information) ]) log = Logger('Trade Labo') #TODO: reimplement fatal function with (colors ?) exit '''--------------------------------------------------------------------------------------- Logger class ---------------------------------------------------------------------------------------''' class LogSubsystem(object): ''' Trade logging version ''' def __init__(self, name='default', lvl='debug', file_channel=False):