php 如何在PHP中写入错误日志文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15530039/
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 09:25:20  来源:igfitidea点击:

How to write to error log file in PHP

phpreference

提问by Vasant

I want to write a message to an error log file when executing PHP code.

我想在执行 PHP 代码时将消息写入错误日志文件。

I am trying to use the PHP error_log()function Docs.

我正在尝试使用 PHPerror_log()函数Docs

But it's not working properly for me.

但它对我来说不能正常工作。

回答by suspectus

If you don't want to change anything in your php.ini file, according to PHP documentation, you can do this.

如果您不想更改 php.ini 文件中的任何内容,根据PHP 文档,您可以这样做。

error_log("Error message\n", 3, "/mypath/php.log");

The first parameter is the string to be sent to the log. The second parameter 3means expect a file destination. The third parameter is the log file path.

第一个参数是要发送到日志的字符串。第二个参数3表示期望文件目的地。第三个参数是日志文件路径。

回答by SkaJess

you can simply use :

你可以简单地使用:

error_log("your message");

By default, the message will be send to the php system logger.

默认情况下,消息将发送到 php 系统记录器。

回答by Pupil

We all know that PHP save errors in php_errors.log file.

我们都知道 PHP 将错误保存在 php_errors.log 文件中。

But, that file contains a lot of data.

但是,该文件包含大量数据。

If we want to log our application data, we need to save it to a custom location.

如果我们想记录我们的应用程序数据,我们需要将其保存到自定义位置。

We can use two parameters in the error_log function to achieve this.

我们可以在 error_log 函数中使用两个参数来实现这一点。

http://php.net/manual/en/function.error-log.php

http://php.net/manual/en/function.error-log.php

We can do it using:

我们可以使用:

error_log(print_r($v, TRUE), 3, '/var/tmp/errors.log');

Where,

在哪里,

print_r($v, TRUE) :logs $v (array/string/object) to log file. 3: Put log message to custom log file specified in the third parameter.

print_r($v, TRUE) :将 $v (array/string/object) 记录到日志文件中。 3:将日志消息放入第三个参数中指定的自定义日志文件。

'/var/tmp/errors.log': Custom log file (This path is for Linux, we can specify other depending upon OS).

'/var/tmp/errors.log': 自定义日志文件(此路径适用于 Linux,我们可以根据操作系统指定其他路径)。

OR, you can use file_put_contents()

或者,您可以使用 file_put_contents()

file_put_contents('/var/tmp/e.log', print_r($v, true), FILE_APPEND);

Where:

在哪里:

'/var/tmp/errors.log':Custom log file (This path is for Linux, we can specify other depending upon OS). print_r($v, TRUE) :logs $v (array/string/object) to log file. FILE_APPEND: Constant parameter specifying whether to append to the file if it exists, if file does not exist, new file will be created.

'/var/tmp/errors.log':自定义日志文件(此路径适用于 Linux,我们可以根据操作系统指定其他)。 print_r($v, TRUE) :将 $v (array/string/object) 记录到日志文件中。FILE_APPEND:常量参数,指定文件存在时是否追加,如果文件不存在则新建文件。

回答by Hraday Joshi

You can use normal file operation to create an error log. Just refer this and input this link: PHP File Handling

您可以使用正常的文件操作来创建错误日志。只需参考并输入此链接: PHP文件处理