<?php
// lib/Logger.php
class Logger {
    private static $log_file = __DIR__ . '/../logs/app.log';
    
    public static function init() {
        if (!is_dir(dirname(self::$log_file))) {
            mkdir(dirname(self::$log_file), 0755, true);
        }
    }
    
    public static function info($message, $context = []) {
        self::writeLog('INFO', $message, $context);
    }
    
    public static function error($message, $context = []) {
        self::writeLog('ERROR', $message, $context);
    }
    
    public static function warning($message, $context = []) {
        self::writeLog('WARNING', $message, $context);
    }
    
    private static function writeLog($level, $message, $context = []) {
        $timestamp = date('Y-m-d H:i:s');
        $context_str = !empty($context) ? json_encode($context) : '';
        $log_entry = "[$timestamp] [$level] $message $context_str" . PHP_EOL;
        
        file_put_contents(self::$log_file, $log_entry, FILE_APPEND | LOCK_EX);
    }
    
    public static function getRecentLogs($limit = 100) {
        if (!file_exists(self::$log_file)) return [];
        
        $lines = file(self::$log_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        return array_slice(array_reverse($lines), 0, $limit);
    }
}

// Инициализация логгера
Logger::init();
?>