diff --git a/README.md b/README.md index 69de3d7..fe6e6e6 100644 --- a/README.md +++ b/README.md @@ -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: ~ @@ -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 diff --git a/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/Configuration.php b/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/Configuration.php index 7d32305..f43d1f5 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/Configuration.php +++ b/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/Configuration.php @@ -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() diff --git a/src/M6Web/Bundle/LogBridgeBundle/Formatter/DefaultFormatter.php b/src/M6Web/Bundle/LogBridgeBundle/Formatter/DefaultFormatter.php index 02ad66c..065d7a4 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Formatter/DefaultFormatter.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Formatter/DefaultFormatter.php @@ -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(), @@ -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()); } diff --git a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Formatter/DefaultFormatter.php b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Formatter/DefaultFormatter.php index f45f3d4..6f91a68 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Formatter/DefaultFormatter.php +++ b/src/M6Web/Bundle/LogBridgeBundle/Tests/Units/Formatter/DefaultFormatter.php @@ -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(<< 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(<<