File size: 1,291 Bytes
877e000
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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()