简体   繁体   中英

PHP ini_set not setting 'error_log' custom log file path correctly

So I'm trying to basically set up in my initialization file some code that allows error displaying on my localhost/development mode and does not allow error displaying on my network solutions server/production mode. Ive removed the if else statement as I'm currently testing this on my localhost server, so i'm assuming that you are as well. Here's the code:

// Set INI to log errors
ini_set('log_errors', 1);
// Set Error Log Path Variable (Grabs the Root Directory of the Project, then appends on the path)
$errorPath = dirname(substr(dirname(__FILE__),strlen($_SERVER['DOCUMENT_ROOT']))) . '/logs/php/error.log';
// Set INI for error_log path
ini_set('error_log', $errorPath);
// Give some code thats going to throw an error
$db = new PDO("mysql:host=a;dbname=test", "root", "");

$errorPath contains:

string(36) "/OOP Login System/logs/php/error.log"

That is the correct file path, with "OOP Login System" being the "root" directory. The "OOP Login System" folder is currently sitting in my htdocs folder within my Apache Server.

Now my app is throwing a "Uncaught PDOException" which is great! I want that. But what its not doing is saving that error into my error.log file. Just curious as to how to go about this. Any reason why its not saving? I have created the error.log file in the php folder, and tried to run again, but its still not working.

Very new to working with PHP's ini, so any help would be great. Sorry in advance if you reply and i still have no idea what you're saying.

A little late but… I guess the problem resides in this line:

$errorPath = dirname(substr(dirname(__FILE__),strlen($_SERVER['DOCUMENT_ROOT']))) . '/logs/php/error.log';

That produces this invalid path:

\/logs/php/error.log

You should use

$errorPath = basename(substr(dirname(__FILE__),strlen($_SERVER['DOCUMENT_ROOT']))) . '/logs/php/error.log';

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