PHP code example of garbetjie / http-request-logger
1. Go to this page and download the library: Download garbetjie/http-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/ */
garbetjie / http-request-logger example snippets
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
// ...
protected $middleware = [
\Garbetjie\RequestLogging\Http\Middleware\IncomingRequestLoggingMiddleware::class
];
// ...
}
$monolog = new Monolog\Logger('logger');
$logger = new Garbetjie\RequestLogging\Http\Logger($monolog);
$server = new Garbetjie\RequestLogging\Http\SoapServer($logger, '/path/to/wsdl/or/null');
$server->setObject(new stdClass());
$server->handle();;
namespace App\Http\Controllers;
class MyController
{
public function myAction(\GuzzleHttp\ClientInterface $client)
{
return $client->request('GET', 'https://example.org')->getBody()->getContents();
}
}
$monolog = new Monolog\Logger('logger');
$logger = new Garbetjie\RequestLogging\Http\Logger($monolog);
$stack = GuzzleHttp\HandlerStack::create();
$stack->push(new Garbetjie\RequestLogging\Http\Middleware\OutgoingRequestLoggingMiddleware($logger));
$client = new GuzzleHttp\Client(['stack' => $stack]);
$client->request('GET', 'https://example.org');
$context = [
'id' => '', // string - contains the unique ID that links this request to its response.
'method' => '', // string - upper-cased request method.
'url' => '', // string - URL to which the request was sent.
'body_length' => 0, // integer - size of the body sent.
'body' => base64_encode(''), // string - base64-encoded body sent in the request.
'headers' => [], // array<string, string> - array of headers sent in the request. Names are normalized and lower-cased.
];
$context = [
'id' => '', // string - contains the unique ID that links this response to the request that created it.
'duration' => 0.0, // float - the duration of the request, in seconds (with fractional milliseconds).
'status_code' => 0, // integer - the HTTP status code returned in the response.
'body_length' => 0, // integer - the size of the body sent.
'body' => base64_encode(''), // string - base64-encoded body sent in the response.
'headers' => [], // array<string, string> - array of headers sent in the response. Names are normalized and lower-cased.
];
$monolog = new Monolog\Logger('name');
$logger = new Garbetjie\RequestLogging\Http\Logger($monolog);
$logger->context(
function (Garbetjie\RequestLogging\Http\RequestEntry $entry): array {
// Return an array containing the context to log.
return [];
},
function (Garbetjie\RequestLogging\Http\ResponseEntry $entry): array {
// Return an array containing the context to log.
return [];
}
);
$monolog = new Monolog\Logger('name');
$logger = new Garbetjie\RequestLogging\Http\Logger($monolog);
$logger->context(
function (Garbetjie\RequestLogging\Http\RequestEntry $entry): array {
$context = (new Garbetjie\RequestLogging\Http\Context\RequestContext())($entry);
$body = base64_decode($context['body']);
// Modify $body.
$context['body'] = base64_encode($body);
return $context;
},
null
);
$monolog = new Monolog\Logger('name');
$logger = new Garbetjie\RequestLogging\Http\Logger($monolog);
$logger->id(
function(): string {
return base64_encode(random_bytes(8));
}
);
$monolog = new Monolog\Logger('name');
$logger = new Garbetjie\RequestLogging\Http\Logger($monolog);
function shouldLog($request) {
if ($request instanceof Symfony\Component\HttpFoundation\Request) {
return stripos($request->getUri(), 'https://example.org') === false;
} elseif ($request instanceof Psr\Http\Message\RequestInterface) {
return stripos((string)$request->getUri(), 'https://example.org') === false;
} else {
return false;
}
}
$logger->enabled(
function(Garbetjie\RequestLogging\Http\RequestEntry $entry): bool { // Provide a callable that returns a boolean.
return shouldLog($entry->request());
},
function(Garbetjie\RequestLogging\Http\ResponseEntry $entry): bool {
return shouldLog($entry->request());
}
);
// Alternatively:
$logger->enabled(false, true);
$logger->enabled(
new Garbetjie\RequestLogging\Http\Context\RequestContext(),
new Garbetjie\RequestLogging\Http\Context\ResponseContext()
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.