Last active
March 23, 2023 12:12
-
-
Save kostiantyn-petlia/94368e72e2e87077d127a5677ba213c7 to your computer and use it in GitHub Desktop.
Logging function write_log() for debugging process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ----------------------------------------------------------------------------- | |
/** | |
* Logging to debug.log in DEBUG_LOG_DIR dir (default is the root site dir) | |
* | |
* Note: In the wp-config.php: | |
* | |
* you need define: | |
* 0) define( 'DEBUG_LOG', true ) | |
* | |
* you can define: | |
* 1) define( 'DEBUG_LOG_DIR', dirname( __FILE__ ) . '/' ); | |
* 2) define( 'DEBUG_LOG_INVERTED', false); | |
* 3) define( 'DEBUG_BACKTRACE', true); | |
* | |
* Author K | |
* Version 2.2.0.0 | |
*/ | |
if ( ! function_exists( 'write_log' ) ) { | |
// const | |
if ( ! defined( 'DEBUG_LOG' ) ) { | |
define( 'DEBUG_LOG', true ); | |
} | |
if ( ! defined( 'DEBUG_LOG_INVERTED' ) ) { | |
define( 'DEBUG_LOG_INVERTED', true ); | |
} | |
if ( ! defined( 'DEBUG_BACKTRACE' ) ) { | |
define( 'DEBUG_BACKTRACE', true ); | |
} | |
// function | |
function write_log( $log = null, $before = '', $after = '', $clear = false ) { | |
static $is_first_run = true; | |
static $backtrace_last = null; | |
if ( defined( 'DEBUG_LOG' ) && true === DEBUG_LOG && ( defined( 'DEBUG_LOG_DIR' ) || defined( 'ABSPATH' ) ) ) { | |
// Smart formatting by the delta time in seconds | |
//$time_new = microtime( true ); | |
//$time_delta = round( microtime( true ) - $time_new, 3 ) * 1000; | |
//$new_pass_separator = ( $time_delta > 3 ) ? ( str_repeat( '-', 80 ) . PHP_EOL ) : ''; | |
//$time = $time_new; | |
// Destination File | |
$debug_log_file = ( ( defined( 'DEBUG_LOG_DIR' ) ) ? DEBUG_LOG_DIR : ABSPATH ) . 'debug.log'; | |
// Clear debug.log if 'true' or the file is more than 10Mb (10485760 bytes) | |
if ( true === $clear || filesize( $debug_log_file ) > 10485760 || ! file_exists( $debug_log_file ) ) { | |
file_put_contents( $debug_log_file, '' ); | |
} | |
if ( is_writable( $debug_log_file ) ) { | |
// CONTENT | |
$content = ''; | |
$date = date( 'Y-m-d H:i:s' ); | |
// Only string can be in the wrapper role | |
$before = is_string( $before ) ? $before : ''; | |
$after = is_string( $after ) ? $after : ''; | |
// SEPARATOR | |
// Build content ('-' & 'hr' define a separate line before the content) | |
$hr_normal = ( str_repeat( '-', 29 ) . '[' . $date . ']' . str_repeat( '-', 30 ) ) . PHP_EOL; | |
$hr_first_time = ( str_repeat( '=', 29 ) . '[' . $date . ']' . str_repeat( '=', 30 ) ) . PHP_EOL; | |
$hr = in_array( $clear, [ '-', 'hr' ], true ) ? $hr_normal : ''; | |
$hr = $is_first_run ? $hr_first_time : $hr; | |
// BACKTRACE | |
// Debug context info. Help: http://php.net/manual/en/function.debug-backtrace.php | |
$info = ''; | |
if ( defined( 'DEBUG_BACKTRACE' ) && true === DEBUG_BACKTRACE ) { | |
$backtrace = debug_backtrace(); | |
$backtrace_current = $backtrace[1]; | |
// Separate line only for new context | |
if ( $backtrace_last !== $backtrace_current ) { | |
$info = ' Context: '; | |
$info .= ! empty( $backtrace_current['class'] ) ? $backtrace_current['class'] : ''; | |
$info .= ! empty( $backtrace_current['object'] ) ? '[' . $backtrace_current['object'] . ']' : ''; | |
$info .= ! empty( $backtrace_current['type'] ) ? $backtrace_current['type'] : ''; | |
$info .= ! empty( $backtrace_current['function'] ) ? $backtrace_current['function'] : ''; | |
$line = ! empty( $backtrace_current['line'] ) ? $backtrace_current['line'] : ''; | |
$info .= ! empty( $backtrace_current['file'] ) ? ' in ' . pathinfo( $backtrace_current['file'], PATHINFO_BASENAME ) . '[' . $line . ']' . ' in ' . pathinfo( $backtrace_current['file'], PATHINFO_DIRNAME ) . '\\' : ''; | |
$info .= PHP_EOL; | |
$hr = $is_first_run ? $hr_first_time : $hr_normal; | |
} | |
$backtrace_last = $backtrace_current; | |
} | |
$content .= $before . var_export( $log, true ) . $after . PHP_EOL; | |
// Write log | |
if ( defined( 'DEBUG_LOG_INVERTED' ) && false === DEBUG_LOG_INVERTED ) { | |
$content = $hr . $info . '↓ ' . $content; | |
file_put_contents( $debug_log_file, $content, FILE_APPEND ); // was (temp): error_log( $content, 3, $debug_log_file ); | |
} else { | |
$content = '↑ ' . $content . $info . $hr . file_get_contents( $debug_log_file ); | |
file_put_contents( $debug_log_file, $content ); | |
} | |
$is_first_run = false; | |
} | |
} | |
} | |
} | |
// ----------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment