PHP code example of whereof / think-http-logger

1. Go to this page and download the library: Download whereof/think-http-logger library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

whereof / think-http-logger example snippets


return [
    /*
   * The log profile which determines whether a request should be logged.
   * It should implement `LogProfile`.
   */
    'log_profile' => \tp5er\think\HttpLogger\LogProfile\LogNonGetRequests::class,

    /*
     * The log writer used to write the request to a log.
     * It should implement `LogWriter`.
     */
    'log_writer'  => \tp5er\think\HttpLogger\LogWriter\DefaultLogWriter::class,

    /*
    * The log channel used to write the request.
    */
    'log_channel' => env('LOG_CHANNEL', 'file')
];
~~~



## 控制器中使用

~~~
use tp5er\think\HttpLogger\Middlewares\HttpLogger;
class Index extends BaseController
{
    protected $middleware =[
        HttpLogger::class
    ];
    public function index()
    {
        return 'test HttpLogger';
    }

// 全局中间件定义文件
return [
		......
    \tp5er\think\HttpLogger\Middlewares\HttpLogger::class
];
~~~

日志记录
两个类用于处理传入请求的日志记录:`LogProfile` 类将确定是否应记录请求,`LogWriter` 类将请求写入日志。

在这个包中添加了一个默认的日志实现。 它只会记录 `POST`、`PUT`、`PATCH` 和 `DELETE` 请求,并将写入默认的 thinkphp 记录器。

您可以自由实现自己的日志配置文件和/或日志编写器类,并在` config/http-logger.php` 中进行配置。

自定义日志配置文件必须实现` \tp5er\think\HttpLogger\LogProfile`。 这个接口需要你实现 shouldLogRequest。

~~~
// Example implementation from `tp5er\think\HttpLogger\LogNonGetRequests`

public function shouldLogRequest(Request $request): bool
{
   return in_array(strtolower($request->method()), ['post', 'put', 'patch', 'delete']);