Skip to content

Instantly share code, notes, and snippets.

@jenky
Created June 15, 2020 12:02
Show Gist options
  • Save jenky/b6d24d393eedf65793656302f434f65f to your computer and use it in GitHub Desktop.
Save jenky/b6d24d393eedf65793656302f434f65f to your computer and use it in GitHub Desktop.
Guzzle custom MessageFormatter
<?php
namespace App\Logging;
use GuzzleHttp\MessageFormatter;
use Illuminate\Support\Arr;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class GuzzleMessageFormatter extends MessageFormatter
{
/**
* The list of hidden request and response headers.
*
* @var array
*/
public static $hiddenHeaders = [
'Authorization',
];
/**
* The list of hidden request and response parameters.
*
* @var array
*/
public static $hiddenParameters = [
'password',
'password_confirmation',
];
/**
* {@inheritDoc}
*/
public function format(
RequestInterface $request,
ResponseInterface $response = null,
\Exception $error = null
) {
$message = sprintf('%s %s', $request->getMethod(), (string) $request->getUri());
if (! is_null($response)) {
$message = sprintf('[%d] %s', $response->getStatusCode(), $message);
} elseif (! is_null($error)) {
$message = $error->getMessage();
}
return json_encode([
'message' => $message,
'request' => $this->request($request),
'response' => $this->response($response),
'error' => $this->error($error),
]);
}
/**
* Format the request data.
*
* @param \Psr\Http\Message\RequestInterface $request
* @return array
*/
protected function request(RequestInterface $request)
{
$data = [
'target' => $request->getRequestTarget(),
'method' => $request->getMethod(),
'uri' => (string) $request->getUri(),
'headers' => $this->headers($request->getHeaders()),
'body' => $this->payload(json_decode($request->getBody(), true)),
'client_ip' => request()->ip(),
'user_agent' => request()->userAgent(),
];
$query = parse_url($data['uri'], PHP_URL_QUERY);
parse_str($query, $array);
$data['query_string'] = $this->payload($array);
return $data;
}
/**
* Format the response data.
*
* @param \Psr\Http\Message\ResponseInterface|null $response
* @return array
*/
protected function response(ResponseInterface $response = null)
{
if (is_null($response)) {
return [];
}
$body = (string) $response->getBody();
if (is_array(json_decode($body, true)) &&
json_last_error() === JSON_ERROR_NONE) {
$body = $this->payload(json_decode($response->getBody(), true));
}
return [
'status' => $response->getStatusCode(),
'headers' => $this->headers($response->getHeaders()),
'body' => $body,
];
}
/**
* Format the exception message.
*
* @param \Exception $exception
* @return array
*/
protected function error(\Exception $exception = null)
{
if (is_null($exception)) {
return [];
}
return [
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
];
}
/**
* Format the given headers.
*
* @param array $headers
* @return array
*/
protected function headers($headers)
{
$headers = collect($headers)->map(function ($header) {
return $header[0] ?? [];
})->toArray();
return $this->hideParameters($headers, static::$hiddenHeaders);
}
/**
* Format the given payload.
*
* @param array $payload
* @return array
*/
protected function payload($payload)
{
return $this->hideParameters($payload, static::$hiddenParameters);
}
/**
* Hide the given parameters.
*
* @param array $data
* @param array $hidden
* @return mixed
*/
protected function hideParameters($data, $hidden)
{
foreach ($hidden as $parameter) {
if (Arr::get($data, $parameter)) {
Arr::set($data, $parameter, '********');
}
}
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment