Created
February 7, 2018 18:28
-
-
Save lgedeon/90de39995da54d06c395343f61a757e6 to your computer and use it in GitHub Desktop.
Self-contained log function.
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
<?php | |
/** | |
* Log messages, timestamps and values throughout a page load and return them at the end for output at the bottom of | |
* the page or for storage with a single database touch. | |
* | |
* @param string $message Log message. | |
* @param mixed $value Log value. | |
* @param string $format Optional. Return type. Either text or array. Default array. | |
* | |
* @return array|string | |
*/ | |
function log( $message, $value, $format = 'array' ) { | |
static $log = []; | |
$log[] = [ | |
'message' => $message, | |
'value' => $value, | |
'time' => current_time( 'mysql' ), | |
]; | |
if ( 'text' === $format ) { | |
$return = ''; | |
foreach ( $log as $line ) { | |
$return .= printf( "[%s] %s: %s \n", esc_html( $line['time'] ), esc_html( $line['message'] ), esc_html( $line['value'] ) ); | |
} | |
return $return; | |
} | |
return $log; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment