PHP项目中,记录错误日志

一、场景介绍:

环境:LNMP

我们通常是通过nginx的错误日志来分析分错的,也就是我们在各个server中定义的error_log。

比如下面这样,就是将错误日志定义在/etc/nginx/logs/error/www.xiaobudiu.top.log,发生错误,可以查看的对应错误日志文件即可。

server {
    listen       80 default_server;
    server_name  www.xiaobudiu.top;

    charset utf-8;
    error_log    /etc/nginx/logs/error/www.xiaobudiu.top.log error;
    access_log  /etc/nginx/logs/access/www.xiaobudiu.top.log main;

    root   /data/www;
    index  index.html index.htm index.php;

    location /favicon.ico {
        log_not_found off;
        access_log off;
    }

    error_page  404 403 500 502 503 504  /404.html;

   location = /404.html {
        root   /data/errorPage;
   }

   location ~ \.php$ {
        fastcgi_pass   unix:/dev/shm/php-cgi.sock;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
   }

    location ~ /\.ht {
        deny  all;
    }

}

但是,这只是将一个网站的所有错误日志都放在了一起,十分不利于分析。

比如,用户订单操作错误时,我们最好把有关订单的错误写到一个日志。再比如登录操作错误的,我们最好也是把出错误日志写在登录错误日志中,如何实现呢。

这里,我们只需要在程序中自定义一个错误日志函数即可,然后,在程序中进行相应的判断,如果程序没执行成功,则调用记录错误日志函数,比如下面这样。



二、自定义错误日志格式,并进行记录日志


1、程序中编写相应程序

<?php
function set_debug($uid = '', $order = '', $data = ''){
    $error_path = 'order.error.html';//自定义错误日志保存的文件和路径
    $error_data = array(
        'time' => date("Y-m-d H:i",time()),//记录错误发生的时间
        'error' => urlencode($data),//防止中文乱码
        'order'=> $order,//记录订单
        'user_name'=> $uid,//记录当前用户
    );
    //判断文件大小,选择追加还是重新写入,注意之前防止乱码用了urlencode
    if( abs(filesize($error_path)) < 10240 ){
        @file_put_contents($error_path, urldecode(json_encode($error_data))."<br>",FILE_APPEND);
    }else{
        @file_put_contents($error_path, urldecode(json_encode($error_data)) ."<br>");
    };
}

//模拟订单录入错误时,将日志记录到错误日志中
$uid = 1000070;//模拟用户uid
$order = 2132215641000070;//模拟用户订单号
if (true)  {
    set_debug($uid,$order,'订单录入失败');
}


2、创建错误文件,并赋予权限

 cd /data/www

 touch order.error.html

 chmod 777 order.error.html 


3、效果


注:鉴于安全考虑,错误日志外人肯定是不能访问到的,所以可以在server中定义正则匹配location,要求指定ip地址段才可以访问这个文件。可以使用nginx自带的 http_access_module 模块进行ip鉴权。当然,也可以采取其他办法。

这里,以http_access_module 模块ip鉴权为例。

server {
    listen       80 default_server;
    server_name  www.xiaobudiu.top;

    charset utf-8;
    error_log    /etc/nginx/logs/error/www.xiaobudiu.top.log error;
    access_log  /etc/nginx/logs/access/www.xiaobudiu.top.log main;

    root   /data/www;
    index  index.html index.htm index.php;

    location /favicon.ico {
        log_not_found off;
        access_log off;
    }

    error_page  404 403 500 502 503 504  /404.html;

   location = /404.html {
        root   /data/errorPage;
   }

   location ~ \.php$ {
        fastcgi_pass   unix:/dev/shm/php-cgi.sock;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
   }

   location ~ ^/order.error.html$ {
       allow 183.128.227.94;  #这个ip能访问order.error.html
       deny all;              #其他的不可以访问这个页面
   }

    location ~ /\.ht {
        deny  all;
    }

}

这样,就实现了除了我规定的ip地址外,其他ip地址是访问不到这个文件的。

注:也可以参考https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80688740 最后面内容。



三、使用PHP自带的error_log 或者 trigger_error 函数

代码:

<?php
$uid = 1000060;
if (true) {
    //采取下面记录php错误日志的其中一种
    if (true) {
        error_log($uid."订单录入错误,请核实后重新录入");
    }
    if (true) {
        trigger_error($uid."订单录入错误,请核实后重新录入,heihei~");
    }
}

效果:



总结:其实在项目中,最好还是使用自定义的记录错误函数比较好,简单明了,而使用error_log 或者 trigger_error 则没有太大必要,当做一个辅助措施即可。


猜你喜欢

转载自blog.csdn.net/m_nanle_xiaobudiu/article/details/80942930