简体   繁体   中英

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.

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.

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?

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_set("log_errors", 1);

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

error_log( "one error" );

  • 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.

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 .

If you were practicing Object Oriented Programming with try/catch Exceptions you could use some logging method to separate out error into individual files.

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). 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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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