• PHP应用日志记录


    $log = array();
    $log['time']               = date('Y-m-d H:i:s',time());
    $log['ip']                 = IpHelper::realIp();
    $log["REDIRECT_STATUS"]    = isset($_SERVER["HTTP_HOST"])?$_SERVER["HTTP_HOST"]:'';
    $log['user_id']            = $_SESSION['id'];
    $log['session_id']         = session_id();
    $log['get']                = isset($_GET)    ? http_build_query($_GET):'';
    $log['post']               = isset($_POST)   ? http_build_query($_POST):'';
    $log['session']            = isset($_SESSION)? http_build_query($_SESSION):'';
    $log["REDIRECT_STATUS"]    = isset($_SERVER["REDIRECT_STATUS"])?$_SERVER["REDIRECT_STATUS"]:'';
    $log["HTTP_REFERER"]       = isset($_SERVER["HTTP_REFERER"])?$_SERVER["HTTP_REFERER"]:'';
    $log["HTTP_COOKIE"]        = isset($_SERVER["HTTP_COOKIE"])?$_SERVER["HTTP_COOKIE"]:'';
    $log["REDIRECT_URL"]       = isset($_SERVER["REDIRECT_URL"])?$_SERVER["REDIRECT_URL"]:'';
    $log["REQUEST_URI"]        = isset($_SERVER["REQUEST_URI"])?$_SERVER["REQUEST_URI"]:'';
    $log["REQUEST_TIME_FLOAT"] = isset($_SERVER["REQUEST_TIME_FLOAT"])?$_SERVER["REQUEST_TIME_FLOAT"]:'';
    $log["REQUEST_TIME"]       = isset($_SERVER["REQUEST_TIME"])?$_SERVER["REQUEST_TIME"]:'';
    $log["HTTP_USER_AGENT"]    = isset($_SERVER["HTTP_USER_AGENT"])?$_SERVER["HTTP_USER_AGENT"]:'';
    $curl = curl_init();
    $options = [
        CURLOPT_URL => 'http://日志服务器/index.php?data='.urlencode(str_replace(array("
    ", "
    ", "
    "), '',var_export($log,true))),
        CURLOPT_RETURNTRANSFER => 1,        #将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
        CURLOPT_FOLLOWLOCATION => 1,        #启用时会将服务器服务器返回的"Location: "放在header中递归的返回给服务器,使用CURLOPT_MAXREDIRS可以限定递归返回的数量。
        CURLOPT_USERAGENT => "",
        CURLOPT_TIMEOUT => 1,
        CURLOPT_NOSIGNAL =>1,               #只获取信息,不下载内容
        CURLOPT_NOBODY =>true
    ];
    curl_setopt_array($curl, $options);
    curl_exec($curl);
    curl_close($ch);
    ignore_user_abort(true);
    $data = isset($_GET['data']) ? $_GET['data']: '';
    if($data === '')
    {
        exit;
    }
    if(function_exists('fastcgi_finish_request'))
    {
        fastcgi_finish_request(); //如果是fastcgi 环境 快速响应
    }
    file_put_contents('/data_log/'.date('Ymd',time()).'.log', $data. PHP_EOL , FILE_APPEND | LOCK_EX );
    /* 15分钟的执行时间 */
    set_time_limit(60*15); 
    $f_name = date('Ymd',strtotime("-9 day")) .'.log';
    $path = '/data_log/';
    if( file_exists( $path.$f_name ) !== false )
    {
        //exit; /* 日志不存在 */
        /* 打包 */
        exec("cd {$path} && tar -cjf {$f_name}.tar.bz2 {$f_name} && rm -f {$f_name}");
    }
    0 4 * * * /usr/bin/php "/data/日志压缩.php" > /data_log/cron.log 2>&1

     一个简单的日志查看方法

    <?php
    $handle = @fopen("log.txt", "r");
    if ($handle) 
    {
        $i = 1;
        while (($buffer = fgets($handle)) !== false) 
        {
            $str = '$data='.$buffer.';';
            eval($str);
            $data['get'] = urldecode($data['get']);
            $data['post'] = urldecode($data['post']);
            $data['session'] = urldecode($data['session']);
            $data['HTTP_REFERER'] = urldecode($data['HTTP_REFERER']);
            $data['HTTP_COOKIE'] = urldecode($data['HTTP_COOKIE']);
            $data['REDIRECT_URL'] = urldecode( $data['REDIRECT_URL']);
            $data['REQUEST_URI'] = urldecode($data['REQUEST_URI']);
            echo $i,',',$data['REQUEST_URI'], PHP_EOL;
            $i++;
        }
        
        if (!feof($handle)) {
            echo "Error: unexpected fgets() fail
    ";
        }
        fclose($handle);
    }
  • 相关阅读:
    Spring中的事务传播行为与隔离级别
    redis分布式共享锁模拟抢单的实现
    synchronized关键字修饰非静态方法与静态方法的区别
    docker安装Tensorflow并使用jupyter notebook
    python 图像处理(4):图像的绘制
    python 图像处理(3):图像数据类型及颜色空间转换
    python 图像处理 (2):图像像素的访问与裁剪
    python 读取图像的几种方法
    python 图像处理:图像的读取、显示与保存
    NLP 正向、逆向 分词
  • 原文地址:https://www.cnblogs.com/toumingbai/p/11339745.html
Copyright © 2020-2023  润新知