Restructured hedger modules: moved CLP hedger and auto hedger into separate folders, updated data fetchers and main app, removed deprecated files
This commit is contained in:
131
clp_auto_hedger/logging_utils.py
Normal file
131
clp_auto_hedger/logging_utils.py
Normal file
@ -0,0 +1,131 @@
|
||||
"""
|
||||
Logging utilities module for CLP Auto Hedger
|
||||
|
||||
Provides consistent logging configuration across all modules.
|
||||
Supports different log levels and outputs to both console and files.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from datetime import datetime
|
||||
from logging.handlers import RotatingFileHandler
|
||||
|
||||
|
||||
def setup_logging(level="normal", log_prefix="CLP_HEDGER"):
|
||||
"""
|
||||
Setup logging configuration with console and file output
|
||||
|
||||
Args:
|
||||
level (str): Logging level - "debug", "normal", "quiet"
|
||||
log_prefix (str): Prefix for log files and logger name
|
||||
"""
|
||||
|
||||
# Create logs directory if it doesn't exist
|
||||
logs_dir = os.path.join(os.getcwd(), "logs")
|
||||
if not os.path.exists(logs_dir):
|
||||
os.makedirs(logs_dir)
|
||||
|
||||
# Determine log level
|
||||
if level.lower() == "debug":
|
||||
log_level = logging.DEBUG
|
||||
console_level = logging.DEBUG
|
||||
elif level.lower() == "quiet":
|
||||
log_level = logging.WARNING
|
||||
console_level = logging.WARNING
|
||||
else: # normal
|
||||
log_level = logging.INFO
|
||||
console_level = logging.INFO
|
||||
|
||||
# Create logger
|
||||
logger = logging.getLogger(log_prefix)
|
||||
logger.setLevel(log_level)
|
||||
|
||||
# Clear existing handlers to avoid duplicates
|
||||
logger.handlers.clear()
|
||||
|
||||
# Create formatters
|
||||
detailed_formatter = logging.Formatter(
|
||||
fmt='%(asctime)s (%(name)s) - %(levelname)s - %(message)s',
|
||||
datefmt='%Y-%m-%d %H:%M:%S'
|
||||
)
|
||||
|
||||
console_formatter = logging.Formatter(
|
||||
fmt='%(asctime)s - %(levelname)s - %(message)s',
|
||||
datefmt='%H:%M:%S'
|
||||
)
|
||||
|
||||
# File handler with rotation
|
||||
timestamp = datetime.now().strftime("%Y%m%d")
|
||||
log_file = os.path.join(logs_dir, f"{log_prefix}_{timestamp}.log")
|
||||
|
||||
file_handler = RotatingFileHandler(
|
||||
log_file,
|
||||
maxBytes=50*1024*1024, # 50MB
|
||||
backupCount=5,
|
||||
encoding='utf-8'
|
||||
)
|
||||
file_handler.setLevel(log_level)
|
||||
file_handler.setFormatter(detailed_formatter)
|
||||
|
||||
# Console handler
|
||||
console_handler = logging.StreamHandler(sys.stdout)
|
||||
console_handler.setLevel(console_level)
|
||||
console_handler.setFormatter(console_formatter)
|
||||
|
||||
# Add handlers to logger
|
||||
logger.addHandler(file_handler)
|
||||
logger.addHandler(console_handler)
|
||||
|
||||
# Log initialization
|
||||
logger.info(f"Logging initialized - Level: {level.upper()}")
|
||||
logger.info(f"Log file: {log_file}")
|
||||
logger.info(f"Process ID: {os.getpid()}")
|
||||
|
||||
return logger
|
||||
|
||||
|
||||
def get_logger(name="CLP_HEDGER"):
|
||||
"""
|
||||
Get a logger instance with the specified name
|
||||
|
||||
Args:
|
||||
name (str): Logger name
|
||||
|
||||
Returns:
|
||||
logging.Logger: Logger instance
|
||||
"""
|
||||
return logging.getLogger(name)
|
||||
|
||||
|
||||
def log_system_info(logger):
|
||||
"""
|
||||
Log system information for debugging
|
||||
|
||||
Args:
|
||||
logger: Logger instance to use
|
||||
"""
|
||||
try:
|
||||
import platform
|
||||
logger.info(f"System: {platform.system()} {platform.release()}")
|
||||
logger.info(f"Python: {platform.python_version()}")
|
||||
logger.info(f"Working Directory: {os.getcwd()}")
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
def log_exception(logger, exception, context=""):
|
||||
"""
|
||||
Log exception with context information
|
||||
|
||||
Args:
|
||||
logger: Logger instance to use
|
||||
exception: Exception object
|
||||
context (str): Additional context information
|
||||
"""
|
||||
if context:
|
||||
logger.error(f"Exception in {context}: {type(exception).__name__}: {exception}")
|
||||
else:
|
||||
logger.error(f"Exception: {type(exception).__name__}: {exception}")
|
||||
|
||||
logger.debug("Exception details:", exc_info=True)
|
||||
Reference in New Issue
Block a user