Skip to content

Instantly share code, notes, and snippets.

@schurpf
Last active August 29, 2015 14:08
Show Gist options
  • Save schurpf/bb9522aaa3fe42fa5cd4 to your computer and use it in GitHub Desktop.
Save schurpf/bb9522aaa3fe42fa5cd4 to your computer and use it in GitHub Desktop.
php: custom file log
/**
* custom log in current directory
* @author schurpf
* @url http://schurpf.com
* @version 0.0
* @date 2014-10-28
* @dependency WP, needs plugin_dir_path
* @param string $log log message
* @param string $file filename
* @return bool return true on success
*/
if (! function_exists('sch_log')) {
function sch_log($log, $file_path ='', $file = 'log.txt'){
//set file path if not existing
if ($file_path == ''){
$file_path = dirname(__FILE__). '/' .$file;
}
//Process $log
if ( is_array($log)){
ob_start( );
print_r($log);
$log = ob_get_clean( );
}
//Add date and time
$log = date(DATE_RFC2822).PHP_EOL.$log;
// Open the file to get existing content
$current = $log;
if (file_exists($file_path)){
$current = file_get_contents($file_path);
// Append a new person to the file
$current = $log."\n".$current;
// Write the contents back to the file
}
file_put_contents($file_path, $current);
return TRUE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment