Skip to content

Commit

Permalink
Added logger path listener - ListenLogger class
Browse files Browse the repository at this point in the history
  • Loading branch information
evgrmn committed Jun 4, 2024
1 parent d15769d commit 3d47a9c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 26 deletions.
9 changes: 4 additions & 5 deletions api/bitmex/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class Agent(Bitmex):
logger = logging.getLogger(__name__)

def get_active_instruments(self) -> int:
data = Send.request(self, path=Listing.GET_ACTIVE_INSTRUMENTS, verb="GET")
Expand All @@ -29,7 +28,7 @@ def get_active_instruments(self) -> int:
if self.Instrument.get_keys():
for symbol in self.symbol_list:
if symbol not in self.Instrument.get_keys():
Agent.logger.error(
self.logger.error(
"Unknown symbol: "
+ str(symbol)
+ ". 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."
Expand Down Expand Up @@ -62,7 +61,7 @@ def get_instrument(self, symbol: tuple):
category = Agent.fill_instrument(self, instrument=instrument)
self.symbol_category[instrument["symbol"]] = category
else:
Agent.logger.info(str(symbol) + " not found in get_instrument()")
self.logger.info(str(symbol) + " not found in get_instrument()")

def fill_instrument(self, instrument: dict) -> str:
"""
Expand Down Expand Up @@ -143,13 +142,13 @@ def get_position(self, symbol: tuple) -> OrderedDict:
self.Instrument[symbol].currentQty = data[0]["currentQty"]
else:
self.positions[symbol] = {"POS": 0}
Agent.logger.info(
self.logger.info(
str(symbol)
+ " has been added to the positions dictionary for "
+ self.name
)
else:
Agent.logger.info(str(symbol) + " not found in get_position()")
self.logger.info(str(symbol) + " not found in get_position()")

def trade_bucketed(
self, symbol: tuple, time: datetime, timeframe: int
Expand Down
3 changes: 1 addition & 2 deletions api/bitmex/ws.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import logging
import threading
import time
import traceback
Expand Down Expand Up @@ -51,7 +50,7 @@ def __init__(self):
self.currency_divisor = {"XBt": 100000000, "USDt": 1000000, "BMEx": 1000000}
self.timefrs = {1: "1m", 5: "5m", 60: "1h"}
self.symbol_category = dict()
self.logger = logging.getLogger(__name__)
self.logger = var.logger
self.robots = OrderedDict()
self.frames = dict()
self.robot_status = dict()
Expand Down
22 changes: 10 additions & 12 deletions api/bybit/agent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
import threading
from datetime import datetime, timedelta, timezone
from typing import Union
Expand All @@ -11,14 +10,13 @@

@exceptions_manager
class Agent(Bybit):
logger = logging.getLogger(__name__)

def get_active_instruments(self):
def get_in_thread(category, success, num):
cursor = "no"
while cursor:
cursor = ""
Agent.logger.info(
self.logger.info(
"Sending get_instruments_info() - category - " + category
)
result = self.session.get_instruments_info(
Expand Down Expand Up @@ -54,7 +52,7 @@ def get_in_thread(category, success, num):
if self.Instrument.get_keys():
for symbol in self.symbol_list:
if symbol not in self.Instrument.get_keys():
Agent.logger.error(
self.logger.error(
"Unknown symbol: "
+ str(symbol)
+ ". 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."
Expand All @@ -71,7 +69,7 @@ def get_user(self) -> None:
Returns the user ID and other useful information about the user and
places it in self.user. If unsuccessful, logNumFatal is not 0.
"""
Agent.logger.info("Sending get_uid_wallet_type()")
self.logger.info("Sending get_uid_wallet_type()")
data = self.session.get_uid_wallet_type()
if isinstance(data, dict):
self.user = data
Expand All @@ -83,10 +81,10 @@ def get_user(self) -> None:
message = (
"A user ID was requested from the exchange but was not received. Reboot"
)
Agent.logger.error(message)
self.logger.error(message)

def get_instrument(self, symbol: tuple) -> None:
Agent.logger.info(
self.logger.info(
"In get_instrument - sending get_instruments_info() - symbol - "
+ str(symbol)
)
Expand All @@ -103,7 +101,7 @@ def get_position(self, symbol: tuple = False):
def trade_bucketed(
self, symbol: tuple, time: datetime, timeframe: str
) -> Union[list, None]:
Agent.logger.info(
self.logger.info(
"Sending get_kline() - symbol - "
+ str(symbol)
+ " - interval - "
Expand Down Expand Up @@ -149,7 +147,7 @@ def get_in_thread(category, startTime, limit, success, num):
nonlocal trade_history
cursor = "no"
while cursor:
Agent.logger.info(
self.logger.info(
"Sending get_executions() - category - "
+ category
+ " - startTime - "
Expand Down Expand Up @@ -237,7 +235,7 @@ def request_open_orders(parameters: dict):
parameters.pop("success")
parameters.pop("num")
while cursor:
Agent.logger.info(
self.logger.info(
"Sending open_orders() - parameters - " + str(parameters)
)
result = self.session.get_open_orders(**parameters)
Expand Down Expand Up @@ -345,7 +343,7 @@ def remove_order(self, order: dict):

def get_wallet_balance(self) -> None:
for account_type in self.account_types:
Agent.logger.info(
self.logger.info(
"Sending get_wallet_balance() - accountType - " + account_type
)
data = self.session.get_wallet_balance(accountType=account_type)
Expand Down Expand Up @@ -386,7 +384,7 @@ def get_position_info(self):
def get_in_thread(category, settlCurrency, success, num):
cursor = "no"
while cursor:
Agent.logger.info(
self.logger.info(
"Sending get_positions() - category - "
+ category
+ " - settlCurrency - "
Expand Down
5 changes: 1 addition & 4 deletions api/bybit/errors.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import logging
import traceback
from datetime import datetime, timezone

from api.variables import Variables
from common.variables import Variables as var

logger = logging.getLogger(__name__)


def exception(method):
"""
Expand Down Expand Up @@ -192,7 +189,7 @@ def decorator(*args, **kwargs):
# os.abort()
if message:
message = message.replace("\n", " ")
logger.error(message)
var.logger.error(message)
var.queue_info.put(
{
"market": self.name,
Expand Down
3 changes: 2 additions & 1 deletion api/bybit/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from api.init import Setup
from api.variables import Variables
from common.data import MetaAccount, MetaInstrument, MetaResult
from common.variables import Variables as var
from services import exceptions_manager

from .pybit.unified_trading import HTTP, WebSocket
Expand Down Expand Up @@ -46,7 +47,7 @@ def __init__(self):
}
self.account_types = ["UNIFIED", "CONTRACT"]
self.ws_private = WebSocket
self.logger = logging.getLogger(__name__)
self.logger = var.logger
if self.depth == "quote":
self.orderbook_depth = 1
else:
Expand Down
15 changes: 15 additions & 0 deletions common/init.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import time
import logging
import sqlite3
Expand All @@ -15,6 +16,18 @@
from functions import Function

db_sqlite = var.env["SQLITE_DATABASE"]
var.working_directory = os.path.abspath(os.getcwd())


class ListenLogger(logging.Filter):
def filter(self, record):
path = record.pathname.replace(var.working_directory, "")[:-3]
path = path.replace("/", ".")
if path[0] == ".":
path = path[1:]
record.name = path
return True



class Init(WS, Variables):
Expand Down Expand Up @@ -292,6 +305,8 @@ def setup_logger():
logger.addHandler(ch)
logger.addHandler(handler)
logger.info("\n\nhello\n")
filter_logger = ListenLogger()
logger.addFilter(filter_logger)

return logger

Expand Down
3 changes: 2 additions & 1 deletion common/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Variables:
"COMMISSION SUM",
"FUNDING SUM",
]
logger = logging
logger: logging
connect_sqlite = None
cursor_sqlite = None
error_sqlite = None
Expand All @@ -145,3 +145,4 @@ class Variables:
queue_info = queue.Queue()
queue_order = queue.Queue()
lock = threading.Lock()
working_directory: str
2 changes: 1 addition & 1 deletion history.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Bitmex 2000-06-03 13:58:58
Bitmex 2000-06-04 04:00:00
Bybit 2000-06-03 10:17:46

0 comments on commit 3d47a9c

Please sign in to comment.