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

时间:2019-02-28 06:07:50

标签: php rest api continuous-integration

im newbee并混淆如何从日志CI Rest API中获取价值以保存到Txt文件中。这些库已保存到db,但是我无法获得创建变量并将其保存到文件的价值。对不起,英语不好,请帮忙。

此处是代码:

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

2 个答案:

答案 0 :(得分:0)

尝试一下。

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

答案 1 :(得分:0)

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

启用日志

$config['log_threshold'] = 1;

您可以将调试消息,信息消息和错误消息存储到日志文件中

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