PHP列印log日誌檔案,儲存資料到指定檔案中
阿新 • • 發佈:2018-12-07
在專案開發中,作為一個後端開發人員肯定是要經常觀察日誌等記錄來查詢開發或上線的各種bug
分享一個列印日誌記錄的方法
/** * [errorLog 列印錯誤日誌記錄] * @param [type] $message [列印日誌記錄] * @param [type] $file [日誌檔名] * @return [type] [description] */ function errorLog($message,$file) { //將日誌檔案放在根目錄下/log/日期的資料夾名 $log_dir=$_SERVER['DOCUMENT_ROOT']."/log/".date('Ymd')."/"; //判斷是否存在資料夾,沒有則建立 if(!is_dir($log_dir)){ @mkdir($log_dir,0777,true); } //將錯誤日誌記錄寫入檔案中 $file=$log_dir.$file; if(is_array($message)){ $arr=explode(".",$file); if($arr[1]=='php'){ error_log("<?php \n return ".var_export($message, true)."\n", 3,$file); }else{ error_log(var_export($message, true)."\n", 3,$file); } }else{ error_log($message."\n\n", 3,$file); } }
用此方法可以將日誌資訊寫入到自己定義的檔案中,方便檢視和解決bug問題!