Commit 4abe8c0f authored by 王源's avatar 王源
Browse files

调整异常日志收集,优化获取trace中真实文件和行数,只对项目app文件夹下的错误获取编码人

parent 12117b7e
Loading
Loading
Loading
Loading
+36 −17
Original line number Diff line number Diff line
@@ -18,33 +18,52 @@ class ExceptionLogProducer extends ProducerMessage

    public function __construct($data)
    {
        if (strpos($data['message'], 'Cannot select any node from load balancer') !== false) {
            return;
        }
        try {
            $data['operator'] = Auth::user()['name'];
        } catch (HttpResponseException $e) {
            put_log('获取操作人失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
        }
        // 排除依赖包文件和运行缓存文件
        // 获取trace中真实文件和行数
        if (!empty($data['trace'])) {
            preg_match('/(\/var\/www\/html\/.+?\/app\/.+?\.php)(?=\()/', $data['trace'], $matchFiles);
            put_log('$matchFiles: ' . json_encode($matchFiles), 'ExceptionLogProducerPreg.log');
            if (!empty($matchFiles[0])) {
                $data['file'] = $matchFiles[0];
                $file = str_replace('/', '\/', $matchFiles[0]);
                $file = str_replace('.', '\.', $file);
                $pattern = '/(?<=' . $file . '\()[0-9]+(?=\))/';
                put_log('$pattern: ' . $pattern, 'ExceptionLogProducerPreg.log');
                preg_match($pattern, $data['trace'], $matchLines);
                put_log('$matchLines: ' . json_encode($matchLines), 'ExceptionLogProducerPreg.log');
                if (!empty($matchLines[0])) {
                    $data['line'] = $matchLines[0];
                }
            }
        }
        put_log("file: {$data['file']}", 'ExceptionLogProducerEcho.log');
        if (strpos($data['file'], '/vendor/') === false && strpos($data['file'], '/runtime/') === false) {
        if (!empty($data['file'])) {
            // 只对项目app文件夹下的错误获取编码人
            preg_match('/(\/var\/www\/html\/.+?\/app\/)/', $data['file'], $matchPaths);
            put_log('$matchPaths: ' . json_encode($matchPaths), 'ExceptionLogProducerPreg.log');
            if (!empty($matchPaths[0])) {
                try {
                put_log("git blame -L {$data['line']},{$data['line']} {$data['file']}", 'ExceptionLogProducerEcho.log');
                $output = [];
                exec("git blame -L {$data['line']},{$data['line']} {$data['file']}", $output);
                    put_log("cd {$matchPaths[0]} && git blame -L {$data['line']},{$data['line']} {$data['file']}", 'ExceptionLogProducerEcho.log');
                    exec("cd {$matchPaths[0]} && git blame -L {$data['line']},{$data['line']} {$data['file']}", $output);
                    put_log("output: " . json_encode($output), 'ExceptionLogProducerEcho.log');
                    if (!empty($output[0]) && is_string($output[0])) {
                    preg_match('/(?<=\()[^ ]+/', $output[0], $matches);
                    put_log("matches: {$matches[0]}", 'ExceptionLogProducerEcho.log');
                    if (!empty($matches[0])) {
                        $data['coder'] = $matches[0];
                        preg_match('/(?<=\()[^ ]+/', $output[0], $matchCoders);
                        put_log('$matchCoders: ' . json_encode($matchCoders), 'ExceptionLogProducerPreg.log');
                        if (!empty($matchCoders[0])) {
                            $data['coder'] = $matchCoders[0];
                        }
                    }
                } catch (Exception $e) {
                    put_log('获取编码人失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
                }
            }
        }
        put_log('----------', 'ExceptionLogProducerPreg.log');
        put_log('----------', 'ExceptionLogProducerEcho.log');
        $this->payload = $data;
    }