PHP 小方法之 写日志方法

if(! function_exists ('write_log') ) {
    function write_log($data, $name='debug', $date=null){
        if (is_array($data)) {
            $data = json_encode($data);
        }
        if (strpos($data, "\n")) {
            $data = str_replace("\n", '\n', $data);
        }
        $type = $type ? $type.'_' : '';
        if (!$date) {
            $date = 'today';
        }
        $date = date('Ymd', strtotime($date));

        $log_file = storage_path().DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.$name.'_'.$date.'.log';
        make_dir(dirname($log_file));

        $fp = fopen($log_file, "a+");
        flock($fp, LOCK_EX);
        fwrite($fp, "\n" . date("Y-m-d H:i:s") . ' [' . get_client_ip() . '] : ' . $data);
        flock($fp, LOCK_UN);
        fclose($fp);
    }
}

 

posted @ 2016-12-20 15:52  桔子木木  阅读(1295)  评论(0编辑  收藏  举报