如何将CI REST API登录保存到文件TXT中



im newbee,并混淆了如何从logs 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="nn".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);

您应该使用Error Logging Threshold,编辑application/config.php

启用日志

$config['log_threshold'] = 1;

您可以存储debugg消息,信息消息和错误消息到日志文件

0 = Disables logging, Error logging TURNED OFF
1 = Error Messages (including PHP errors)
2 = Debug Messages
3 = Informational Messages
4 = All Messages

最新更新