From 44f53ffa4048907e9398a1c5cd266e255c923141 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Mon, 20 Nov 2017 12:41:34 +0200 Subject: [PATCH] Bug 19652: Debug-level logging for response JSON This patch adds debug-level logging for response JSON. This is useful for viewing responses via REST API log file. To test: 1. prove t/db_dependent/api/v1.t 2. Configure log4perl configuration file for REST API and set log level to DEBUG 3. Make any request to existing REST API endpoint 4. Observe response JSON in your log file --- Koha/REST/V1.pm | 11 +++++++++++ t/db_dependent/api/v1.t | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/Koha/REST/V1.pm b/Koha/REST/V1.pm index 90f7e00..3008efa 100644 --- a/Koha/REST/V1.pm +++ b/Koha/REST/V1.pm @@ -52,6 +52,7 @@ sub startup { } $self->app->hook(before_render => \&default_exception_handling); + $self->app->hook(before_render => \&log_response); $self->plugin(OpenAPI => { url => $self->home->rel_file("api/v1/swagger/swagger.json"), @@ -100,6 +101,16 @@ sub default_exception_handling { } } +=head3 log_response + +=cut + +sub log_response { + my ($c, $args) = @_; + + eval { $c->app->log->debug(Mojo::JSON::encode_json($args)) }; +} + sub hateoas { my ($c, $responseBody, @refAndUrls) = @_; diff --git a/t/db_dependent/api/v1.t b/t/db_dependent/api/v1.t index b5804b5..a74ddcf 100644 --- a/t/db_dependent/api/v1.t +++ b/t/db_dependent/api/v1.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; use Test::Mojo; use Test::Warn; @@ -29,7 +29,7 @@ use Mojolicious::Lite; use Try::Tiny; my $config = { - 'log4perl.logger.rest.Koha.REST.V1' => 'ERROR, TEST', + 'log4perl.logger.rest.Koha.REST.V1' => 'DEBUG, TEST', 'log4perl.appender.TEST' => 'Log::Log4perl::Appender::TestBuffer', 'log4perl.appender.TEST.layout' => 'SimpleLayout', }; @@ -103,6 +103,50 @@ subtest 'default_exception_handling() tests' => sub { }; }; +subtest 'log_response() tests' => sub { + plan tests => 12; + + $t->app->routes->get('/response/log/json' => sub { + $_[0]->render( status => 200, json => { wow => "it worked" } ) + }); + $t->app->routes->get('/response/log/other' => sub { + $_[0]->render( status => 200, data => 'ERROR!' ) + }); + $t->app->routes->get('/response/log/500' => sub { + die; + }); + + my $appender = Log::Log4perl->appenders->{TEST}; + + $t->get_ok('/response/log/json') + ->status_is(200) + ->json_is('/wow' => 'it worked'); + is($appender->buffer, + "DEBUG - {\"json\":{\"wow\":\"it worked\"},\"status\":200}\n", + 'Found response JSON' + ); + $appender->{appender}->{buffer} = undef; + + $t->get_ok('/response/log/other') + ->status_is(200) + ->content_is('ERROR!'); + is($appender->buffer, + "DEBUG - {\"data\":\"ERROR!<\\/b>\",\"status\":200}\n", + 'Found response JSON' + ); + $appender->{appender}->{buffer} = undef; + + $t->get_ok('/response/log/500') + ->status_is(500) + ->json_is('/error' => 'Something went wrong, check the logs.'); + like($appender->buffer, + qr/DEBUG - \{"json":\{"error":"Something went wrong, check the logs."\},"status":500\}\n/, + 'Found response error content' + ); + $appender->{appender}->{buffer} = undef; + +}; + sub add_default_exception_routes { my ($t) = @_; -- 2.7.4