Post log messages to Matrix #6

Merged
eric merged 4 commits from matrix_logging into main 2025-02-09 15:48:04 +00:00
5 changed files with 26 additions and 18 deletions
Showing only changes of commit 0ffd06e162 - Show all commits

6
app.py
View File

@ -1,3 +1,5 @@
import asyncio
from dotenv import load_dotenv
from kraken_bot.KrakenBot import KrakenBot
@ -13,5 +15,5 @@ bot = KrakenBot(
)
if __name__ == '__main__':
bot.update(full=True)
bot.execute()
asyncio.run(bot.update(full=True))
asyncio.run(bot.execute())

View File

@ -12,7 +12,7 @@ class KrakenBot(KrakenAPI):
def __init__(self, tier, model, trading_strategy):
self.model = model
self.trading_strategy = trading_strategy
self.log = MatrixLogger(self.__class__.__name__)
self.log = MatrixLogger(str(self.__class__.__name__))
super().__init__(krakenex.API(os.getenv('KRAKEN_API_TOKEN'), os.getenv('KRAKEN_API_SEC')), tier=tier)
self.update(full=True)

View File

@ -1,29 +1,34 @@
import asyncio
import logging
import os
from nio import HttpClient, MatrixRoom, RoomMessageText
from nio import AsyncClient, MatrixRoom, RoomMessageText
class MatrixHandler(logging.Handler):
def __init__(self):
super().__init__()
eric marked this conversation as resolved Outdated
Outdated
Review

AsyncClient will need to be executed like so:

if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        pass

As such, it will need to be instantiated and started elsewhere with the necessary values passed in via parameters.

`AsyncClient` will need to be executed like so: ``` if __name__ == "__main__": try: asyncio.run(main()) except KeyboardInterrupt: pass ``` As such, it will need to be instantiated and started elsewhere with the necessary values passed in via parameters.
Outdated
Review

The HttpClient was used instead. Fortunately, the interface was compatible.

The `HttpClient` was used instead. Fortunately, the interface was compatible.
Outdated
Review

This is still an issue. The Matrix logger likely needs to be redesigned.

This is still an issue. The Matrix logger likely needs to be redesigned.
self.homeserver_address = os.getenv('MATRIX_HOMESERVER_ADDRESS'),
self.user_id = os.getenv('MATRIX_USER_ID'),
self.user_password = os.getenv('MATRIX_USER_PASSWORD'),
self.address = os.getenv('MATRIX_HOMESERVER_ADDRESS')
self.user_id = os.getenv('MATRIX_USER_ID')
self.user_password = os.getenv('MATRIX_USER_PASSWORD')
self.room_id = os.getenv('MATRIX_ROOM_ID')
self.client = HttpClient(self.address, self.user_id)
self.client.add_event_callback(self.message_callback, RoomMessageText)
self.client.login(self.user_password)
self.device_id = os.getenv('MATRIX_DEVICE_ID')
def message_callback(self, room: MatrixRoom, event: RoomMessageText) -> None:
return
def emit(self, record):
self.client.room_send(
async def send_to_matrix(self, record):
client = AsyncClient(self.address, self.user_id)
await client.login(self.user_password, self.device_id)
client.add_event_callback(self.message_callback, RoomMessageText)
await client.room_send(
self.room_id,
message_type='m.room.message',
content={'msgtype': 'm.text', 'body': record}
content={'msgtype': 'm.text', 'body': self.formatter.format(record)}
)
def emit(self, record):
asyncio.run(self.send_to_matrix(record))
class MatrixLogger:
def __init__(self, name):
self.log = logging.getLogger(name)
@ -31,11 +36,11 @@ class MatrixLogger:
console_handler = logging.StreamHandler()
console_handler.setLevel(os.getenv('LOG_LEVEL', 'INFO'))
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %{name}s - %(message)s'))
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s'))
matrix_handler = MatrixHandler()
matrix_handler.setLevel(os.getenv('LOG_LEVEL', 'INFO'))
matrix_handler.setFormatter(logging.Formatter('%(levelname)s - %{name}s - %(message)s'))
matrix_handler.setFormatter(logging.Formatter('%(levelname)s - %(name)s - %(message)s'))
self.log.addHandler(console_handler)
self.log.addHandler(matrix_handler)

View File

@ -6,8 +6,8 @@ from kraken_bot.MatrixLogger import MatrixLogger
class TradingStrategy(ABC):
def __init__(self):
self.investment_count = int(os.getenv('INVESTMENT_COUNT')),
self.investment_volume = float(os.getenv('INVESTMENT_VOLUME')),
self.investment_count = int(os.getenv('INVESTMENT_COUNT'))
self.investment_volume = float(os.getenv('INVESTMENT_VOLUME'))
self.log = MatrixLogger(self.__class__.__name__)
self.orders = []

View File

@ -1,6 +1,7 @@
krakenex
nio
matrix-nio
pykrakenapi
python-dotenv
scipy
backtrader
matplotlib