- Logger: use root_path()/runtime/log/ instead of runtime_path()/log/ (runtime_path() returns app-specific path in multi-app mode) - Remove debug output from Log controller
41 lines
1.4 KiB
PHP
41 lines
1.4 KiB
PHP
<?php
|
|
namespace app\utils;
|
|
|
|
class Logger
|
|
{
|
|
const CAT_CONNECT = 'connect';
|
|
const CAT_GAME = 'game';
|
|
const CAT_BET = 'bet';
|
|
const CAT_ERROR = 'error';
|
|
const CAT_FRONTEND = 'frontend';
|
|
|
|
public static function log(string $category, string $level, string $message, array $context = []): void
|
|
{
|
|
$date = date('Y-m-d');
|
|
$time = date('Y-m-d H:i:s');
|
|
$dir = root_path() . 'runtime' . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . $category;
|
|
if (!is_dir($dir)) {
|
|
mkdir($dir, 0755, true);
|
|
}
|
|
$file = $dir . DIRECTORY_SEPARATOR . $date . '.log';
|
|
$contextStr = $context ? ' ' . json_encode($context, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : '';
|
|
$line = "[{$time}][{$level}] {$message}{$contextStr}" . PHP_EOL;
|
|
file_put_contents($file, $line, FILE_APPEND | LOCK_EX);
|
|
}
|
|
|
|
public static function info(string $category, string $message, array $context = []): void
|
|
{
|
|
self::log($category, 'INFO', $message, $context);
|
|
}
|
|
|
|
public static function warn(string $category, string $message, array $context = []): void
|
|
{
|
|
self::log($category, 'WARN', $message, $context);
|
|
}
|
|
|
|
public static function error(string $category, string $message, array $context = []): void
|
|
{
|
|
self::log($category, 'ERROR', $message, $context);
|
|
}
|
|
}
|