在PHP項(xiàng)目開(kāi)發(fā)過(guò)程中柠辞,我們可以自定義一個(gè)記錄錯(cuò)誤日志的函數(shù),方便我們進(jìn)行debug主胧,下面編寫(xiě)一個(gè)簡(jiǎn)單的記錄錯(cuò)誤日志的function叭首。
public static function errorLog($err_type, $method, $contents) {
$file = $err_type."_".date('Ymd')."log";
$contents = date('Y-m-s H:i:s')." {$method}"." {$contents}\n";
if (is_dir('/tmp/project/logs')) {
file_put_contents("/tmp/project/logs/{$file}", $contents, FILE_APPEND);
} else {
mkdir("/tmp/project/logs", 0777, true);
file_put_contents("/tmp/project/logs/{$file}", $contents, FILE_APPEND);
}
}
參數(shù)說(shuō)明:
$err_type:自定義的錯(cuò)誤類(lèi)型
$method:方法名,便于debug時(shí)能夠定位到具體的函數(shù)中
$contents:日志內(nèi)容