/** * 仅用于日志调试 * * @param string|array $content 日志内容 * @param bool $json 是否采用JSON格式存储 * @param bool $append 是否采用追加模式记录日志 * @param string $filename 文件名(考虑到权限问题,统统压入/tmp目录) * * @return int */ if (!function_exists("debug")) { function debug($content, $json = true, $append = true, $filename = "debug", $base_dir = "/tmp/") { if (empty($filename) || empty($content)) return 0; $filename = str_replace("/", "", $filename); $filename = str_replace("\\", "", $filename); // if ("linux" == strtolower(PHP_OS)) { // $dir = !empty($base_dir) ? $base_dir : (env("CACHE_DIR") ? env("CACHE_DIR") . "/logs/" : "/tmp/"); // } else { // $dir = env("CACHE_DIR") ? CACHE_DIR : 'C:\\'; // } $dir = storage_path('logs/' . date('Ymd')); $rs = true; //if (!is_dir($dir)) { $rs = @mkdir($dir, 0755, true); //} if (empty($rs)) $dir = "/tmp/"; $filename = $dir . DIRECTORY_SEPARATOR . $filename . ".log"; if (!empty($json)) { $content = json_encode($content, JSON_UNESCAPED_UNICODE); } else { $content = var_export($content, true); } $client_ip = getClientIp(); $log_time = date('Y-m-d H:i:s', time()); $content = "【" . $log_time . " | {$client_ip}】" . $content . "\r\n"; if (empty($append)) { $rs = file_put_contents($filename, $content); } else { $rs = file_put_contents($filename, $content, FILE_APPEND); } return $rs; } }