Spaces:
Runtime error
Runtime error
# models/logging_config.py | |
import os | |
import logging | |
def setup_logging(): | |
log_dir = os.environ.get('LOG_DIR', '/app/logs') | |
try: | |
os.makedirs(log_dir, exist_ok=True) | |
log_file = os.path.join(log_dir, 'app.log') | |
# Ensure log file exists and is writable | |
if not os.path.exists(log_file): | |
open(log_file, 'a').close() | |
try: | |
os.chmod(log_file, 0o666) | |
except Exception as chmod_err: | |
# On Windows or restricted environments, this may fail | |
pass | |
logging.basicConfig( | |
level=logging.INFO, | |
format='%(asctime)s - %(levelname)s - %(message)s', | |
handlers=[ | |
logging.FileHandler(log_file, encoding='utf-8', delay=True), | |
logging.StreamHandler() | |
] | |
) | |
return logging.getLogger(__name__) | |
except Exception as e: | |
# Fallback to console-only logging if file logging fails | |
logging.basicConfig( | |
level=logging.INFO, | |
format='%(asctime)s - %(levelname)s - %(message)s', | |
handlers=[logging.StreamHandler()] | |
) | |
logging.warning(f"File logging setup failed, using console only: {e}") | |
return logging.getLogger(__name__) | |
logger = setup_logging() | |