Files
Socket/app/utils/Logger.php
T
testadmin d77c6beac7 feat: add logging module for backend + frontend error reporting
Backend:
- New Logger utility (app/utils/Logger.php) with category-based log files
  (connect/game/bet/error/frontend under runtime/log/)
- Log WebSocket connect/close events (WsConnect, WsClose)
- Log dealer auth success/failure (SpaceConnectService)
- Log all game events: startBet, endBet, changeBoot, opening for all 7 game types
- Log unhandled exceptions (ExceptionHandle)
- Separate SQL/error logs via apart_level, enable realtime_write for Swoole
- Add /log/report POST endpoint for frontend error ingestion

Frontend:
- New error-report.js with window.onerror + unhandledrejection auto-reporting
- Added to all 12 dealer HTML templates
- Bump VERSION_V to v10.1.0
2026-05-14 11:28:33 +08:00

41 lines
1.3 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 = runtime_path() . '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);
}
}