class="java" name="code">
class DEBUG{
const LOGPATH = "..".DIRECTORY_SEPARATOR."log";
// CLOSE 不显示 DEBUG INFO ERROR
const LEVEL="DEBUG";
// kay 设置的level级别 , val 要显示打印的级别
private static $_SHOWLEVELAS = [
'DEBUG' => ['DEBUG','ERROR'],
'INFO' => ['INFO','DEBUG','ERROR'],
'ERROR' => ['ERROR'],
'CLOSE' => []
];
// error_reporting(0); 0
const PHP_ERROR = 0 ;
public static function getShowLevel($level){
return isset(SELF::$_SHOWLEVELAS[$level]) ? SELF::$_SHOWLEVELAS[$level] : [];
}
/**
* @param $message
* @param string $source
* @param string $line
*
* @for example log_trace( $result_code, __FILE__, __LINE__);
*/
function log_trace($message,$level="DEBUG",$source='',$line=''){
$show_levels = DEBUG::getShowLevel(DEBUG::LEVEL);
if( in_array($level,$show_levels) ) {
$file = DEBUG::LOGPATH . DIRECTORY_SEPARATOR . date("YmdH") . ".log";
if(!file_exists(DEBUG::LOGPATH)){
@mkdir(DEBUG::LOGPATH);
}
if($source!='') {
$mess_string = date("Ymd H:i:s")." 源代码:".$source." ";
if($line!=''){
$mess_string .= "行号:".$line.PHP_EOL;
}
$mess_string .= "$level : ".var_export($message, true) . PHP_EOL;
} else{
$mess_string = date("Ymd H:i:s")." ".$level.": ".var_export($message, true) . PHP_EOL;
}
file_put_contents($file, $mess_string, FILE_APPEND);
}
}
}
调用:
log_trace(['LOG $send_data:'=>$send_data],"INFO", __FILE__, __LINE__);