99import logging
1010from market_maker .settings import settings
1111from market_maker .auth .APIKeyAuth import generate_expires , generate_signature
12- from market_maker .utils . log import setup_custom_logger
12+ from market_maker .utils import log
1313from market_maker .utils .math import toNearest
1414from future .utils import iteritems
1515from future .standard_library import hooks
1616with hooks (): # Python 2/3 compat
1717 from urllib .parse import urlparse , urlunparse
1818
1919
20+ logger = log .setup_custom_logger ('root' )
21+
22+
2023# Connects to BitMEX websocket for streaming realtime data.
2124# The Marketmaker still interacts with this as if it were a REST Endpoint, but now it can get
2225# much more realtime data without heavily polling the API.
@@ -31,7 +34,6 @@ class BitMEXWebsocket():
3134 MAX_TABLE_LEN = 200
3235
3336 def __init__ (self ):
34- self .logger = logging .getLogger ('root' )
3537 self .__reset ()
3638
3739 def __del__ (self ):
@@ -40,7 +42,7 @@ def __del__(self):
4042 def connect (self , endpoint = "" , symbol = "XBTN15" , shouldAuth = True ):
4143 '''Connect to the websocket and initialize data stores.'''
4244
43- self . logger .debug ("Connecting WebSocket." )
45+ logger .debug ("Connecting WebSocket." )
4446 self .symbol = symbol
4547 self .shouldAuth = shouldAuth
4648
@@ -57,15 +59,15 @@ def connect(self, endpoint="", symbol="XBTN15", shouldAuth=True):
5759 urlParts [0 ] = urlParts [0 ].replace ('http' , 'ws' )
5860 urlParts [2 ] = "/realtime?subscribe=" + "," .join (subscriptions )
5961 wsURL = urlunparse (urlParts )
60- self . logger .info ("Connecting to %s" % wsURL )
62+ logger .info ("Connecting to %s" % wsURL )
6163 self .__connect (wsURL )
62- self . logger .info ('Connected to WS. Waiting for data images, this may take a moment...' )
64+ logger .info ('Connected to WS. Waiting for data images, this may take a moment...' )
6365
6466 # Connected. Wait for partials
6567 self .__wait_for_symbol (symbol )
6668 if self .shouldAuth :
6769 self .__wait_for_account ()
68- self . logger .info ('Got all market data. Starting.' )
70+ logger .info ('Got all market data. Starting.' )
6971
7072 #
7173 # Data methods
@@ -132,7 +134,7 @@ def recent_trades(self):
132134 #
133135 def error (self , err ):
134136 self ._error = err
135- self . logger .error (err )
137+ logger .error (err )
136138 self .exit ()
137139
138140 def exit (self ):
@@ -145,7 +147,7 @@ def exit(self):
145147
146148 def __connect (self , wsURL ):
147149 '''Connect to the websocket in a thread.'''
148- self . logger .debug ("Starting thread" )
150+ logger .debug ("Starting thread" )
149151
150152 ssl_defaults = ssl .get_default_verify_paths ()
151153 sslopt_ca_certs = {'ca_certs' : ssl_defaults .cafile }
@@ -157,11 +159,10 @@ def __connect(self, wsURL):
157159 header = self .__get_auth ()
158160 )
159161
160- setup_custom_logger ('websocket' , log_level = settings .LOG_LEVEL )
161162 self .wst = threading .Thread (target = lambda : self .ws .run_forever (sslopt = sslopt_ca_certs ))
162163 self .wst .daemon = True
163164 self .wst .start ()
164- self . logger .info ("Started thread" )
165+ logger .info ("Started thread" )
165166
166167 # Wait for connect before continuing
167168 conn_timeout = 5
@@ -170,7 +171,7 @@ def __connect(self, wsURL):
170171 conn_timeout -= 1
171172
172173 if not conn_timeout or self ._error :
173- self . logger .error ("Couldn't connect to WS! Exiting." )
174+ logger .error ("Couldn't connect to WS! Exiting." )
174175 self .exit ()
175176 sys .exit (1 )
176177
@@ -180,7 +181,7 @@ def __get_auth(self):
180181 if self .shouldAuth is False :
181182 return []
182183
183- self . logger .info ("Authenticating with API Key." )
184+ logger .info ("Authenticating with API Key." )
184185 # To auth to the WS using an API key, we generate a signature of a nonce and
185186 # the WS API endpoint.
186187 nonce = generate_expires ()
@@ -208,14 +209,14 @@ def __send_command(self, command, args):
208209 def __on_message (self , message ):
209210 '''Handler for parsing WS messages.'''
210211 message = json .loads (message )
211- self . logger .debug (json .dumps (message ))
212+ logger .debug (json .dumps (message ))
212213
213214 table = message ['table' ] if 'table' in message else None
214215 action = message ['action' ] if 'action' in message else None
215216 try :
216217 if 'subscribe' in message :
217218 if message ['success' ]:
218- self . logger .debug ("Subscribed to %s." % message ['subscribe' ])
219+ logger .debug ("Subscribed to %s." % message ['subscribe' ])
219220 else :
220221 self .error ("Unable to subscribe to %s. Error: \" %s\" Please check and restart." %
221222 (message ['request' ]['args' ][0 ], message ['error' ]))
@@ -238,13 +239,13 @@ def __on_message(self, message):
238239 # 'update' - update row
239240 # 'delete' - delete row
240241 if action == 'partial' :
241- self . logger .debug ("%s: partial" % table )
242+ logger .debug ("%s: partial" % table )
242243 self .data [table ] += message ['data' ]
243244 # Keys are communicated on partials to let you know how to uniquely identify
244245 # an item. We use it for updates.
245246 self .keys [table ] = message ['keys' ]
246247 elif action == 'insert' :
247- self . logger .debug ('%s: inserting %s' % (table , message ['data' ]))
248+ logger .debug ('%s: inserting %s' % (table , message ['data' ]))
248249 self .data [table ] += message ['data' ]
249250
250251 # Limit the max length of the table to avoid excessive memory usage.
@@ -253,7 +254,7 @@ def __on_message(self, message):
253254 self .data [table ] = self .data [table ][(BitMEXWebsocket .MAX_TABLE_LEN // 2 ):]
254255
255256 elif action == 'update' :
256- self . logger .debug ('%s: updating %s' % (table , message ['data' ]))
257+ logger .debug ('%s: updating %s' % (table , message ['data' ]))
257258 # Locate the item in the collection and update it.
258259 for updateData in message ['data' ]:
259260 item = findItemByKeys (self .keys [table ], self .data [table ], updateData )
@@ -267,7 +268,7 @@ def __on_message(self, message):
267268 contExecuted = updateData ['cumQty' ] - item ['cumQty' ]
268269 if contExecuted > 0 :
269270 instrument = self .get_instrument (item ['symbol' ])
270- self . logger .info ("Execution: %s %d Contracts of %s at %.*f" %
271+ logger .info ("Execution: %s %d Contracts of %s at %.*f" %
271272 (item ['side' ], contExecuted , item ['symbol' ],
272273 instrument ['tickLog' ], item ['price' ] or updateData ['price' ]))
273274
@@ -279,21 +280,21 @@ def __on_message(self, message):
279280 self .data [table ].remove (item )
280281
281282 elif action == 'delete' :
282- self . logger .debug ('%s: deleting %s' % (table , message ['data' ]))
283+ logger .debug ('%s: deleting %s' % (table , message ['data' ]))
283284 # Locate the item in the collection and remove it.
284285 for deleteData in message ['data' ]:
285286 item = findItemByKeys (self .keys [table ], self .data [table ], deleteData )
286287 self .data [table ].remove (item )
287288 else :
288289 raise Exception ("Unknown action: %s" % action )
289290 except :
290- self . logger .error (traceback .format_exc ())
291+ logger .error (traceback .format_exc ())
291292
292293 def __on_open (self ):
293- self . logger .debug ("Websocket Opened." )
294+ logger .debug ("Websocket Opened." )
294295
295296 def __on_close (self ):
296- self . logger .info ('Websocket Closed' )
297+ logger .info ('Websocket Closed' )
297298 self .exit ()
298299
299300 def __on_error (self , error ):
@@ -318,16 +319,8 @@ def findItemByKeys(keys, table, matchData):
318319
319320if __name__ == "__main__" :
320321 # create console handler and set level to debug
321- logger = logging .getLogger ()
322- logger .setLevel (logging .DEBUG )
323- ch = logging .StreamHandler ()
324- # create formatter
325- formatter = logging .Formatter ("%(asctime)s - %(name)s - %(levelname)s - %(message)s" )
326- # add formatter to ch
327- ch .setFormatter (formatter )
328- logger .addHandler (ch )
322+ logger = log .setup_custom_logger ('websocket' , logging .DEBUG )
329323 ws = BitMEXWebsocket ()
330- ws .logger = logger
331324 ws .connect ("https://testnet.bitmex.com/api/v1" )
332325 while (ws .ws .sock .connected ):
333326 sleep (1 )
0 commit comments