python - How to Log to a variable or write observer that sends messages to variable in Twisted / Autobahn -
i writing websocket client receive updates every few seconds or utilizing autobahn twisted. logging data using multiple observers, want use part of messages receiving send dataframe (and plot in real time). assumption can log variable file-like object, cannot figure out how that. correct way achieve this.
i have thoroughly read docs current , legacy twisted loggers:
twisted.log https://twistedmatrix.com/documents/current/core/howto/logging.html
twisted.logger https://twistedmatrix.com/documents/current/core/howto/logger.html
in code have tried use zope.interface , @provider referenced in new twisted.logger package create custom log observer have had no luck far getting custom log observer print, let alone send data variable.
from twisted.internet import reactor autobahn.twisted.websocket import websocketclientfactory, websocketclientprotocol, connectws twisted.logger import (globallogbeginner, logger, globallogpublisher, jsonfilelogobserver, ilogobserver) import sys import io import json pandas import dataframe def loggit(message): log.info("echo: {message!r}", message=message) class clientprotocol(websocketclientprotocol): def onconnect(self, response): print("server connected: {0}".format(response.peer)) def initmessage(self): message_data = {} message_json = json.dumps(message_data) print "sendmessage: " + message_json self.sendmessage(message_json) def onopen(self): print "onopen calls initmessage()" self.initmessage() def onmessage(self, msg, binary, df): loggit(msg) def onclose(self, wasclean, code, reason): print("websocket connection closed: {0}".format(reason)) if __name__ == '__main__': factory = websocketclientfactory("wss://ws-feed.whatever.com") factory.protocol = clientprotocol @provider(ilogobserver) def customobserver(whatgoeshere?): print event observers = [jsonfilelogobserver(io.open("loga.json", "a")), jsonfilelogobserver(io.open("logb.json", "a")), customobserver(whatgoeshere?)] log = logger() globallogbeginner.beginloggingto(observers) connectws(factory) reactor.run()
a log observer callable object takes dictionary containing values part of log message.
this means can have instance of class __call__
method decorated @zope.interface.implementer(ilogobserver)
, or function decorated @zope.interface.provider(ilogobserver)
, can perform role.
here's example of code logs values text file, json file, , in-memory statistics collector sums things on fly.
import io zope.interface import implementer twisted.logger import (globallogbeginner, logger, jsonfilelogobserver, ilogobserver, textfilelogobserver) class something(object): log = logger() def dosomething(self, value): self.log.info("doing {value}", value=value) @implementer(ilogobserver) class realtimestatistics(object): def __init__(self): self.stats = [] def __call__(self, event): if 'value' in event: self.stats.append(event['value']) def reportcurrent(self): print("current sum is: " + repr(sum(self.stats))) if __name__ == "__main__": stats = realtimestatistics() globallogbeginner.beginloggingto([ jsonfilelogobserver(io.open("log1.json", "ab")), textfilelogobserver(io.open("log2.txt", "ab")), stats, # here pass our log observer ], redirectstandardio=false) = something() something.dosomething(1) something.dosomething(2) something.dosomething(3) stats.reportcurrent()
Comments
Post a Comment