forked from Drakkar-Software/OctoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
400 lines (341 loc) · 15.1 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# This file is part of OctoBot (https://github.com/Drakkar-Software/OctoBot)
# Copyright (c) 2023 Drakkar-Software, All rights reserved.
#
# OctoBot is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either
# version 3.0 of the License, or (at your option) any later version.
#
# OctoBot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with OctoBot. If not, see <https://www.gnu.org/licenses/>.
import logging
import os
import shutil
import traceback
import logging.config as config
import sys
import async_channel.channels as channel_instances
import async_channel.enums as channel_enums
import octobot_commons.enums as enums
import octobot_commons.constants as commons_constants
import octobot_commons.logging as common_logging
import octobot_commons.channels_name as channels_name
import octobot_commons.pretty_printer as pretty_printer
import octobot_evaluators.evaluators.channel as evaluator_channels
import octobot_trading.exchange_channel as exchanges_channel
import octobot_trading.enums as trading_enums
import octobot.constants as constants
import octobot.configuration_manager as configuration_manager
BOT_CHANNEL_LOGGER = None
LOGGER_PRIORITY_LEVEL = channel_enums.ChannelConsumerPriorityLevels.OPTIONAL.value
def _log_uncaught_exceptions(ex_cls, ex, tb):
logging.exception("".join(traceback.format_tb(tb)))
logging.exception("{0}: {1}".format(ex_cls, ex))
def init_logger():
try:
if not os.path.exists(constants.LOGS_FOLDER):
os.mkdir(constants.LOGS_FOLDER)
_load_logger_config()
init_bot_channel_logger()
except KeyError:
print(
"Impossible to start OctoBot: the logging configuration can't be found in '"
+ constants.LOGGING_CONFIG_FILE
+ "' please make sure you are running OctoBot from its root directory."
)
os._exit(-1)
logger = logging.getLogger("OctoBot Launcher")
try:
# Force new log file creation not to log at the previous one's end.
logger.parent.handlers[1].doRollover()
except PermissionError:
print(
"Impossible to start OctoBot: the logging file is locked, this is probably due to another running "
"OctoBot instance."
)
os._exit(-1)
sys.excepthook = _log_uncaught_exceptions
return logger
def init_bot_channel_logger():
# overwrite BOT_CHANNEL_LOGGER to apply global logging configuration
global BOT_CHANNEL_LOGGER
BOT_CHANNEL_LOGGER = common_logging.get_logger("OctoBot Channel")
def _load_logger_config():
try:
# use local logging file to allow users to customize the log level
if not os.path.isfile(configuration_manager.get_user_local_config_file()):
if not os.path.exists(commons_constants.USER_FOLDER):
os.mkdir(commons_constants.USER_FOLDER)
shutil.copyfile(constants.LOGGING_CONFIG_FILE, configuration_manager.get_user_local_config_file())
config.fileConfig(configuration_manager.get_user_local_config_file())
except Exception as ex:
config.fileConfig(constants.LOGGING_CONFIG_FILE)
logging.getLogger("Logging Configuration").warning(f"Impossible to initialize local logging configuration file,"
f" using default one. {ex}")
async def init_exchange_chan_logger(exchange_id):
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.OHLCV_CHANNEL.value,
exchange_id).new_consumer(
ohlcv_callback, priority_level=LOGGER_PRIORITY_LEVEL
)
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.BALANCE_CHANNEL.value,
exchange_id).new_consumer(
balance_callback, priority_level=channel_enums.ChannelConsumerPriorityLevels.MEDIUM.value
)
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.TRADES_CHANNEL.value,
exchange_id).new_consumer(
trades_callback, priority_level=channel_enums.ChannelConsumerPriorityLevels.MEDIUM.value
)
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.LIQUIDATIONS_CHANNEL.value,
exchange_id).new_consumer(
liquidations_callback, priority_level=channel_enums.ChannelConsumerPriorityLevels.MEDIUM.value
)
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.POSITIONS_CHANNEL.value,
exchange_id).new_consumer(
positions_callback, priority_level=channel_enums.ChannelConsumerPriorityLevels.MEDIUM.value
)
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.ORDERS_CHANNEL.value,
exchange_id).new_consumer(
orders_callback, priority_level=channel_enums.ChannelConsumerPriorityLevels.MEDIUM.value
)
# secondary logs, very verbose on websockets
if constants.ENV_TRADING_ENABLE_DEBUG_LOGS:
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.RECENT_TRADES_CHANNEL.value,
exchange_id).new_consumer(
recent_trades_callback, priority_level=LOGGER_PRIORITY_LEVEL
)
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.FUNDING_CHANNEL.value,
exchange_id).new_consumer(
funding_callback, priority_level=LOGGER_PRIORITY_LEVEL
)
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.TICKER_CHANNEL.value,
exchange_id).new_consumer(
ticker_callback, priority_level=LOGGER_PRIORITY_LEVEL
)
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.MINI_TICKER_CHANNEL.value,
exchange_id).new_consumer(
mini_ticker_callback, priority_level=LOGGER_PRIORITY_LEVEL
)
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.ORDER_BOOK_CHANNEL.value,
exchange_id).new_consumer(
order_book_callback, priority_level=LOGGER_PRIORITY_LEVEL
)
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.ORDER_BOOK_TICKER_CHANNEL.value,
exchange_id).new_consumer(
order_book_ticker_callback, priority_level=LOGGER_PRIORITY_LEVEL
)
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.KLINE_CHANNEL.value,
exchange_id).new_consumer(
kline_callback, priority_level=LOGGER_PRIORITY_LEVEL
)
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.MARK_PRICE_CHANNEL.value,
exchange_id).new_consumer(
mark_price_callback, priority_level=LOGGER_PRIORITY_LEVEL
)
await exchanges_channel.get_chan(channels_name.OctoBotTradingChannelsName.BALANCE_PROFITABILITY_CHANNEL.value,
exchange_id).new_consumer(
balance_profitability_callback, priority_level=channel_enums.ChannelConsumerPriorityLevels.MEDIUM.value
)
async def init_evaluator_chan_logger(matrix_id: str):
await evaluator_channels.get_chan(channels_name.OctoBotEvaluatorsChannelsName.MATRIX_CHANNEL.value,
matrix_id).new_consumer(
matrix_callback, priority_level=LOGGER_PRIORITY_LEVEL
)
await evaluator_channels.get_chan(channels_name.OctoBotEvaluatorsChannelsName.EVALUATORS_CHANNEL.value,
matrix_id).new_consumer(
evaluators_callback, priority_level=LOGGER_PRIORITY_LEVEL
)
async def init_octobot_chan_logger(bot_id: str):
await channel_instances.get_chan_at_id(constants.OCTOBOT_CHANNEL, bot_id).new_consumer(
octobot_channel_callback,
priority_level=LOGGER_PRIORITY_LEVEL,
bot_id=bot_id,
subject=[enums.OctoBotChannelSubjects.NOTIFICATION.value, enums.OctoBotChannelSubjects.ERROR.value]
)
async def ticker_callback(
exchange: str, exchange_id: str, cryptocurrency: str, symbol: str, ticker
):
BOT_CHANNEL_LOGGER.debug(
f"TICKER : EXCHANGE = {exchange} || CRYPTOCURRENCY = {cryptocurrency} "
f"|| SYMBOL = {symbol} || TICKER = {ticker}"
)
async def mini_ticker_callback(
exchange: str, exchange_id: str, cryptocurrency: str, symbol: str, mini_ticker
):
BOT_CHANNEL_LOGGER.debug(
f"MINI TICKER : EXCHANGE = {exchange} || CRYPTOCURRENCY = {cryptocurrency} "
f"|| SYMBOL = {symbol} || MINI TICKER = {mini_ticker}"
)
async def order_book_callback(
exchange: str, exchange_id: str, cryptocurrency: str, symbol: str, asks, bids
):
BOT_CHANNEL_LOGGER.debug(
f"ORDERBOOK : EXCHANGE = {exchange} || CRYPTOCURRENCY = {cryptocurrency} "
f"|| SYMBOL = {symbol} || ASKS = {len(asks)} orders || BIDS = {len(bids)} orders"
)
async def order_book_ticker_callback(
exchange: str,
exchange_id: str,
cryptocurrency: str,
symbol: str,
ask_quantity,
ask_price,
bid_quantity,
bid_price,
):
BOT_CHANNEL_LOGGER.debug(
f"ORDERBOOK TICKER : EXCHANGE = {exchange} || SYMBOL = {symbol} "
f"|| ASK PRICE / QUANTIY = {ask_price} / {ask_quantity}"
f"|| BID PRICE / QUANTIY = {bid_price} / {bid_quantity}"
)
async def ohlcv_callback(
exchange: str,
exchange_id: str,
cryptocurrency: str,
symbol: str,
time_frame,
candle,
):
BOT_CHANNEL_LOGGER.debug(
f"OHLCV : EXCHANGE = {exchange} || CRYPTOCURRENCY = {cryptocurrency} || SYMBOL = {symbol} "
f"|| TIME FRAME = {time_frame} || CANDLE = {candle}"
)
async def recent_trades_callback(
exchange: str, exchange_id: str, cryptocurrency: str, symbol: str, recent_trades
):
BOT_CHANNEL_LOGGER.debug(
f"RECENT TRADES : EXCHANGE = {exchange} || CRYPTOCURRENCY = {cryptocurrency} "
f"|| SYMBOL = {symbol} || 10 first RECENT TRADES = {recent_trades[:10]}"
)
async def liquidations_callback(
exchange: str, exchange_id: str, cryptocurrency: str, symbol: str, liquidations
):
BOT_CHANNEL_LOGGER.debug(
f"LIQUIDATIONS : EXCHANGE = {exchange} || CRYPTOCURRENCY = {cryptocurrency} "
f"|| SYMBOL = {symbol} || LIQUIDATIONS = {liquidations}"
)
async def kline_callback(
exchange: str, exchange_id: str, cryptocurrency: str, symbol: str, time_frame, kline
):
BOT_CHANNEL_LOGGER.debug(
f"KLINE : EXCHANGE = {exchange} || CRYPTOCURRENCY = {cryptocurrency} || SYMBOL = {symbol} "
f"|| TIME FRAME = {time_frame} || KLINE = {kline}"
)
async def mark_price_callback(
exchange: str, exchange_id: str, cryptocurrency: str, symbol: str, mark_price
):
BOT_CHANNEL_LOGGER.debug(
f"MARK PRICE : EXCHANGE = {exchange} || CRYPTOCURRENCY = {cryptocurrency} "
f"|| SYMBOL = {symbol} || MARK PRICE = {mark_price}"
)
async def balance_callback(exchange: str, exchange_id: str, balance):
BOT_CHANNEL_LOGGER.debug(f"BALANCE : EXCHANGE = {exchange} || BALANCE = {balance}")
async def balance_profitability_callback(
exchange: str,
exchange_id: str,
profitability,
profitability_percent,
market_profitability_percent,
initial_portfolio_current_profitability,
):
BOT_CHANNEL_LOGGER.debug(
f"BALANCE PROFITABILITY : EXCHANGE = {exchange} || PROFITABILITY = "
f"{pretty_printer.portfolio_profitability_pretty_print(profitability, profitability_percent, 'USDT')}"
)
async def trades_callback(
exchange: str,
exchange_id: str,
cryptocurrency: str,
symbol: str,
trade: dict,
old_trade: bool,
):
BOT_CHANNEL_LOGGER.debug(
f"TRADES : EXCHANGE = {exchange} || CRYPTOCURRENCY = {cryptocurrency} || SYMBOL = {symbol} "
f"|| TRADE = {trade} "
f"|| OLD_TRADE = {old_trade}"
)
async def orders_callback(
exchange: str,
exchange_id: str,
cryptocurrency: str,
symbol: str,
order: dict,
update_type: str,
is_from_bot: bool,
):
order_string = f"ORDERS : EXCHANGE = {exchange} || SYMBOL = {symbol} || " \
f"{pretty_printer.open_order_pretty_printer(exchange, order)} || " \
f"status = {order.get(trading_enums.ExchangeConstantsOrderColumns.STATUS.value, None)} || " \
f"UPDATE_TYPE = {update_type} || FROM_BOT = {is_from_bot}"
BOT_CHANNEL_LOGGER.debug(order_string)
async def positions_callback(
exchange: str,
exchange_id: str,
cryptocurrency: str,
symbol: str,
position,
is_updated: bool
):
BOT_CHANNEL_LOGGER.debug(f"POSITIONS : EXCHANGE = {exchange} || POSITIONS = {position}")
async def funding_callback(
exchange: str,
exchange_id: str,
cryptocurrency: str,
symbol: str,
funding_rate,
predicted_funding_rate,
next_funding_time,
timestamp,
):
BOT_CHANNEL_LOGGER.debug(
f"FUNDING : EXCHANGE = {exchange} || CRYPTOCURRENCY = {cryptocurrency} || SYMBOL = {symbol} "
f"|| RATE = {str(funding_rate)} || NEXT RATE = {str(predicted_funding_rate)}"
f"|| NEXT TIME = {str(next_funding_time)} || TIMESTAMP = {str(timestamp)}"
)
async def matrix_callback(
matrix_id,
evaluator_name,
evaluator_type,
eval_note,
eval_note_type,
exchange_name,
cryptocurrency,
symbol,
time_frame,
):
BOT_CHANNEL_LOGGER.debug(
f"MATRIX : EXCHANGE = {exchange_name} || "
f"EVALUATOR = {evaluator_name} || EVALUATOR_TYPE = {evaluator_type} || "
f"CRYPTOCURRENCY = {cryptocurrency} || SYMBOL = {symbol} || TF = {time_frame} "
f"|| NOTE = {eval_note} [MATRIX id = {matrix_id}] "
)
async def evaluators_callback(
matrix_id,
evaluator_name,
evaluator_type,
exchange_name,
cryptocurrency,
symbol,
time_frame,
data,
):
BOT_CHANNEL_LOGGER.debug(
f"EVALUATORS : EXCHANGE = {exchange_name} || "
f"EVALUATOR = {evaluator_name} || EVALUATOR_TYPE = {evaluator_type} || "
f"CRYPTOCURRENCY = {cryptocurrency} || SYMBOL = {symbol} || TF = {time_frame} "
f"|| DATA = {data} [MATRIX id = {matrix_id}] "
)
async def octobot_channel_callback(
bot_id: str,
subject: str,
action: str,
data: dict
):
BOT_CHANNEL_LOGGER.debug(
f"OCTOBOT_CHANNEL : SUBJECT = {subject} || ACTION = {action} || DATA = {data} "
)