如何正确地将变量打印到 PHP 错误日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19486547/
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
How to properly print variable to PHP error log
提问by user2354835
I'm trying to properly print a variable to the error log so that I know if my MySQL query worked, but it doesn't print anything to my errorlog.log
file. I set everything up below, I set errors to true and told it the file to print to but it doesn't print anything at all:
我正在尝试将一个变量正确打印到错误日志中,以便我知道我的 MySQL 查询是否有效,但它没有将任何内容打印到我的errorlog.log
文件中。我在下面设置了所有内容,将错误设置为 true 并告诉它要打印的文件,但它根本不打印任何内容:
<?php
error_reporting(E_ALL); //to set the level of errors to log, E_ALL sets all warning, info , error
ini_set("log_errors", true);
ini_set("error_log", "/errorlog.log"); //send error log to log file specified here.
include ("connection.php");
$city = $_POST['city'];
$state = $_POST['state'];
$results = array();
if( $query = $db->query("SELECT business_id, BusinessName, date, post".
"FROM WolfeboroC.posts".
"JOIN WolfeboroC.users ON users.recid = posts.business_id".
"WHERE city= '$city' && state='$state' ".
"ORDER BY date DESC LIMIT 0, 500") )
{
while($record = $query->fetch_assoc())
{
I defined $results
, here its a MySQL query that fetches a bunch of info from the database and returns it in $results:
我定义了$results
,这里是一个 MySQL 查询,它从数据库中获取一堆信息并在 $results 中返回它:
$results[] = $record;
}
$query->close();
}
echo json_encode($results);
This is where I try to print the variable to the error log file:
这是我尝试将变量打印到错误日志文件的地方:
error_log(print_r($results));
?>
回答by Ofir Baruch
print_r
(php manual) will print the array and won't return a value,
so basically you're using it wrong.
print_r
( php manual) 将打印数组并且不会返回值,所以基本上你使用它是错误的。
The right way is using the second parameter of the function which is boolean that determines if the function will print the output or will return it.
正确的方法是使用函数的第二个参数,它是布尔值,决定函数是打印输出还是返回它。
error_log(print_r($results,true));
EDITIf your server has DirectAdmin or CPanel, there's a built-in option to see the Apache error logs. Check if your custom error appears there. If so, there's a problem with the errorlogs file.
编辑如果您的服务器有 DirectAdmin 或 CPanel,则有一个内置选项可以查看 Apache 错误日志。检查您的自定义错误是否出现在那里。如果是这样,则错误日志文件存在问题。