Skip to content

Instantly share code, notes, and snippets.

@bgrgicak
Created November 18, 2022 13:10
Show Gist options
  • Save bgrgicak/39231667e89c9ab4e2b757f4df96a0fe to your computer and use it in GitHub Desktop.
Save bgrgicak/39231667e89c9ab4e2b757f4df96a0fe to your computer and use it in GitHub Desktop.
PHP pretty print objects and arrays in a log file
function debug( $message, $label = '', $log_file = 'debug.log' ) {
if ( is_bool( $message ) ) {
$message = $message ? 'true' : 'false';
} else if ( is_array( $message ) || is_object( $message ) ) {
$message = json_encode(
$message,
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
);
}
if ( ! empty( $label ) ) {
$label = ' - ' . $label;
}
error_log(
date( 'Y-m-d H:i:s' ) . $label . ': ' . $message ."\n",
3,
$_SERVER['DOCUMENT_ROOT'] . $log_file
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment