Skip to content

Instantly share code, notes, and snippets.

@robneu
Created September 29, 2016 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robneu/74cbb63e317b2e0a08a6aef54c118b74 to your computer and use it in GitHub Desktop.
Save robneu/74cbb63e317b2e0a08a6aef54c118b74 to your computer and use it in GitHub Desktop.
Write a debug log to the WP uploads directory. Use tail -f in a terminal to watch for changes on the fly
<?php
/**
* Write a debug log.
*
* @param mixed $msg A message to log.
*/
function sitecare_debug_log( $msg = null ) {
if ( ! is_string( $msg ) ) {
$msg = print_r( $msg, true );
}
$upload_dir = wp_upload_dir();
$log_dir = "{$upload_dir['basedir']}/logs/";
$log_file = "{$log_dir}sitecare-debug-log.txt";
if ( ! is_dir( $log_dir ) ) {
wp_mkdir_p( $log_dir );
}
$open_file = fopen( $log_file, 'a+' );
if ( false !== $open_file ) {
if ( null === $msg ) {
fwrite( $open_file, date( "\r\nY-m-d H:I:s:\r\n" ) );
} else {
fwrite( $open_file, date( 'Y-m-d H:i:s - ' ) . $msg . "\r\n" );
}
}
fclose( $open_file );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment