简体   繁体   English

PHP错误日志-php.ini的PHP错误的唯一文件

[英]PHP Error Log - php.ini Unique file for php errors

I'm currently working on a project and to give a helping hand, I've changed my php.ini file to log errors to "php_errors.log" inside the directory of where a PHP file has returned an error. 我目前正在研究一个项目,并且要提供帮助,我已将php.ini文件更改为将错误记录到PHP文件已返回错误的目录内的“ php_errors.log”中。

I was wondering, after searching online (maybe I'm not wording this correctly), is there any way to set a unique file name in the php.ini file for the different errors. 我想知道,在在线搜索后(也许我的措词不正确),是否可以在php.ini文件中为不同的错误设置唯一的文件名。

For example, if an error occurred on let's say account.php, is there any way to log an error file "account_php_errors.log" through the ini file? 例如,如果在account.php上发生错误,是否有任何方法可以通过ini文件记录错误文件“ account_php_errors.log”?

From the ini file you can set a global error file, if you want to override that you can add the following lines to the "account.php" 您可以从ini文件中设置一个全局错误文件,如果要覆盖它,可以将以下行添加到“ account.php”中

ini_set("log_errors", 1); ini_set(“ log_errors”,1);

ini_set("error_log", "/path/to/logfile.log"); ini_set(“ error_log”,“ /path/to/logfile.log”);

error_log( "one error" ); error_log(“一个错误”);

  • Or if you use a framework you could check the documentation to see how you can customize errors. 或者,如果您使用框架,则可以查看文档以了解如何自定义错误。 Frameworks will have advanced logging mechanisms. 框架将具有高级的日志记录机制。

There is no way for you to use php.ini to log to multiple files based on the php file that generated the fatal error. 您无法使用php.ini根据生成致命错误的php文件登录到多个文件。

You could try and use register_shutdown_function to catch fatal errors then log to individual files using the ['file'] you get in the array response to a get_last_error . 您可以尝试使用register_shutdown_function来捕获致命错误,然后使用在get_last_error数组响应中获得的['file']登录到单个文件。

If you were practicing Object Oriented Programming with try/catch Exceptions you could use some logging method to separate out error into individual files. 如果您正在尝试使用try / catch异常进行面向对象的编程,则可以使用某些日志记录方法将错误分成单独的文件。

Options inside the php.ini file itself are not this flexible (which is actually a good thing so that other programs such as logrotate can be applied in an effective manner). php.ini文件本身的选项不是那么灵活(实际上是一件好事,因此可以有效地应用其他程序(例如logrotate))。 You can look at accomplishing what you need via your application (whether that is a framework or purely custom code). 您可以查看通过应用程序完成所需的内容(无论是框架还是纯自定义代码)。 You could just grep out what you need if you're on a linux system or search in the Event Viewer on Windows. 如果您使用的是Linux系统或在Windows的“事件查看器”中进行搜索,则可以仅列出所需的内容。 It really depends on what your specific needs really are. 这实际上取决于您的具体需求。

You can create a custom error handler within the application. 您可以在应用程序中创建自定义错误处理程序。 You should be able to get all the needed details from here 您应该可以从这里获取所有所需的详细信息

// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline) {
 // file_put_contents
}

set_error_handler("myErrorHandler");

More info: http://php.net/manual/en/function.set-error-handler.php 更多信息: http : //php.net/manual/en/function.set-error-handler.php

I would rethink the purpose for keeping errors logs in different directories, because it will make Your app messy. 我会重新考虑将错误日志保留在不同目录中的目的,因为这会使您的应用程序混乱。 Greping logs from different folders alone is not so convienient and if You ever want to add log monitoring tools like Logstash with elasticsearch, you will be forced to do a more complex setup 仅从不同文件夹添加日志并不是那么方便,并且如果您想通过elasticsearch添加Logstash这样的日志监视工具,则将被迫进行更复杂的设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM