Skip to content

Instantly share code, notes, and snippets.

@derpixler
Last active April 11, 2016 04:40
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 derpixler/bc3dddfb5872ac465c851049e12b5cb3 to your computer and use it in GitHub Desktop.
Save derpixler/bc3dddfb5872ac465c851049e12b5cb3 to your computer and use it in GitHub Desktop.
Simple WordPress Logger
<?php
add_filter( 'logfile_path', function(){ return __DIR__ . '/log/'; } );
add_filter( 'logfile_name', function(){ return 'app.log'; } );
wp_logger( __LINE__, 'Log msg' );
/**
* log easy a msg
*
* @param int $line the linenumer
* @param mixed $log_content
*
* @return void
*/
function wp_logger( $line, $log_content = FALSE ) {
if ( $log_content != FALSE ) {
date_default_timezone_set( 'UTC' );
// $filename should be the path to a file in the upload directory.
$kw = date('W', time());
$log_path = apply_filters( 'logfile_path', WP_CONTENT_DIR . '/uploads/logs/' . $kw . '/' );
$log_file = $log_path . apply_filters( 'logfile_name', date('Y-d-m', time()) . '.log' );
if ( ! file_exists( $log_path ) ) {
mkdir( $log_path, 0777, TRUE );
}
#$file = file_get_contents( $log_file );
$response = date( '[d-m-Y H:i ', time() ) . '- LINE ' . $line . '] ~ $ ' . $log_content . "\n";
file_put_contents( $log_file, $response, FILE_APPEND | LOCK_EX );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment