【PHP】ThinkPHP5 实现sentry通知,记录日志

小破孩
2022-06-21 / 0 评论 / 118 阅读 / 正在检测是否收录...
    <?php
    namespace app\index\common;
    use think\Log;
    class Logs {
        //sentry日志收集器
        static public function sentryLogs($e,$line,array $data = [])
        {
            Log::init([
                'type'  => 'File',
                'path'  => LOG_PATH,
            ]);
            $sentryClient = new \Raven_Client(
                'https://c5485e6c233347ca8a7990a2bf77514a:9a69f3eb68c446f8ae3247eb368c1ff6@sentry.io/1193480',
                [
                    'name' => \Raven_Compat::gethostname(),//服务器主机名
                    'environment' => 'production',
                    'level' => 'error',
                    //附加数据
                    'extra' => $data,
                    'app_path' => ROOT_PATH,
                    'sample_rate' => 1,//值0.00将拒绝发送任何事件,值1.00将发送100%的事件。
                    'curl_method' => 'async',//curl异步发送,比同步体验好很多
                    //回掉方法,在发送数据之前操作
                    'send_callback' => function ($data) {
                    },
                ]);
    
            //单独设置用户信息
            $sentryClient->user_context([
                  'id' => 2966,
                     'username' => 'XPH',
                     'email' => '3584685883@qq.com',
                     'mobile' => '176****376',
                     'ip_address' => $_SERVER['REMOTE_ADDR']
             ]);
    
            if ( $e && is_object($e) ) {
                $sentryClient->captureException($e);
                $errorMsg = "\n文件:".$e->getFile()."\n行数:".$e->getLine()."\n错误代码:".$e->getCode()."\n错误信息:".$e->getMessage()."\n";
            }else{
                //当没有异常只想记录信息的时候可以使用这个
                $sentryClient->captureMessage($e);
                $errorMsg = $e."\n行数:".$line;
            }
            Log::record($errorMsg);
        }
    }
0

评论 (0)

取消