Skip to content

Commit 3d47a9c

Browse files
committed
Added logger path listener - ListenLogger class
1 parent d15769d commit 3d47a9c

File tree

8 files changed

+36
-26
lines changed

8 files changed

+36
-26
lines changed

api/bitmex/agent.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212

1313
class Agent(Bitmex):
14-
logger = logging.getLogger(__name__)
1514

1615
def get_active_instruments(self) -> int:
1716
data = Send.request(self, path=Listing.GET_ACTIVE_INSTRUMENTS, verb="GET")
@@ -29,7 +28,7 @@ def get_active_instruments(self) -> int:
2928
if self.Instrument.get_keys():
3029
for symbol in self.symbol_list:
3130
if symbol not in self.Instrument.get_keys():
32-
Agent.logger.error(
31+
self.logger.error(
3332
"Unknown symbol: "
3433
+ str(symbol)
3534
+ ". Check the SYMBOLS in the .env.Bitmex file. Perhaps the name of the symbol does not correspond to the category or such symbol does not exist. Reboot."
@@ -62,7 +61,7 @@ def get_instrument(self, symbol: tuple):
6261
category = Agent.fill_instrument(self, instrument=instrument)
6362
self.symbol_category[instrument["symbol"]] = category
6463
else:
65-
Agent.logger.info(str(symbol) + " not found in get_instrument()")
64+
self.logger.info(str(symbol) + " not found in get_instrument()")
6665

6766
def fill_instrument(self, instrument: dict) -> str:
6867
"""
@@ -143,13 +142,13 @@ def get_position(self, symbol: tuple) -> OrderedDict:
143142
self.Instrument[symbol].currentQty = data[0]["currentQty"]
144143
else:
145144
self.positions[symbol] = {"POS": 0}
146-
Agent.logger.info(
145+
self.logger.info(
147146
str(symbol)
148147
+ " has been added to the positions dictionary for "
149148
+ self.name
150149
)
151150
else:
152-
Agent.logger.info(str(symbol) + " not found in get_position()")
151+
self.logger.info(str(symbol) + " not found in get_position()")
153152

154153
def trade_bucketed(
155154
self, symbol: tuple, time: datetime, timeframe: int

api/bitmex/ws.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import logging
32
import threading
43
import time
54
import traceback
@@ -51,7 +50,7 @@ def __init__(self):
5150
self.currency_divisor = {"XBt": 100000000, "USDt": 1000000, "BMEx": 1000000}
5251
self.timefrs = {1: "1m", 5: "5m", 60: "1h"}
5352
self.symbol_category = dict()
54-
self.logger = logging.getLogger(__name__)
53+
self.logger = var.logger
5554
self.robots = OrderedDict()
5655
self.frames = dict()
5756
self.robot_status = dict()

api/bybit/agent.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import logging
21
import threading
32
from datetime import datetime, timedelta, timezone
43
from typing import Union
@@ -11,14 +10,13 @@
1110

1211
@exceptions_manager
1312
class Agent(Bybit):
14-
logger = logging.getLogger(__name__)
1513

1614
def get_active_instruments(self):
1715
def get_in_thread(category, success, num):
1816
cursor = "no"
1917
while cursor:
2018
cursor = ""
21-
Agent.logger.info(
19+
self.logger.info(
2220
"Sending get_instruments_info() - category - " + category
2321
)
2422
result = self.session.get_instruments_info(
@@ -54,7 +52,7 @@ def get_in_thread(category, success, num):
5452
if self.Instrument.get_keys():
5553
for symbol in self.symbol_list:
5654
if symbol not in self.Instrument.get_keys():
57-
Agent.logger.error(
55+
self.logger.error(
5856
"Unknown symbol: "
5957
+ str(symbol)
6058
+ ". Check the SYMBOLS in the .env.Bybit file. Perhaps the name of the symbol does not correspond to the category or such symbol does not exist. Reboot."
@@ -71,7 +69,7 @@ def get_user(self) -> None:
7169
Returns the user ID and other useful information about the user and
7270
places it in self.user. If unsuccessful, logNumFatal is not 0.
7371
"""
74-
Agent.logger.info("Sending get_uid_wallet_type()")
72+
self.logger.info("Sending get_uid_wallet_type()")
7573
data = self.session.get_uid_wallet_type()
7674
if isinstance(data, dict):
7775
self.user = data
@@ -83,10 +81,10 @@ def get_user(self) -> None:
8381
message = (
8482
"A user ID was requested from the exchange but was not received. Reboot"
8583
)
86-
Agent.logger.error(message)
84+
self.logger.error(message)
8785

8886
def get_instrument(self, symbol: tuple) -> None:
89-
Agent.logger.info(
87+
self.logger.info(
9088
"In get_instrument - sending get_instruments_info() - symbol - "
9189
+ str(symbol)
9290
)
@@ -103,7 +101,7 @@ def get_position(self, symbol: tuple = False):
103101
def trade_bucketed(
104102
self, symbol: tuple, time: datetime, timeframe: str
105103
) -> Union[list, None]:
106-
Agent.logger.info(
104+
self.logger.info(
107105
"Sending get_kline() - symbol - "
108106
+ str(symbol)
109107
+ " - interval - "
@@ -149,7 +147,7 @@ def get_in_thread(category, startTime, limit, success, num):
149147
nonlocal trade_history
150148
cursor = "no"
151149
while cursor:
152-
Agent.logger.info(
150+
self.logger.info(
153151
"Sending get_executions() - category - "
154152
+ category
155153
+ " - startTime - "
@@ -237,7 +235,7 @@ def request_open_orders(parameters: dict):
237235
parameters.pop("success")
238236
parameters.pop("num")
239237
while cursor:
240-
Agent.logger.info(
238+
self.logger.info(
241239
"Sending open_orders() - parameters - " + str(parameters)
242240
)
243241
result = self.session.get_open_orders(**parameters)
@@ -345,7 +343,7 @@ def remove_order(self, order: dict):
345343

346344
def get_wallet_balance(self) -> None:
347345
for account_type in self.account_types:
348-
Agent.logger.info(
346+
self.logger.info(
349347
"Sending get_wallet_balance() - accountType - " + account_type
350348
)
351349
data = self.session.get_wallet_balance(accountType=account_type)
@@ -386,7 +384,7 @@ def get_position_info(self):
386384
def get_in_thread(category, settlCurrency, success, num):
387385
cursor = "no"
388386
while cursor:
389-
Agent.logger.info(
387+
self.logger.info(
390388
"Sending get_positions() - category - "
391389
+ category
392390
+ " - settlCurrency - "

api/bybit/errors.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import logging
21
import traceback
32
from datetime import datetime, timezone
43

54
from api.variables import Variables
65
from common.variables import Variables as var
76

8-
logger = logging.getLogger(__name__)
9-
107

118
def exception(method):
129
"""
@@ -192,7 +189,7 @@ def decorator(*args, **kwargs):
192189
# os.abort()
193190
if message:
194191
message = message.replace("\n", " ")
195-
logger.error(message)
192+
var.logger.error(message)
196193
var.queue_info.put(
197194
{
198195
"market": self.name,

api/bybit/ws.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from api.init import Setup
77
from api.variables import Variables
88
from common.data import MetaAccount, MetaInstrument, MetaResult
9+
from common.variables import Variables as var
910
from services import exceptions_manager
1011

1112
from .pybit.unified_trading import HTTP, WebSocket
@@ -46,7 +47,7 @@ def __init__(self):
4647
}
4748
self.account_types = ["UNIFIED", "CONTRACT"]
4849
self.ws_private = WebSocket
49-
self.logger = logging.getLogger(__name__)
50+
self.logger = var.logger
5051
if self.depth == "quote":
5152
self.orderbook_depth = 1
5253
else:

common/init.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import time
23
import logging
34
import sqlite3
@@ -15,6 +16,18 @@
1516
from functions import Function
1617

1718
db_sqlite = var.env["SQLITE_DATABASE"]
19+
var.working_directory = os.path.abspath(os.getcwd())
20+
21+
22+
class ListenLogger(logging.Filter):
23+
def filter(self, record):
24+
path = record.pathname.replace(var.working_directory, "")[:-3]
25+
path = path.replace("/", ".")
26+
if path[0] == ".":
27+
path = path[1:]
28+
record.name = path
29+
return True
30+
1831

1932

2033
class Init(WS, Variables):
@@ -292,6 +305,8 @@ def setup_logger():
292305
logger.addHandler(ch)
293306
logger.addHandler(handler)
294307
logger.info("\n\nhello\n")
308+
filter_logger = ListenLogger()
309+
logger.addFilter(filter_logger)
295310

296311
return logger
297312

common/variables.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class Variables:
126126
"COMMISSION SUM",
127127
"FUNDING SUM",
128128
]
129-
logger = logging
129+
logger: logging
130130
connect_sqlite = None
131131
cursor_sqlite = None
132132
error_sqlite = None
@@ -145,3 +145,4 @@ class Variables:
145145
queue_info = queue.Queue()
146146
queue_order = queue.Queue()
147147
lock = threading.Lock()
148+
working_directory: str

history.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Bitmex 2000-06-03 13:58:58
1+
Bitmex 2000-06-04 04:00:00
22
Bybit 2000-06-03 10:17:46

0 commit comments

Comments
 (0)