development

사람이 읽을 수있는 형식으로 다중 레벨 배열을 (로그에) 출력하는 방법은 무엇입니까?

big-blog 2020. 9. 20. 10:23
반응형

사람이 읽을 수있는 형식으로 다중 레벨 배열을 (로그에) 출력하는 방법은 무엇입니까?


저는 drupal 사이트에서 작업 중이며 디버깅 할 때 항상 길고 중첩 된 배열을 읽어야합니다. 결과적으로 내 인생의 많은 부분을 화살표, 리턴 및 탭 키를 사용하여 1000 개 이상의 문자열을 중첩되고 읽을 수있는 형식으로 분할합니다.

drupal 개발자의 경우 여러 단계의 # ahah / # ajax 양식으로 작업하기 때문에 devel의 dsm ()을 사용할 수 없으며 화면이 아닌 오류 로그에만 배열을 출력 할 수 있습니다.

시각적 예 :

악:

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'=> '', '통과'=> '',),
...

좋은:

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', 
        ), 

편집 : 죄송합니다. "화면에 출력하지 않음"은 클릭 가능한 중첩 형식 (devel.module 사용)으로 배열을 출력 할 수있는 drupal의 시스템 메시지를 통해 의미했습니다.


Apache 오류 로그에 오류를 기록해야하는 경우 다음을 시도 할 수 있습니다.

error_log( print_r($multidimensionalarray, TRUE) );

http://php.net/manual/en/function.print-r.php 이 함수는 출력 형식을 지정하는 데 사용할 수 있습니다.

$output = print_r($array,1);

$output문자열 변수이며 다른 모든 문자열처럼 기록 될 수 있습니다. 순수한 PHP에서는 다음을 사용할 수 있습니다.trigger_error

전의. trigger_error($output);

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

HTML로도 형식을 지정해야하는 경우 <pre>태그 를 사용할 수 있습니다.


간단한 것 :

사용 print_r, var_dump또는 var_export아주 잘 당신이하지 HTML 모드에서 또는 당신이 모든 것을 감싸는 경우 @Joel 라슨 말했듯이보기 소스 모드에서 결과를 보면 그것을해야 <pre>태그를.

print_r 가독성에 가장 좋지만 null / false 값을 인쇄하지 않습니다.

var_dump 값 및 길이 유형과 null / false 값을 확인하는 데 가장 적합합니다.

var_export에에 simmilar입니다 var_dump있지만 덤프 된 문자열을 가져올 수 있습니다.

이들 중 하나에 의해 반환 된 형식은 소스 코드에서 올바르게 들여 쓰기 var_export되며 덤프 된 문자열을 반환하는 데 사용할 수 있으므로 로깅에 사용할 수 있습니다.

고급 기능 :

PHP 용 xdebug 플러그인을 사용하면 var_dumps를 원시 덤프 형식이 아닌 HTML 형식의 문자열로 인쇄 하고 형식화에 사용할 사용자 정의 함수를 제공 할 수 있습니다.


Drupal의 Devel 모듈 에는 형식화 된 배열과 객체를 로그 파일에 인쇄 할 수있는 기능을 포함하여 다른 유용한 기능이 있습니다. 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.


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.


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.


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

참고URL : https://stackoverflow.com/questions/11884891/how-to-output-to-a-log-a-multi-level-array-in-a-format-that-is-human-readable

반응형