PHP code example of xxxcoltxxx / request-logger

1. Go to this page and download the library: Download xxxcoltxxx/request-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/ */

    

xxxcoltxxx / request-logger example snippets


public function report(Exception $exception)
{
    request_logger()->setException($exception);

    // ...
    parent::report($exception);
}

// Enable request logger for all requests without adds middleware
'all_routes' => true,

// Default transport
'default' => env('REQUEST_LOGGER_TRANSPORT', 'graylog'),

// The class for format log message
'formatter' => RequestLogger\RequestFormatter::class,

/*
 * Allowed transports with its configuration.
 * Drivers: 'graylog', 'log', 'null'
 */
'transports' => [

    // The graylog transport
    'graylog' => [
        // The Short message for graylog
        'short_message' => env('GRAYLOG_SHORT_MESSAGE', 'requests'),

        // Limit content size (in bytes). Set false to disable. Graylog has limitations on input messages
        'content_limit' => env('GRAYLOG_CONTENT_LIMIT', 30000),

        // The IP address of the log server
        'host' => env('GRAYLOG_HOST', '127.0.0.1'),

        // The UDP port of the log server
        'port' => env('GRAYLOG_PORT', '12201'),

        // The driver for send requests log to log server
        'driver' => RequestLogger\Transports\GelfUdpTransport::class,
    ],

    // ...
],

request_logger()->addMessage('Full name: John Doe')

# config/request_logger.php

return [
    'all_routes' => false,

    // ...
];

# routes/api.php

Route::get('admin/profile', function () {
    //
})->middleware('request_logger');

# config/request_logger.php

return [
    // ...

    'formatter' => function () {
        $provider  = request_logger();
        $exception = $provider->getException();
        $request   = $provider->getRequest();
        $response  = $provider->getResponse();

        return [
            'uri'           => $request->getRequestUri(),
            'has_exception' => ! is_null($exception),
        ];
    }

    // ...

# .env

REQUEST_LOGGER_TRANSPORT=custom

# config/request_logger.php

return [
    // ...

    'transports' => [
        // ...

        'custom' => [
            'driver' => Namespace\CustomTransport::class,
            'custom_option' => 'value',
        ],
    ],
];
bash
php artisan vendor:publish --provider="RequestLogger\RequestLoggerServiceProvider"