Skip to content

Commit a593573

Browse files
committed
request_body option to log request content
1 parent ca1074a commit a593573

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function getConfigTreeBuilder(): TreeBuilder
7474
->arrayNode('options')
7575
->children()
7676
->booleanNode('post_parameters')->defaultFalse()->end()
77+
->booleanNode('request_body')->defaultFalse()->end()
7778
->booleanNode('response_body')->defaultFalse()->end()
7879
->end()
7980
->end()

src/M6Web/Bundle/LogBridgeBundle/Formatter/DefaultFormatter.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ public function getLogContent(Request $request, Response $response, array $optio
6464
$requestContent .= str_pad((string) $name, 20, ' ', STR_PAD_RIGHT).': '.$value."\n";
6565
}
6666

67+
if (array_key_exists('request_body', $options)
68+
&& $options['request_body'] === true
69+
&& in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) {
70+
$content = $request->getContent();
71+
if ($content) {
72+
$requestContent .= "###> Body\n";
73+
$requestContent .= $content . "\n";
74+
}
75+
76+
}
77+
6778
$responseContent = sprintf(
6879
"Response\n------------------------\nHTTP %s %d\n",
6980
$response->getProtocolVersion(),
@@ -77,8 +88,8 @@ public function getLogContent(Request $request, Response $response, array $optio
7788

7889
// Render post parameters
7990
if (array_key_exists('post_parameters', $options)
80-
&& $options['post_parameters'] == true
81-
&& in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'])) {
91+
&& $options['post_parameters'] === true
92+
&& in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) {
8293
$responseContent .= "Post parameters\n";
8394
$responseContent .= $this->formatParameters($request->request->all());
8495
}

src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Formatter/DefaultFormatter.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,48 @@ public function testPostProvider(): void
136136
->contains('└ title : Non mais Allo quoi')
137137
;
138138
}
139+
140+
public function testRequestBodyProvider(): void
141+
{
142+
$data = [
143+
'var1' => 'value un',
144+
'var2' => 'value 2',
145+
'programs' => [
146+
'id' => 42,
147+
'title' => 'Non mais Allo quoi'
148+
]
149+
];
150+
151+
152+
$request = new Request([], [], [], [], [], ['REQUEST_METHOD' => 'POST'], json_encode($data, JSON_THROW_ON_ERROR));
153+
154+
$response = new Response('Body content response');
155+
$tokenstorage = $this->getMockedTokenStorage();
156+
157+
$this
158+
->if($provider = $this->createProvider())
159+
->then
160+
->object($provider->setTokenStorage($tokenstorage))
161+
->isInstanceOf(\M6Web\Bundle\LogBridgeBundle\Formatter\DefaultFormatter::class)
162+
->string($provider->getLogContent($request, $response, ['request_body' => true]))
163+
->contains('HTTP 1.0 200')
164+
->contains(<<<TXT
165+
Request
166+
------------------------
167+
###> Body
168+
{"var1":"value un","var2":"value 2","programs":{"id":42,"title":"Non mais Allo quoi"}}
169+
TXT
170+
)
171+
->string($provider->getLogContent($request, $response, ['request_body' => false]))
172+
->contains('HTTP 1.0 200')
173+
->contains(<<<TXT
174+
Request
175+
------------------------
176+
------------------------
177+
Response
178+
------------------------
179+
TXT
180+
)
181+
;
182+
}
139183
}

0 commit comments

Comments
 (0)