【问题标题】:PHP ini_set not setting 'error_log' custom log file path correctlyPHP ini_set 未正确设置“error_log”自定义日志文件路径
【发布时间】:2017-06-23 08:47:14
【问题描述】:

所以我试图在我的初始化文件中设置一些代码,允许在我的本地主机/开发模式上显示错误,并且不允许在我的网络解决方案服务器/生产模式上显示错误。我已经删除了 if else 语句,因为我目前正在我的 localhost 服务器上测试它,所以我假设你也是。代码如下:

// 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 包含:

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

这是正确的文件路径,“OOP 登录系统”是“根”目录。 “OOP 登录系统”文件夹当前位于我的 Apache 服务器内的 htdocs 文件夹中。

现在我的应用程序抛出“未捕获的 PDOException”,这很棒!我要那个。但它没有做的是将该错误保存到我的 error.log 文件中。只是好奇如何去做。有什么理由不保存吗?我已经在 php 文件夹中创建了 error.log 文件,并尝试再次运行,但它仍然无法正常工作。

对使用 PHP 的 ini 非常陌生,因此任何帮助都会很棒。如果您回复我,请提前抱歉,我仍然不知道您在说什么。

【问题讨论】:

  • 你应该检查$errorPath(如果你没有调试器,只需var_dump($errorPath);它)。还有phpinfo() 可以查看实际的指令值。
  • @ÁlvaroGonzález 我在 $errorPath 上做了一个 var 转储,得到了以下结果。将其发布在此处的粘贴箱中:pastebin.com/GSxSQD7Q
  • 粘贴箱?为什么?它可能产生多少输出?另外我不知道你的文件系统是什么样子的。
  • vardump返回文件路径的字符串,是正确的。正确的文件路径,“OOP 登录系统”是“应用程序根”目录。 (/OOP 登录系统/logs/php/error.log)“OOP 登录系统”文件夹当前位于我的 Apache 服务器内的 htdocs 文件夹(服务器根目录)中。 var_dump() 给了我这个:string(36) "/OOP Login System/logs/php/error.log"
  • “OOP 登录系统”真的在两个位置,根目录和一些 htdocs 子目录吗?这是可能的(您可以在所有主要操作系统中创建链接),但我怀疑情况并非如此。

标签: php php-ini


【解决方案1】:

有点晚了,但是…… 我猜问题出在这一行:

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

这会产生这个无效的路径:

\/logs/php/error.log

你应该使用

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多