线上项目调试不方便打印本地日志的解决思路

343次阅读
没有评论

调试线上项目,经常会需要打印日志来定位问题,但是并不是所有的项目环境都适合在本地打印日志。

这里可能会遇上的问题有

  1. 框架不够友好,打印日志太凌乱,很难找到自己需要调试的数据

  2. 线上环境打印的日子没有查看权限

这种情况我的解决思路就是通过 curl 方法把我想要打印的日志打印到我自己都服务器中,然后再在自己的服务器中查看打印的日子。

服务器端 putlog.php

<?php

if(empty($_GET['sign'])) die('end');

if($_GET['sign'] != 'bomxcn')die('end');

$a= file_get_contents("php://input");
if(empty($a)){$a=$_POST;}
if(!is_string($a)){$a = json_encode($a, 256);
}
$t = date('Y-m-d H:i:s')."\n";
$a = $t.$a."\n\n";

file_put_contents('log.txt',$a,FILE_APPEND);

线上环境
在需要打印日志的地方添加如下的代码

$post_request =  function($data){
    $url = 'http://159.xxx.xxx.xxx/putlog.php?sign=bomxcn';
    $ch = curl_init();
    $header = [
        'Content-Type: application/json',
        'Content-Length: ' . strlen(json_encode($data))
    ];
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
};

// $data 即为需要打印的日志数据
$post_request(['data'=>$data]);

这样简单粗暴的解决了日志打印调试的问题。

正文完
有偿技术支持加微信
post-qrcode
 1
评论(没有评论)
验证码