Skip to content

Releases: zxc7563598/oh-shit-logger

v2.0.0

09 Oct 10:59

Choose a tag to compare

⚠️ 项目完全重构,与 1.x 不兼容


🚀 部署说明

📦 方式一:直接使用编译版本

下载下方 app-linux.zip 到服务器,解压后进入目录执行:

chmod +x ./app
./app -port=9999 -retain=7 -user=admin -pass=123123

可用参数说明:

参数 说明 默认值
port 运行端口号 9999
retain 日志保留天数 7
user BasicAuth 用户名(建议设置) -
pass BasicAuth 密码(建议设置) -

userpass 均不传递时,将关闭 BasicAuth 认证,不建议关闭认证,避免错误信息被不相关的人看到

启动后访问:
👉 http://您的服务器IP:端口号/read
即可查看错误信息。

注意:服务器需要开放对应端口


🧰 方式二:自行构建

同步项目到本地或服务器:

git clone https://github.com/zxc7563598/oh-shit-logger.git ./oh-shit-logger

构建项目:

cd oh-shit-logger
go build -o ./app main.go

运行项目(与上方一致):

./app -port=9999

启动后访问 http://您的服务器IP:端口号/read 查看错误列表。


🐘 如何在 PHP 中使用

在各项目的异常处理逻辑中,将错误信息格式化为统一结构并上报:

添加在异常处理的位置,不管什么框架总该要有一个统一的异常处理类

/**
 * 格式化 Throwable 为标准 JSON 字符串
 * 将返回的数据 POST 到您的服务器 /write 接口,例如:http://您的服务器IP:端口号/write
 *
 * @param Throwable $e 错误对象
 * @param array $context 可选的上下文信息,用于帮助定位问题
 */
function formatThrowable(Throwable $e, array $context = []): string
{
    $trace = array_map(static function ($t) {
        return [
            'file'     => $t['file'] ?? null,
            'line'     => $t['line'] ?? null,
            'function' => $t['function'] ?? null,
            'class'    => $t['class'] ?? null,
        ];
    }, $e->getTrace() ?? []);

    $data = [
        'uuid'      => bin2hex(random_bytes(8)),
        'project'   => 'project', // 你的项目名
        'level'     => 'error',
        'timestamp' => date('c'),
        'message'   => $e->getMessage(),
        'code'      => $e->getCode(),
        'file'      => $e->getFile(),
        'line'      => $e->getLine(),
        'trace'     => $trace,
        'context'   => (object)$context,
        'server'    => [
            'hostname'    => gethostname() ?: 'unknown',
            'ip'          => $_SERVER['SERVER_ADDR'] ?? '127.0.0.1',
            'php_version' => PHP_VERSION,
        ],
    ];
    return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}

// 将错误上报到 oh-shit-logger
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://您的服务器IP:端口号/write');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, formatThrowable($exception, [
    'module' => 'user-center',
    'action' => 'login',
]));
curl_exec($ch);
curl_close($ch);

🔍 查看与分析

访问 http://您的服务器IP:端口号/read
即可查看所有已上报的错误日志。

  • 按日期自动分类存储
  • 支持分页加载
  • 内置 DeepSeek 分析,一键生成问题分析思路

v1.0.0

19 May 15:15

Choose a tag to compare

php-tools 项目的情况下自行上报的大概格式如下:

curl -X POST http://localhost:8080/write -H "Content-Type: application/json" -d '{
    "time": "2025-03-14 10:14:36",
    "level": "ERROR",
    "message": "Call to undefined method stdClass::orderBy()",
    "context": {
        "project": "BilibiliDanmuji",
        "ip": "127.0.0.1",
        "method": "POST",
        "full_url": "//127.0.0.1:7776/api/points-mall/user-management/get-data",
        "trace": {
            "message": "Call to undefined method stdClass::orderBy()",
            "file": "/Users/lisiqi/Documents/bilibili-danmuji/app/controller/shop/management/UserManagementController.php",
            "line": 45,
            "trace": [
                {
                "file": "/Users/lisiqi/Documents/bilibili-danmuji/vendor/workerman/webman-framework/src/App.php",
                "line": 343
                }
            ]
        }
    }
}'