File Coverage

blib/lib/Cookieville/Plugin/AccessLog.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition 5 7 71.4
subroutine 5 5 100.0
pod 1 1 100.0
total 30 32 93.7


line stmt bran cond sub pod time code
1             package Cookieville::Plugin::AccessLog;
2              
3             =head1 NAME
4              
5             Cookieville::Plugin::AccessLog - Plugin for logging requests
6              
7             =head1 DESCRIPTION
8              
9             L is a plugin that will log any data used to
10             select, delete, insert or update records in the database. This log will be
11             written using the default L object, with "info" log level.
12              
13             Below you can see an example log created by this plugin:
14              
15             [Fri Jun 20 14:39:51 2014] [info] [Anonymous] GET / {} 200
16             [Fri Jun 20 14:39:51 2014] [info] [Anonymous] GET /sources {} 401
17             [Fri Jun 20 14:39:51 2014] [info] [Some-Invalid-Auth-Id] DELETE /Artist/1 {} 401
18             [Fri Jun 20 14:39:51 2014] [info] [Some-Invalid-Auth-Id] PATCH /Artist/1 {} 401
19             [Fri Jun 20 14:39:51 2014] [info] [Some-Invalid-Auth-Id] PUT /Artist {} 401
20             [Fri Jun 20 14:39:51 2014] [info] [Some-Invalid-Auth-Id] GET /Artist/search {"q" => "{}"} 401
21             [Fri Jun 20 14:39:51 2014] [info] [Some-Long-Auth-Id-12b34acf274] GET /sources {} 200
22             [Fri Jun 20 14:39:51 2014] [info] [Some-Long-Auth-Id-12b34acf274] DELETE /Artist/1 {} 200
23             [Fri Jun 20 14:39:51 2014] [info] [Some-Long-Auth-Id-12b34acf274] PUT /Artist {"name" => "Elvis"} 200
24             [Fri Jun 20 14:39:51 2014] [info] [Some-Long-Auth-Id-12b34acf274] PATCH /Artist/1 {"url" => "http://mojolicio.us"} 200
25             [Fri Jun 20 14:39:51 2014] [info] [Some-Long-Auth-Id-12b34acf274] GET /Artist/search {"q" => "{}"} 200
26             [Fri Jun 20 14:39:51 2014] [info] [Some-Long-Auth-Id-12b34acf274] DELETE /CD/1 {} 401
27             [Fri Jun 20 14:39:51 2014] [info] [Some-Long-Auth-Id-12b34acf274] PUT /CD {"name" => "Elvis"} 401
28             [Fri Jun 20 14:39:51 2014] [info] [Some-Long-Auth-Id-12b34acf274] PATCH /CD/1 {"url" => "http://mojolicio.us"} 404
29             [Fri Jun 20 14:39:51 2014] [info] [Some-Long-Auth-Id-12b34acf274] GET /CD/search {"q" => "{}"} 200
30              
31             The output is taken from the C unit test.
32              
33             THE LOG FORMAT IS CURRENTLY EXPERIMENTAL AND WILL CHANGE WITHOUT ANY NOTICE.
34              
35             =head1 SYNOPSIS
36              
37             This L explains how to enable this plugin in the L server.
38              
39             Example C file that will enable this plugin:
40              
41             {
42             access_log => {},
43             }
44              
45             =cut
46              
47 2     2   1921 use Mojo::Base 'Mojolicious::Plugin';
  2         2  
  2         12  
48 2     2   244 use Data::Dumper ();
  2         2  
  2         39  
49 2     2   7 use Time::HiRes qw( gettimeofday tv_interval );
  2         4  
  2         16  
50              
51             =head1 METHODS
52              
53             =head2 register
54              
55             This plugin will register a "before_dispatch" hook which will log on
56             L "finish" event.
57              
58             =cut
59              
60             sub register {
61 2     2 1 62 my ($self, $app, $config) = @_;
62              
63             $app->hook(
64             before_dispatch => sub {
65 15     15   129459 my $c = shift;
66 15         80 my $t0 = [gettimeofday];
67              
68             $c->tx->on(
69             finish => sub {
70 15         41503 my $tx = shift;
71 15         75 my $req = $c->req;
72 15   66     738 my $dd = Data::Dumper->new([$req->json || $req->url->query->to_hash]);
73              
74 15         5294 $dd->Indent(1)->Indent(0)->Sortkeys(1)->Terse(1)->Useqq(1);
75              
76 15   100     778 $app->log->info(
      50        
77             sprintf '[%s] %s %s %s %s',
78             $c->stash('auth_id') || 'Anonymous',
79             $req->method, $req->url->path, $dd->Dump, $c->res->code || '000',
80             );
81             },
82 15         312 );
83             }
84 2         14 );
85             }
86              
87             =head1 AUTHOR
88              
89             Jan Henning Thorsen - C
90              
91             =cut
92              
93             1;