Skip to content

Instantly share code, notes, and snippets.

@maxkostinevich
Last active August 29, 2015 14:27
Show Gist options
  • Save maxkostinevich/0fada15a013e9c0a6e98 to your computer and use it in GitHub Desktop.
Save maxkostinevich/0fada15a013e9c0a6e98 to your computer and use it in GitHub Desktop.
Logger for WP plugins
<?php
/**
* Simple logger for WordPress plugins
* Author: Max Kostinevich
* Author URI: https://maxkostinevich.com
* Version: 1.0.0
* License: MIT
* 2015 (c) Max Kostinevich
*/
/**
* ===== SIMPLE LOGGER FOR WORDPRESS PLUGINS =====
*/
/**
* This function allows you to write any messages to wpdev-log.txt file
*/
if(!function_exists('wpdevlogger_log')){
function wpdevlogger_log($msg){
// current file dir
$plugin_dir = plugin_dir_path( __FILE__ );
// open the log file
$log_file = fopen( $plugin_dir . 'wpdev-log.txt', 'a+' );
if( is_array($msg) || is_object($msg) ){
fwrite($log_file, '[' . date('M j, Y H:i:s'). '] ' . ':: START TRACE ::' . " \n");
fwrite($log_file, var_export($msg,true));
fwrite($log_file, "\n");
fwrite($log_file, '[' . date('M j, Y H:i:s'). '] ' . ':: END TRACE ::' . " \n");
}
else{
fwrite($log_file, '[' . date('M j, Y H:i:s'). '] ' . $msg . " \n");
}
fflush($log_file);
fclose($log_file);
}
}
/**
* This function will remove the log file from your plugin directory
* Call this function when you don't need logger anymore
*/
if(!function_exists('wpdevlogger_unlink_log_file')){
function wpdevlogger_unlink_log_file(){
// current file dir
$plugin_dir = plugin_dir_path( __FILE__ );
$log_file = $plugin_dir . 'wpdev-log.txt';
if(file_exists($log_file)){
unlink($log_file);
}
}
}
/*
* ===== END LOGGER =====
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment