업데이트
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import os
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import logging.handlers
|
||||
import gzip
|
||||
import logging
|
||||
import logging.handlers
|
||||
import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
LOG_DIR = os.getenv("LOG_DIR", "logs")
|
||||
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
|
||||
@@ -56,12 +56,15 @@ def setup_logger(dry_run: bool):
|
||||
|
||||
Log Rotation Strategy:
|
||||
- Size-based: 10MB per file, keep 7 backups (total ~80MB)
|
||||
- Time-based: Daily rotation, keep 30 days
|
||||
- Compression: Old logs are gzipped (saves ~70% space)
|
||||
|
||||
Log Levels (production recommendation):
|
||||
- dry_run=True: INFO (development/testing)
|
||||
- dry_run=False: WARNING (production - only important events)
|
||||
- dry_run=False: INFO (production - retain important trading logs)
|
||||
|
||||
⚠️ CRITICAL: Production mode uses INFO level to ensure trading events are logged.
|
||||
This is essential for auditing buy/sell orders and debugging issues.
|
||||
For high-volume environments, adjust LOG_LEVEL via environment variable.
|
||||
"""
|
||||
global logger, _logger_configured
|
||||
if _logger_configured:
|
||||
@@ -69,8 +72,9 @@ def setup_logger(dry_run: bool):
|
||||
|
||||
logger.handlers.clear()
|
||||
|
||||
# Use WARNING level for production, INFO for development
|
||||
effective_level = getattr(logging, LOG_LEVEL, logging.INFO if dry_run else logging.WARNING)
|
||||
# Use INFO level for both dry_run and production to ensure trading events are logged
|
||||
# Production systems can override via LOG_LEVEL environment variable if needed
|
||||
effective_level = getattr(logging, LOG_LEVEL, logging.INFO)
|
||||
logger.setLevel(effective_level)
|
||||
|
||||
formatter = logging.Formatter("%(asctime)s - %(levelname)s - [%(threadName)s] - %(message)s")
|
||||
@@ -82,30 +86,22 @@ def setup_logger(dry_run: bool):
|
||||
ch.setFormatter(formatter)
|
||||
logger.addHandler(ch)
|
||||
|
||||
# Size-based rotating file handler with compression
|
||||
# Size-based rotating file handler with compression (only one rotation strategy)
|
||||
fh_size = CompressedRotatingFileHandler(
|
||||
LOG_FILE, maxBytes=10 * 1024 * 1024, backupCount=7, encoding="utf-8" # 10MB per file # Keep 7 backups
|
||||
LOG_FILE,
|
||||
maxBytes=10 * 1024 * 1024,
|
||||
backupCount=7,
|
||||
encoding="utf-8", # 10MB per file # Keep 7 backups
|
||||
)
|
||||
fh_size.setLevel(effective_level)
|
||||
fh_size.setFormatter(formatter)
|
||||
logger.addHandler(fh_size)
|
||||
|
||||
# Time-based rotating file handler (daily rotation, keep 30 days)
|
||||
daily_log_file = os.path.join(LOG_DIR, "AutoCoinTrader_daily.log")
|
||||
fh_time = logging.handlers.TimedRotatingFileHandler(
|
||||
daily_log_file, when="midnight", interval=1, backupCount=30, encoding="utf-8"
|
||||
)
|
||||
fh_time.setLevel(effective_level)
|
||||
fh_time.setFormatter(formatter)
|
||||
fh_time.suffix = "%Y-%m-%d" # Add date suffix to rotated files
|
||||
logger.addHandler(fh_time)
|
||||
|
||||
_logger_configured = True
|
||||
|
||||
logger.info(
|
||||
"[SYSTEM] 로그 설정 완료: level=%s, size_rotation=%dMB×%d, daily_rotation=%d일",
|
||||
"[SYSTEM] 로그 설정 완료: level=%s, size_rotation=%dMB×%d (일별 로테이션 제거됨)",
|
||||
logging.getLevelName(effective_level),
|
||||
10,
|
||||
7,
|
||||
30,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user