Skip to content

Instantly share code, notes, and snippets.

@emptyhua
Last active December 14, 2015 08:08
Show Gist options
  • Save emptyhua/5055043 to your computer and use it in GitHub Desktop.
Save emptyhua/5055043 to your computer and use it in GitHub Desktop.
PHP日志打印
function debug_log() {
if (false) return; //开关
static $fp = 0;
if ($fp === 0) {
$logname = 'myprojectname';//日志名称
$fp = fopen('/tmp/' . $logname . '.debug.log', 'a');
}
$traces = debug_backtrace();
$trace = count($traces) > 1 ? $traces[1] : $traces[0];
$log_msg = date('Y-m-d H:i:s') . ' FILE:' . basename($trace['file']) . ' FUNC:' . $trace['function'] . ' LINE:' . $trace['line'] . ' :' . "\n";
foreach(func_get_args() as $arg) {
if (is_string($arg)) {
$log_msg .= $arg . ' ';
} else {
$log_msg .= var_export($arg, true) . ' ';
}
}
fwrite($fp, $log_msg . "\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment