118

I'm working on a drupal site and when debugging, I am always having to read through long, nested arrays. As a result, a large portion of my life is spent using the arrow, return, and tab keys, to split up 1000+ character strings into a nested, readable format.

For drupal devs, I can't use devel's dsm(), as I'm working with multi-step #ahah/#ajax forms, and I can only output the arrays to the error log, not to the screen.

Visual example:

Evil:

array ( 'form_wrapper' => array ( '#tree' => true, '#type' => 'fieldset', '#prefix' => '', '#suffix' => '', '#value' => '', 'name' => array ( '#type' => 'textfield', '#title' => NULL, '#size' => 60, '#maxlength' => 60, '#required' => false, '#description' => NULL, '#attributes' => array ( 'placeholder' => 'Email', ), '#post' => array ( 'form_wrapper' => array ( 'name' => '', 'pass' => '', ),
...

Good:

array ( 
'form_wrapper' => array ( 
    '#tree' => true, 
    '#type' => 'fieldset', 
    '#prefix' => '<div>', 
    '#suffix' => '</div>', 
    '#value' => '', 
    'name' => array ( 
        '#type' => 'textfield', 
        '#title' => NULL, 
        '#size' => 60, 
        '#maxlength' => 60, 
        '#required' => false, 
        '#description' => NULL, 
        '#attributes' => array ( 
            'placeholder' => 'Email', 
        ), 

Edit: Sorry, by "not output to screen", I meant via drupal's system messages where it's possible to output arrays in a clickable, nested format (using devel.module).

Mark Shiraldi
  • 2,401
  • 3
  • 15
  • 9

8 Answers8

256

If you need to log an error to Apache error log you can try this:

error_log( print_r($multidimensionalarray, true) );

If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to true, print_r() will return the information rather than print it.

Avatar
  • 14,622
  • 9
  • 119
  • 198
Akhilraj N S
  • 9,049
  • 5
  • 36
  • 42
  • 3
    Looks like it's `print_r` (lowercase). Does `print_R` really work as well? – evanrmurphy Jul 29 '16 at 21:13
  • thanks @AkhilrajNS can you tell me more about how can i send the inserted query or any query which is executed above this log message.? – always-a-learner Jan 19 '17 at 03:52
  • @ankitsuthar Did you mean SQL Query ? – Akhilraj N S Feb 10 '17 at 05:27
  • Yes But I got it by the last query function in CI. Actually, I want to log a data which are get inserted or edited, deleted. – always-a-learner Feb 10 '17 at 05:56
  • This is dum. This outputs newline characters as literal `\n` instead of actual newlines. – Otheus Oct 24 '18 at 10:09
  • @Otheus I think may have missed the point in the first sentence. This targets the APACHE ERROR LOG which is a single entry per line. If you want to capture multi-line info it needs to be escaped. – oligofren Jan 07 '22 at 14:46
  • @oligofren perhaps I missed Akhillaraj's point, but the OP asked for something _human readable_. Apparently this is an apache "security" fix: https://stackoverflow.com/a/1574558/3849157. But the idea of `print_r` is to output the array in a human-readable way and apache kills that. His is the top answer, mysteriously. Well, maybe things changed since 2013. Or maybe he uses php-fpm or CGI. – Otheus Feb 23 '22 at 11:54
23

http://php.net/manual/en/function.print-r.php This function can be used to format output,

$output = print_r($array,1);

$output is a string variable, it can be logged like every other string. In pure php you can use trigger_error

Ex. trigger_error($output);

http://php.net/manual/en/function.trigger-error.php

if you need to format it also in html, you can use <pre> tag

Fivell
  • 11,829
  • 3
  • 61
  • 99
  • 1
    Read the question - OP needs to do this for log output - not screen output. – Matt Aug 09 '12 at 13:55
  • @Matt read the answer, `if you need to format it also in html` – code-jaff Aug 09 '12 at 14:08
  • @Fivell, I try. If you clarify your answer to explain that the output can be sent to the log file, I'll remove my -1. – Matt Aug 09 '12 at 14:11
  • 1
    @Fivell there's a small issue with `trigger_error` it limits messages to max 1024 length or something similar. Making some longer `var_exports`/`print_r` strings get cut-off. It's useful for simple structures. – Mihai Stancu Aug 09 '12 at 14:19
12

I just wonder why nobody uses or recommends the way I prefer to debug an array:

error_log(json_encode($array));

Next to my browser I tail my server log in the console eg.

tail -f /var/log/apache2/error.log

Though it's debatable if the output is human-readable, but it's still my preferred way to read it and would look something like that:

[Tue Dec 13] [...] AH01071: Got error 'PHP message: {"form_wrapper":{"#tree":true,
"#type":"fieldset","#prefix":"","#suffix":"","#value":"","name":{"#type":
"textfield","#title":null,"#size":60,"#maxlength":60,"#required":false,
"#description":null,"#attributes":{"placeholder":"Email"},"#post":{
`"form_wrapper":{"name":"","pass":""}}}}}', referer: http://localhost/
wittich
  • 2,079
  • 2
  • 27
  • 50
  • 2
    Because it's not "human readable" :D Seriously, this is a good idea though. A post-processor combined with `jq` can make this an excellent debugging tool – Otheus Feb 23 '22 at 11:55
11

Simple stuff:

Using print_r, var_dump or var_export should do it pretty nicely if you look at the result in view-source mode not in HTML mode or as @Joel Larson said if you wrap everything in a <pre> tag.

print_r is best for readability but it doesn't print null/false values.

var_dump is best for checking types of values and lengths and null/false values.

var_export is simmilar to var_dump but it can be used to get the dumped string.

The format returned by any of these is indented correctly in the source code and var_export can be used for logging since it can be used to return the dumped string.

Advanced stuff:

Use the xdebug plug-in for PHP this prints var_dumps as HTML formatted strings not as raw dump format and also allows you to supply a custom function you want to use for formatting.

Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
2

Drupal's Devel module has other useful functions including ones that can print formatted arrays and objects to log files. See the guide at http://ratatosk.net/drupal/tutorials/debugging-drupal.html

dd()

Logs any variable to a file named “drupal_debug.txt” in the site’s temp directory. All output from this function is appended to the log file, making it easy to see how the contents of a variable change as you modify your code.

If you’re using Mac OS X you can use the Logging Console to monitor the contents of the log file.

If you’re using a flavor of Linux you can use the command “tail -f drupal_debug.txt” to watch the data being logged to the file.

nmc
  • 8,724
  • 5
  • 36
  • 68
1

This will help you

echo '<pre>';

$output = print_r($array,1);

echo '</pre>';

EDIT

using echo '<pre>'; is useless, but var_export($var); will do the thing which you are expecting.

Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
code-jaff
  • 9,230
  • 4
  • 35
  • 56
  • 1
    @Matt When this parameter is set to TRUE, print_r() will return the information rather than print it. – code-jaff Aug 09 '12 at 14:01
1

Syntax

print_r(variable, return);

variable Required. Specifies the variable to return information about

return Optional. When set to true, this function will return the information (not print it). Default is false

Example

error_log( print_r(<array Variable>, TRUE) );
Nikunj K.
  • 8,779
  • 4
  • 43
  • 53
0

You should be able to use a var_dump() within a pre tag. Otherwise you could look into using a library like dump_r.php: https://github.com/leeoniya/dump_r.php

My solution is incorrect. OP was looking for a solution formatted with spaces to store in a log file.

A solution might be to use output buffering with var_dump, then str_replace() all the tabs with spaces to format it in the log file.

Joel Larson
  • 3,064
  • 1
  • 15
  • 12
  • Read the question - OP needs to do this for log output - not screen output. – Matt Aug 09 '12 at 13:55
  • I did read the question. Missed that last little comment. Thanks for letting me know. This solution is not going to work then. – Joel Larson Aug 09 '12 at 13:59