PHP日志记录函数



function logs($message,$data,$file_name = '',$path = 'work/'){
    if(is_array($data)){
        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
    }elseif(is_object($data)) {
        $data = print_r($data,true);
    }else{
        $type = gettype($data);
        if ($type != 'string') {
            $data = var_export($data,true);
        }
    }
    
    
    

    if ($file_name == '') {
        $file_name = date('Y-m-d').'.log';
    }else{
        $date = date('Y-m-d');
        $file_name = "{$file_name}_{$date}.log";
    }

    $path = app()->getRuntimePath().$path;//tp6
    //$path = RUNTIME_PATH.$path;//tp5

    if (!is_dir($path)) {
        mkdir($path);
    }

    $date = date('Y-m-d H:i:s');
    $file_name = $path.$file_name;
    $text = $date.' '.$message . ' -> '.$data.PHP_EOL;
    file_put_contents($file_name,$text,FILE_APPEND);
}

仅有一条评论

添加新评论