refactoring wip broken
This commit is contained in:
@ -9,6 +9,11 @@ from telegram import Bot
|
||||
from telegram.constants import ParseMode
|
||||
from config import TELEGRAM_BOT_TOKEN, DEVELOPER_CHAT_ID, BOT_NAME
|
||||
|
||||
|
||||
import time
|
||||
import logging
|
||||
from logging.handlers import RotatingFileHandler
|
||||
|
||||
class TelegramUtils:
|
||||
def __init__(self):
|
||||
self.bot = None
|
||||
@ -37,7 +42,71 @@ class TelegramUtils:
|
||||
if self.conn_pool:
|
||||
await self.conn_pool.close()
|
||||
|
||||
|
||||
class Log:
|
||||
# Set up success logger for accounting CSV
|
||||
class CSVFormatter(logging.Formatter):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.output = None
|
||||
|
||||
def format(self, record):
|
||||
if self.output is None:
|
||||
self.output = csv.writer(record.stream)
|
||||
self.output.writerow(['Timestamp', 'Token In', 'Token Out', 'Amount In', 'Amount Out', 'USD Value In', 'USD Value Out', 'Transaction Hash', 'Wallet Address'])
|
||||
self.output.writerow([
|
||||
self.formatTime(record, self.datefmt),
|
||||
record.token_in,
|
||||
record.token_out,
|
||||
record.amount_in,
|
||||
record.amount_out,
|
||||
record.usd_value_in,
|
||||
record.usd_value_out,
|
||||
record.tx_hash,
|
||||
record.wallet_address
|
||||
])
|
||||
return ''
|
||||
|
||||
def __init__(self):
|
||||
logger = logging.getLogger(__name__)
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
#logging.basicConfig(level=logging.INFO)
|
||||
|
||||
# Set up error logger
|
||||
log_dir = './logs'
|
||||
log_file = os.path.join(log_dir, 'error.log')
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
error_file_handler = RotatingFileHandler( log_file, maxBytes=10*1024*1024, backupCount=5)
|
||||
error_file_handler.setLevel(logging.ERROR)
|
||||
error_file_handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S') )
|
||||
error_file_handler.formatter.converter = time.localtime
|
||||
error_logger = logging.getLogger('error_logger')
|
||||
error_logger.setLevel(logging.ERROR)
|
||||
error_logger.addHandler(error_file_handler)
|
||||
success_log_file = os.path.join(log_dir, 'successful_swaps.csv')
|
||||
success_file_handler = RotatingFileHandler(success_log_file, maxBytes=10*1024*1024, backupCount=5)
|
||||
success_file_handler.setFormatter(self.CSVFormatter())
|
||||
success_logger_accounting_csv = logging.getLogger('success_logger_accounting_csv')
|
||||
success_logger_accounting_csv.setLevel(logging.INFO)
|
||||
success_logger_accounting_csv.addHandler(success_file_handler)
|
||||
|
||||
|
||||
def log_successful_swap(token_in, token_out, amount_in, amount_out, usd_value_in, usd_value_out, tx_hash, wallet_address):
|
||||
success_logger_accounting_csv.info('', extra={
|
||||
'token_in': token_in,
|
||||
'token_out': token_out,
|
||||
'amount_in': amount_in,
|
||||
'amount_out': amount_out,
|
||||
'usd_value_in': usd_value_in,
|
||||
'usd_value_out': usd_value_out,
|
||||
'tx_hash': tx_hash,
|
||||
'wallet_address': wallet_address
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
# Create a global instance of TelegramUtils
|
||||
telegram_utils = TelegramUtils()
|
||||
|
||||
log = Log()
|
||||
# You can add more Telegram-related methods to the TelegramUtils class if needed
|
Reference in New Issue
Block a user