当前位置:   article > 正文

PHP应用日志记录

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("\r\n", "\r", "\n"), '',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\n";
    }
    fclose($handle);
}

 

转载于:https://www.cnblogs.com/toumingbai/p/11339745.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/191380
推荐阅读
相关标签
  

闽ICP备14008679号