본문 바로가기

WordPress/Debug Settings

WordPress Debug Message Settings for PHP

워드프레스에서 PHP 디버그  메시지 출력하기


원문 참조 : https://codex.wordpress.org/Debugging_in_WordPress


워드프레스 설치 폴더/wp-config.php 파일을 열고 아래의 항목을 추가/변경하면 된다

// Enable WP_DEBUG mode

define( 'WP_DEBUG', true );


// Enable Debug logging to the /wp-content/debug.log file

define( 'WP_DEBUG_LOG', true );


// Disable display of errors and warnings 

define( 'WP_DEBUG_DISPLAY', false );

@ini_set( 'display_errors', 0 );


// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)

define( 'SCRIPT_DEBUG', true );


위와같이 설정한 후, 프로그램이 실행 중에 오류를 발생하면 wp-content/debug.log 파일이 자동으로 생성되고 로그된다.

또한 개발자가 디버깅 메시지를 파일에 출력할 때 PHP 코드에서 error_log()를 사용할 경우에도 마찬가지로 debug.log 파일에 메시지가 출력된다

웹브라우저 화면에 로그가 출력되도록 하려면 WP_DEBUG_DISPLAY 상수를 true 로 설정하면 되지만 개발 중인 페이지 상단에 로그가 직접 출력된다


만약 별도의 로그파일에 디버그 메시지를 출력하고자 한다면, 다음과 같이 파일에 출력하는 일반적인 방법을 사용하면 된다

function log_d ( $tag, $msg ) {

   $fp = fopen ( 'C:/test/my-debug.log', 'a' );

   fwrite ( $fp, "$tag : $msg \r\n" );

   fclose ( $fp );

}