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);
}
}