php - 如何将 CI REST API 日志保存到文件 txt
问题描述
我是新手,很困惑如何从日志 CI Rest API 中获取价值以保存到 Txt 文件中。这些库已保存到数据库中,但我无法获得创建变量并保存到文件中的值。抱歉英语不好,请帮忙。
这里的代码:
protected function _log_request($authorized = FALSE)
{
// Insert the request into the log table
$is_inserted = $this->rest->db->insert(
$this->config->item('rest_logs_table'), [
'uri' => $this->uri->uri_string(),
'method' => $this->request->method,
'params' => $this->_args ? ($this->config->item('rest_logs_json_params') === TRUE ? json_encode($this->_args) : serialize($this->_args)) : NULL,
'api_key' => isset($this->rest->key) ? $this->rest->key : '',
'ip_address' => $this->input->ip_address(),
'time' => time(),
'authorized' => $authorized
]);
//variable for saving value
$uri = $this->uri->uri_string();
$method = $this->request->method;
$params = $this->_args ? ($this->config->item('rest_logs_json_params') === TRUE ? json_encode($this->_args) : serialize($this->_args)) : NULL;
$api_key = isset($this->rest->key) ? $this->rest->key : '';
$ip_address = $this->input->ip_address();
$time = time();
//write into file
$logs = fopen("logs.txt","a");
fputs($logs, $uri. "\n");
fputs($logs, $method. "\n");
fputs($logs, $params. "\n");
fputs($logs, $api_key. "\n");
fputs($logs, $ip_address. "\n");
fputs($logs, $time. "\n");
fclose($logs);
// Get the last insert id to update at a later stage of the request
$this->_insert_id = $this->rest->db->insert_id();
return $is_inserted;
}
解决方案
尝试这个。
$message="\n\n".str_repeat("=", 100);
$message .= "uri =>".$uri."\n";
$message .= "method=>".$method."\n";
$message .= "params =>".$params."\n";
$message .= "api_key=>".$api_key."\n";
$message .= "ip_address =>".$ip_address."\n";
$message .= "time=>".$time."\n";
$filepath="application/logs/log-".date('Y-m-d').'.txt';
$fp = fopen($filepath, "a")
fwrite($fp, $message);
fclose($fp);