stability

This commit is contained in:
Dobromir Popov
2025-08-08 12:09:15 +03:00
parent c58ec789f2
commit e39e9ee95a
2 changed files with 74 additions and 16 deletions

View File

@ -317,4 +317,32 @@ def setup_logging(config: Optional[Config] = None):
except Exception as e:
logger.warning(f"Failed to initialize error log handler: {e}")
# Add dedicated trade log to capture all NN signals (executed and ignored)
try:
from logging.handlers import RotatingFileHandler
trade_log_dir = Path('logs')
trade_log_dir.mkdir(parents=True, exist_ok=True)
trade_log_path = trade_log_dir / 'trades.log'
class FlushingTradeHandler(RotatingFileHandler):
def emit(self, record):
super().emit(record)
self.flush()
trade_handler = FlushingTradeHandler(
str(trade_log_path), maxBytes=20*1024*1024, backupCount=10, encoding='utf-8'
)
trade_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
trade_handler.setFormatter(trade_formatter)
trade_handler.setLevel(logging.INFO)
trade_logger = logging.getLogger('trade_log')
trade_logger.setLevel(logging.INFO)
trade_logger.addHandler(trade_handler)
# Avoid double propagation to root to keep trade log clean
trade_logger.propagate = False
logger.info("Trade log handler initialized at logs/trades.log")
except Exception as e:
logger.warning(f"Failed to initialize trade log handler: {e}")
logger.info("Logging configured successfully with SafeFormatter")