如何更改PHP在错误日志文件中写入错误的方式? [英] How to change the way PHP writes errors in the error log file?

查看:150
本文介绍了如何更改PHP在错误日志文件中写入错误的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在网站上工作了一年多,我非常渴望最终将其发布给人们使用.但是,它已经变得非常庞大-我几乎想说出自己的控制力-而且最重要的是,我实际上只是一个自学成才的业余程序员.

I have been working on my website for over a year now, and I am very anxious to finally put it out there for people to use. It has gotten quite large however - I almost want to say out of my control - and on top of that I am really just an self taught amateur programmer.

因此,我想确定php产生的任何错误都记录在一个文件中,因此我可以访问该文件并跟踪错误.

So I want to be sure, that any errors that php produces are logged in a file, so I can than access this file and track errors down.

当前我的设置如下:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', 1);
ini_set('error_log', 'errors.log');
?>

到目前为止效果很好,我的error.log文件将包含以下内容:

Works pretty good so far, my error.log file will contain stuff like this:

[2013年5月14日00:16:26] PHP注意:未定义的变量:/home/www/dir/index.php在第14行上不存在变量[14-May-2013 00:16:28] PHP注意:未定义的变量:第14行的/home/www/dir/index.php中不存在的变量

[14-May-2013 00:16:26] PHP Notice: Undefined variable: nonexistentvariable in /home/www/dir/index.php on line 14[14-May-2013 00:16:28] PHP Notice: Undefined variable: nonexistentvariable in /home/www/dir/index.php on line 14

太好了,会记录错误.

但是现在我有一个问题:

But now I have a problem:

  1. 它们全部成一行,没有间断.使其难以阅读.如何在新行中获取每个错误?

  1. They are all in one line, no breaks. Makes it hard to read. How do I get each error in a new line?

我看到有一个时间戳.惊人的!我还如何添加用户的IP地址或其他任何自定义内容?

I see there is a timestamp. Awesome! How can I also add things like the user's IP address, or any other custom things?

同样,我的问题:

如何更改PHP在错误日志文件中写入错误的方式? 尤其是,在记录每个错误之后,我如何才能换行,以使error.log文件更易于阅读.以及如何添加自定义数据和值(例如IP地址)?

答案: 我最终做了以下工作-这似乎在某种程度上重现了php在标准情况下所做的事情,并且可以从那里进行修改.

ANSWER: I ended up doing the following - this seems to somewhat reproduce what php is doing by standard and can from there be modified.

<?php
function my_error_handler($type, $message, $file, $line, $vars)
{
    switch($type) 
    { 
        case 1: // 1 // 
            $type_str = 'ERROR'; 
            break;
        case 2: // 2 // 
            $type_str = 'WARNING';
            break;
        case 4: // 4 // 
            $type_str = 'PARSE';
            break;
        case 8: // 8 // 
            $type_str = 'NOTICE'; 
            break;
        case 16: // 16 // 
            $type_str = 'CORE_ERROR'; 
            break;
        case 32: // 32 // 
            $type_str = 'CORE_WARNING'; 
            break;
        case 64: // 64 // 
            $type_str = 'COMPILE_ERROR'; 
            break;
        case 128: // 128 // 
            $type_str = 'COMPILE_WARNING'; 
            break;
        case 256: // 256 // 
            $type_str = 'USER_ERROR'; 
            break;
        case 512: // 512 // 
            $type_str = 'USER_WARNING'; 
            break;
        case 1024: // 1024 // 
            $type_str = 'USER_NOTICE'; 
            break;
        case 2048: // 2048 // 
            $type_str = 'STRICT'; 
            break;
        case 4096: // 4096 // 
            $type_str = 'RECOVERABLE_ERROR'; 
            break;
        case 8192: // 8192 // 
            $type_str = 'DEPRECATED'; 
            break;
        case 16384: // 16384 // 
            $type_str = 'USER_DEPRECATED'; 
            break;
    }


    $errormessage =  '[ '.date(r).' ] '.$type_str.': '.$message.' in '.$file.' on line '.$line."\n";
   // for development simply ECHO $errormessage;

        $file = 'my_errors.log';
        file_put_contents($file, $errormessage, FILE_APPEND);
}

error_reporting(E_ALL);
ini_set('display_errors', '0');
set_error_handler('my_error_handler');

?>

推荐答案

您无法使用内置错误处理程序进行任何其他自定义或实现任何逻辑,您需要使用以下命令设置您自己的错误处理程序 set_error_handler() .

You can't do any additional customization or implement any of that logic with the built-in error handler, you'd need to set your own error handler with set_error_handler().

此函数接受一个回调,当PHP遇到错误时将调用该回调,您可以采取相应的行动.

This function takes a callback which will be invoked when PHP encounters an error, and you can act accordingly.

有关如何使用此功能以及如何实现适当的回调的信息,请参阅文档上的示例.

See the example on the docs for how to use this function and how to implement a proper callback.

这篇关于如何更改PHP在错误日志文件中写入错误的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆