Post log messages to Matrix #6

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

View File

@ -0,0 +1,3 @@
# Kraken bot
A Python bot for day-trading cryptocurrencies on the Kraken exchange.

View File

@ -0,0 +1,55 @@
import asyncio
import logging
from nio import AsyncClient, MatrixRoom, RoomMessageText
class MatrixHandler(logging.Handler):
def __init__(self, url, user_id, user_password, room_id):
super().__init__()
self.client = AsyncClient(url, user_id)
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.room_id = room_id
self.client.add_event_callback(self.message_callback, RoomMessageText)
await self.client.login(user_password)
def message_callback(self, room: MatrixRoom, event: RoomMessageText) -> None:
return
def emit(self, record):
self.client.room_send(
self.room_id,
message_type='m.room.message',
content={'msgtype': 'm.text', 'body': record}
)
class MatrixLogger:
def __init__(self, name):
self.logger = logging.getLogger(name)
self.logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
matrix_handler = MatrixHandler(room_id)
matrix_handler.setLevel(logging.INFO)
matrix_handler.setFormatter(logging.Formatter('%(levelname)s - %(message)s'))
self.logger.addHandler(console_handler)
self.logger.addHandler(matrix_handler)
def debug(self, message):
self.logger.debug(message)
def info(self, message):
self.logger.info(message)
def warning(self, message):
self.logger.warning(message)
def error(self, message):
self.logger.error(message)
def critical(self, message):
self.logger.critical(message)