<?php
namespace App\Core;

/** Pencatat log berkas sederhana (harian). */
class Logger
{
    public static function write(string $level, string $message): void
    {
        try {
            $dir = Config::get('paths.logs', dirname(__DIR__, 2) . '/storage/logs');
            if (!is_dir($dir)) @mkdir($dir, 0775, true);
            $file = rtrim($dir, '/') . '/app-' . date('Y-m-d') . '.log';
            $line = sprintf("[%s] %s: %s%s", date('Y-m-d H:i:s'), strtoupper($level), $message, PHP_EOL);
            @file_put_contents($file, $line, FILE_APPEND | LOCK_EX);
        } catch (\Throwable $e) {
            // logging tidak boleh menggagalkan permintaan
        }
    }

    public static function info(string $m): void    { self::write('info', $m); }
    public static function warning(string $m): void { self::write('warning', $m); }
    public static function error(string $m): void   { self::write('error', $m); }
}
