Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ m6_web_log_bridge:
level: 'error'
options:
post_parameters: true # From add post parameters in response content (with DefaultFormatter)
request_body: true # From add request body in request content (with DefaultFormatter)
all_error: # All route, all method in error
route: ~
routes: ~
Expand All @@ -94,7 +95,7 @@ routes: ['!excluded_one', '!excluded_two'] # Add all routes except the excluded
*By default, `level` is `info`*

You can declare all the options you want.
By default, only `response_body` and `post_parameters` is supported by the DefaultFormatter
By default, only `response_body`, `post_parameters` and `request_body` is supported by the DefaultFormatter

Status support multiples formats :
```yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->arrayNode('options')
->children()
->booleanNode('post_parameters')->defaultFalse()->end()
->booleanNode('request_body')->defaultFalse()->end()
->booleanNode('response_body')->defaultFalse()->end()
->end()
->end()
Expand Down
14 changes: 12 additions & 2 deletions src/M6Web/Bundle/LogBridgeBundle/Formatter/DefaultFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public function getLogContent(Request $request, Response $response, array $optio
$requestContent .= str_pad((string) $name, 20, ' ', STR_PAD_RIGHT).': '.$value."\n";
}

if (array_key_exists('request_body', $options)
&& $options['request_body'] === true
&& in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) {
$content = $request->getContent();
if ($content) {
$requestContent .= "###> Body\n";
$requestContent .= $content."\n";
}
}

$responseContent = sprintf(
"Response\n------------------------\nHTTP %s %d\n",
$response->getProtocolVersion(),
Expand All @@ -77,8 +87,8 @@ public function getLogContent(Request $request, Response $response, array $optio

// Render post parameters
if (array_key_exists('post_parameters', $options)
&& $options['post_parameters'] == true
&& in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'])) {
&& $options['post_parameters'] === true
&& in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) {
$responseContent .= "Post parameters\n";
$responseContent .= $this->formatParameters($request->request->all());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,48 @@ public function testPostProvider(): void
->contains('└ title : Non mais Allo quoi')
;
}

public function testRequestBodyProvider(): void
{
$data = [
'var1' => 'value un',
'var2' => 'value 2',
'programs' => [
'id' => 42,
'title' => 'Non mais Allo quoi'
]
];


$request = new Request([], [], [], [], [], ['REQUEST_METHOD' => 'POST'], json_encode($data, JSON_THROW_ON_ERROR));

$response = new Response('Body content response');
$tokenstorage = $this->getMockedTokenStorage();

$this
->if($provider = $this->createProvider())
->then
->object($provider->setTokenStorage($tokenstorage))
->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Formatter\DefaultFormatter::class)
->string($provider->getLogContent($request, $response, ['request_body' => true]))
->contains('HTTP 1.0 200')
->contains(<<<TXT
Request
------------------------
###> Body
{"var1":"value un","var2":"value 2","programs":{"id":42,"title":"Non mais Allo quoi"}}
TXT
)
->string($provider->getLogContent($request, $response, ['request_body' => false]))
->contains('HTTP 1.0 200')
->contains(<<<TXT
Request
------------------------
------------------------
Response
------------------------
TXT
)
;
}
}