php 如何将错误和警告记录到文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3531703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:06:20  来源:igfitidea点击:

How do I log errors and warnings into a file?

phperror-handling

提问by Gorep

How do I turn on all error and warnings and log them to a file, but to set up all of that within the script (not changing anything in php.ini)?

如何打开所有错误和警告并将它们记录到文件中,但要在脚本中设置所有这些(不更改 php.ini 中的任何内容)?

I want to define a file name and so that all errors and warnings get logged into it.

我想定义一个文件名,以便将所有错误和警告记录到其中。

回答by Aman

Use the following code:

使用以下代码:

ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");
error_log( "Hello, errors!" );

Then watch the file:

然后看文件:

tail -f /tmp/php-error.log

Or update php.inias described in this blog entryfrom 2008.

或者php.ini按照2008 年这篇博客文章中的描述进行更新。

回答by Gordon

See

Example

例子

error_log("You messed up!", 3, "/var/tmp/my-errors.log");

You can customize error handling with your own error handlersto call this function for you whenever an error or warning or whatever you need to log occurs. For additional information, please refer to the Chapter Error Handling in the PHP Manual

您可以使用自己的错误处理程序自定义错误处理,以便在发生错误或警告或需要记录的任何内容时为您调用此函数。更多信息请参考PHP手册中的错误处理章节

回答by Yousha Aleayoub

Simply put these codes at top of your PHP/index file:

只需将这些代码放在 PHP/index 文件的顶部:

error_reporting(E_ALL); // Error/Exception engine, always use E_ALL

ini_set('ignore_repeated_errors', TRUE); // always use TRUE

ini_set('display_errors', FALSE); // Error/Exception display, use FALSE only in production environment or real server. Use TRUE in development environment

ini_set('log_errors', TRUE); // Error/Exception file logging engine.
ini_set('error_log', 'your/path/to/errors.log'); // Logging file path

回答by T.Todua

add this code in .htaccess(as an alternative of php.ini/ ini_setfunction):

.htaccess 中添加此代码(作为php.ini/ ini_set函数的替代):

<IfModule mod_php5.c>
php_flag log_errors on 
php_value error_log ./path_to_MY_PHP_ERRORS.log
# php_flag display_errors on 
</IfModule>

* as commented: this is for Apache-type servers, and not for Nginxor others.

* 评论:这是针对Apache类型的服务器,而不是针对Nginx或其他服务器。

回答by Juergen

That's my personal short function

这是我个人的简短功能

# logging
/*
[2017-03-20 3:35:43] [INFO] [file.php] Here we are
[2017-03-20 3:35:43] [ERROR] [file.php] Not good
[2017-03-20 3:35:43] [DEBUG] [file.php] Regex empty

mylog ('hallo') -> INFO
mylog ('fail', 'e') -> ERROR
mylog ('next', 'd') -> DEBUG
mylog ('next', 'd', 'debug.log') -> DEBUG file debug.log
*/
function mylog($text, $level='i', $file='logs') {
    switch (strtolower($level)) {
        case 'e':
        case 'error':
            $level='ERROR';
            break;
        case 'i':
        case 'info':
            $level='INFO';
            break;
        case 'd':
        case 'debug':
            $level='DEBUG';
            break;
        default:
            $level='INFO';
    }
    error_log(date("[Y-m-d H:i:s]")."\t[".$level."]\t[".basename(__FILE__)."]\t".$text."\n", 3, $file);
}

回答by Frxstrem

Take a look at the log_errorsconfiguration option in php.ini. It seems to do just what you want to. I think you can use the error_logoption to set your own logging file too.

查看log_errorsphp.ini中的配置选项。它似乎只是做你想做的。我认为您也可以使用该error_log选项来设置自己的日志文件。

When the log_errorsdirective is set to On, any errors reported by PHP would be logged to the server log or the file specified with error_log. You can set these options with ini_settoo, if you need to.

当该log_errors指令设置为 时On,PHP 报告的任何错误都将记录到服务器日志或用 指定的文件中error_logini_set如果需要,您也可以设置这些选项。

(Please note that display_errorsshould be disabled in php.ini if this option is enabled)

(请注意,display_errors如果启用此选项,则应在 php.ini 中禁用该选项)

回答by Stéphane

In addition, you need the "AllowOverride Options" directive for this to work. (Apache 2.2.15)

此外,您需要“AllowOverride Options”指令才能使其工作。(阿帕奇 2.2.15)