Skip to content

Instantly share code, notes, and snippets.

@lgedeon
Created February 7, 2018 18:28
Show Gist options
  • Save lgedeon/90de39995da54d06c395343f61a757e6 to your computer and use it in GitHub Desktop.
Save lgedeon/90de39995da54d06c395343f61a757e6 to your computer and use it in GitHub Desktop.
Self-contained log function.
<?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