Skip to content

Commit

Permalink
feat: http request data
Browse files Browse the repository at this point in the history
Signed-off-by: Andrii Nasinnyk <anasinnyk@macpaw.com>
  • Loading branch information
anasinnyk committed Jun 22, 2020
1 parent 7645d26 commit c9ca283
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
}
],
"require": {
"monolog/monolog": "^2.1"
"monolog/monolog": "^2.1",
"guzzlehttp/psr7": "^1.6"
},
"autoload": {
"psr-4": {"MacPaw\\MonologGkeFormatter\\": "src"}
Expand Down
19 changes: 16 additions & 3 deletions src/GkeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

namespace MacPaw\MonologGkeFormatter;

use GuzzleHttp\Psr7\ServerRequest;
use Monolog\Formatter\JsonFormatter;

class GkeFormatter extends JsonFormatter
{
public function format(array $record): string
{
$request = ServerRequest::fromGlobals();

return parent::format(
array_merge(
$record['extra'],
isset($record['exception'])
isset($record['context']) && isset($record['context']['exception'])
? [
'sourceLocation' => [
'file' => str_split(':', $record['exception']['file'])[0],
'line' => str_split(':', $record['exception']['file'])[1]
'file' => str_split(':', $record['context']['exception']['file'])[0],
'line' => str_split(':', $record['context']['exception']['file'])[1]
]
]
: [],
Expand All @@ -25,6 +28,16 @@ public function format(array $record): string
'severity' => $record['level_name'],
'serviceContext' => $record['context'],
'timestamp' => $record['datetime']->getTimestamp(),
'httpRequest' => [
'requestMethod' => $request->getMethod(),
'requestUrl' => $request->getUri()->__toString(),
'requestSize' => $request->getBody()->getSize(),
'protocol' => $request->getProtocolVersion(),
'referer' => $request->getHeader('Referer'),
'userAgent' => $request->getHeader('User-Agent'),
'remoteIp' => $request->getHeader('X-Forwarded-For'),
'serverIp' => $_SERVER['SERVER_ADDR'],
],
]
)
);
Expand Down

0 comments on commit c9ca283

Please sign in to comment.