Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output (to a log) a multi-level array in a format that is human-readable?

Tags:

php

drupal

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).

like image 991
Mark Shiraldi Avatar asked Aug 09 '12 13:08

Mark Shiraldi


People also ask

Which function is used to return an array in human readable form?

print_r() displays information about a variable in a way that's readable by humans. print_r(), var_dump() and var_export() will also show protected and private properties of objects.

Which method is used to print the contents of arrays and objects in a human readable form?

print_r displays human readable information about the values with a format presenting keys and elements for arrays and objects. The most important thing to notice is var_dump will output type as well as values while print_r does not.

What makes a log message human-readable?

What makes a Log Message Human-Readable? A log message is human-readable in the definition of this article if the contained information can be grasped completely at a glance. We don’t want to look at a log message and first have to figure out what information it actually contains. Let’s consider this log excerpt:

How to configure Logback with the desired logging format?

In between the messages we change the thread name to test the log output with thread names of different lengths. Now that we can create log output with all the information we need, we can configure logback with the desired logging format. The configuration is located in the file logback.xml:

What are logging levels and how do I use them?

Briefly, logging levels are labels. You use them to categorize your log entries by urgency, or severity. By applying logging levels, you gain the ability to easily search and filter your log entries by their levels. Levels also help you control the granularity of your log entries—more on that later.

Should your log files be machine-readable?

You might argue that a log file should be machine-readable. I whole-heartedly agree with that. Having machine-readable logs enables you to use tools that can automatically parse, process, and analyze them. As a result, you can gain valuable insights that would otherwise be lost to you. But your log files need to be easily readable to people, too.


1 Answers

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

error_log( print_r($multidimensionalarray, TRUE) ); 
like image 117
Akhilraj N S Avatar answered Sep 25 '22 23:09

Akhilraj N S