Skip to content

Instantly share code, notes, and snippets.

@lloydzhou
Last active January 29, 2016 05:20
Show Gist options
  • Save lloydzhou/6a4e5f549905c4fa5e59 to your computer and use it in GitHub Desktop.
Save lloydzhou/6a4e5f549905c4fa5e59 to your computer and use it in GitHub Desktop.
<?php
function logger($path = 'php.log', $cond = true) {
$logs = array();
register_shutdown_function(function() use ($path, &$logs){
return count($logs) > 0 ? @file_put_contents($path, implode(array_map(function($log){
return count($log) > 1 ? call_user_func_array('sprintf', $log) : current($log);
}, $logs), PHP_EOL). PHP_EOL, FILE_APPEND | LOCK_EX) : false;
});
$cond = is_callable($cond) ? call_user_func($cond) : !!($cond);
return function () use ($cond, &$logs) {
return $cond && func_num_args() > 0 ? $logs[] = func_get_args() : false;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment