I am trying to debug a plugin and am using error_log()
in various places to see what various things are. I am using the following:
error_log( print_r( $variable ) );
When I look at debug.log
, all I see is 1
and the actual output of the error_log()
function is sent to the browser instead.
I know another plugin is not doing this as all are disabled with the exception of the one I am writing. In my wp-config.php
, I have defined WP_DEBUG_DISPLAY
as false
.
The same thing happens if I use var_dump()
and var_export()
. Other functions like gettype()
work just fine.
Is there any way to get the output into debug.log
?
I am trying to debug a plugin and am using error_log()
in various places to see what various things are. I am using the following:
error_log( print_r( $variable ) );
When I look at debug.log
, all I see is 1
and the actual output of the error_log()
function is sent to the browser instead.
I know another plugin is not doing this as all are disabled with the exception of the one I am writing. In my wp-config.php
, I have defined WP_DEBUG_DISPLAY
as false
.
The same thing happens if I use var_dump()
and var_export()
. Other functions like gettype()
work just fine.
Is there any way to get the output into debug.log
?
3 Answers
Reset to default 22The print_r function accept second parameter for return
so it retrun the variable instead of printing it.
print_r($expression, $return)
So you can do
error_log( print_r( $variable, true ) );
If your variable is just a string, then you can simply use error_log ( $variable )
. If it's an object or an array, other than using print_r()
, you can serialize it too:
error_log ( serialize ( $variable ) );
While the accepted answer is correct, I wanted to include an answer that expands the explanation for clarity.
print_r()
accepts two arguments (see the documentation for this function), the second of which is optional and you have omitted. The first argument is the expression to be printed. The second specifies whether the value is returned or printed.
When the second argument is omitted, its default value is false
and therefore it prints the result immediately. If this argument is false, print_r()
will return true
(which is displayed as "1" in the error log).
$result = print_r( $variable );
The returned result here is true
(displayed as "1").
You're passing the returned result of print_r()
to error_log()
. For the reason described above, this result is "1" and that is why you're getting 1 as the entry in the log.
In order to get a result to contain the expression in $variable
and pass it to the error_log()
function, you need print_r()
to return the expression as the result. You can do that by specifying the second argument as TRUE.
$result = print_r( $variable, true );
Now the returned result of print_r()
is the expression contained in $variable
and you can pass it to error_log()
:
$result = print_r( $variable, true );
error_log( $result );
OR:
error_log( print_r( $variable, true ) );
If you want to take this a step further, you could write a utility function for writing to the log that would handle determining if $variable
was an array or an object and thus required print_r()
, or if it could simply be passed as a string. This makes it easy to handle debug logging since you don't have to be concerned with the variable type in order to pass it for logging. I have written such a utility along with a detailed explanation here.
Side note on the PHP functions you mentioned: No matter how experienced you are, it's always helpful to review the documentation of the functions you are using in order to make sure you know what they actually do. I have linked to the print_r()
documentation above, which describes exactly what was talked about in both this and the accepted answer. But also mentioned were some other PHP functions that worked or didn't work. Let's consider those:
var_dump()
: The documentation indicates that this function does not return a result. In order to pass something to error_log()
for including in the error log, it needs to be an expression contained in a variable. This function does not return anything, it only outputs to the screen. So you can't use it to pass anything to error_log()
.
var_export()
: Similar to print_r()
in that you have a second, optional argument to either return a result or print to the screen. Define the second argument as true
to be able to return a result for error_log()
.
gettype()
always returns the type of the variable. So you have something you can pass to error_log()
by default. That is why it worked.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744782923a4593448.html
评论列表(0条)