blib/lib/Jifty/Plugin/AccessLog.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 202 CC 18 SCAR 53.0
CC
18
Cov
0%
before_cleanup
306.0
init
6.0
new_request
2.0
CRAP 201.1
1 package Jifty::Plugin::AccessLog;
2 use strict;
3 use warnings;
4 use base qw/Jifty::Plugin Class::Data::Inheritable/;
5 __PACKAGE__->mk_accessors(qw/path format start respect_proxy/);
6  
7 use Jifty::Util;
8 use CGI::Cookie;
9 use Time::HiRes qw();
10  
11 our $VERSION = 1.0;
12  
13 =head1 NAME
14  
15 Jifty::Plugin::AccessLog - Concisely log Jifty requests
16  
17 =head1 DESCRIPTION
18  
19  
20 =head1 USAGE
21  
22 Add the following to your site_config.yml
23  
24 framework:
25 Plugins:
26 - AccessLog: {}
27  
28 =head2 OPTIONS
29  
30 =over 4
31  
32 =item path
33  
34 The file to log to; defaults to F<log/access_log>.
35  
36 =item respect_proxy
37  
38 If set to a true value, will display the C<X-Forwarded-For> header as
39 the originating IP of requests.
40  
41 =item format
42  
43 The format string to use when logging. This module attempts to be as
44 Apache-compatible as possible; it supports the following format
45 escapes:
46  
47 =over
48  
49 =item %%
50  
51 The percent sign.
52  
53 =item %a
54  
55 Remote IP address.
56  
57 =item %{Foobar}C
58  
59 The contents of the cookie I<Foobar> in the request sent to the
60 server.
61  
62 =item %D
63  
64 The time taken to serve the request, in micoseconds.
65  
66 =item %h
67  
68 Remote IP address.
69  
70 =item %{Foobar}n
71  
72 The content of the I<Foobar> header line(s) in the request.
73  
74 =item %l
75  
76 The first 8 characters of the session ID, if any.
77  
78 =item %m
79  
80 The request method.
81  
82 =item %{Foobar}n
83  
84 The value of the template or request argument I<Foobar>, as sent by
85 the client, or set in the dispatcher.
86  
87 =item %{Foobar}o
88  
89 The value of the I<Foobar> header line(s) in the response.
90  
91 =item %p
92  
93 The canonical port of the server serving the request. Alternate forms
94 include C<%{canonical}p>, C<%{local}p>, and C<%{remote}p>, which are
95 the respective connection ports.
96  
97 =item %P
98  
99 The process ID that serviced the request.
100  
101 =item %s
102  
103 The status code of the response.
104  
105 =item %t
106  
107 The time the request was recieved, formatted in Apache's default
108 format string (C<[%d/%b/%Y:%T %z]>). C<%{I<format>}t> can be used to
109 provide a C<strftime>-style custom I<format>.
110  
111 =item %T
112  
113 The time taken to serve the request, in seconds.
114  
115 =item %u
116  
117 The value of L<Jifty::CurrentUser/username>, if any.
118  
119 =item %U
120  
121 The path requested. In the event that the request was for one or more
122 regions, the list of regions will be given in square brackets.
123  
124 =item %v
125  
126 The canonical server name of the server.
127  
128 =item %x
129  
130 The list of active actions run in the request. Failed actions will be
131 followed with an exclamation mark, un-run actions with a tilde.
132  
133 =item %X
134  
135 As C<%X>, but also includes all argument values to each action.
136  
137 =back
138  
139 =back
140  
141 =head2 METHODS
142  
143 =head2 init
144  
145 Installs the trigger for each request.
146  
147 =cut
148  
149 sub init {
150 my $self = shift;
151 my %args = (
152 path => 'log/access_log',
153 format => '%h %l %u %t %m %U %s %T %x',
154 @_,
155 );
156  
157 return if $self->_pre_init;
158  
159 $self->path(Jifty::Util->absolute_path( $args{path} ));
160 $self->format($args{format});
161 $self->respect_proxy($args{respect_proxy});
162 Jifty::Handler->add_trigger(
163 before_cleanup => sub { $self->before_cleanup }
164 );
165 }
166  
167 =head2 new_request
168  
169 On each request, log when it starts.
170  
171 =cut
172  
173 sub new_request {
174 my $self = shift;
175 $self->start(Time::HiRes::time);
176 }
177  
178 =head2 before_cleanup
179  
180 Open, and append to, the logfile with the format specified.
181  
182 =cut
183  
184 sub before_cleanup {
185 my $self = shift;
186 my $r = Jifty->web->request;
187 Jifty->web->response->status(200)
188 unless defined Jifty->web->response->status;
189  
190 my $actions = sub {
191 my $long = shift;
192  
193 my $one_action = sub {
194 my $a = shift;
195 my $base = $a->class;
196 my $result = Jifty->web->response->result($a->moniker);
197 $base .= "~" if not $a->has_run or not $result;
198 $base .= "!" if $result and not $result->success;
199 return $base unless $long;
200 return "($base={"
201 . join( ",",
202 map { "$_=" . $a->argument($_) } keys %{ $a->arguments } )
203 . "})"
204 };
205 return sub {
206 my @a = grep { $_->active } $r->actions;
207 return "-" unless @a;
208 ( $r->just_validating ? "V" : "" ) . "<" . join(
209 ", ", map {$one_action->($_)} @a) . ">";
210 }
211 };
212  
213 my %ESCAPES = (
214 '%' => sub { '%' },
215 a => sub { ($self->respect_proxy && $r->header("X-Forwarded-For")) || $r->address },
216 C => sub { my $c = { CGI::Cookie->fetch() }->{+shift}; $c ? $c->value : undef },
217 D => sub { sprintf "%.3fms", (Time::HiRes::time - $self->start)*1000 },
218 e => sub { $r->env->{+shift} },
219 h => sub { ($self->respect_proxy && $r->header("X-Forwarded-For")) || $r->remote_host || $r->address },
220 i => sub { $r->header(shift) },
221 l => sub { substr( Jifty->web->session->id || '-', 0, 8 ) },
222 m => sub { $r->method },
223 n => sub { $r->template_argument($_[0]) || $r->argument($_[0]) },
224 o => sub { Jifty->web->response->header(shift) },
225 p => sub {
226 return Jifty->config->framework("Web")->{Port} if $_[0] eq "canonical";
227 return $r->env->{SERVER_PORT} if $_[0] eq "local";
228 return $r->env->{REMOTE_PORT} if $_[0] eq "remote";
229 return Jifty->config->framework("Web")->{Port};
230 },
231 P => sub { $$ },
232 s => sub { Jifty->web->response->status =~ /^(\d+)/; $1 || "200" },
233 t => sub { DateTime->from_epoch(epoch => $self->start)->strftime(shift || "[%d/%b/%Y:%T %z]") },
234 T => sub { sprintf "%.3fs", (Time::HiRes::time - $self->start) },
235 u => sub { Jifty->web->current_user->username },
236 U => sub {
237 if (my @f = $r->fragments) {
238 return '[' . join(" ", map {s/ /%20/g;$_} map {$_->path} @f ) . ']';
239 } else {
240 my $path = $r->path;
241 $path =~ s/ /%20/g;
242 return $path;
243 }
244 },
245 v => sub { URI->new(Jifty->config->framework("Web")->{BaseURL})->host },
246 x => $actions->(0),
247 X => $actions->(1),
248 );
249  
250 my $replace = sub {
251 my ($only_on, $string, $format) = @_;
252 if (defined $only_on) {
253 return "" unless grep {Jifty->web->response->status eq $_} split /,/, $only_on;
254 }
255 my $r;
256 if (exists $ESCAPES{$format}) {
257 $r = ref $ESCAPES{$format} ? eval {$ESCAPES{$format}->($string)} : $ESCAPES{$format};
258 } else {
259 $r = "%".$format;
260 }
261 return defined $r ? $r : "-";
262 };
263  
264  
265 my $s = $self->format;
266 $s =~ s/%(\d+(?:,\d+)*)?(?:{(.*?)})?([a-zA-Z%])/$replace->($1,$2,$3)/ge;
267  
268 open my $access_log, '>>', $self->path or do {
269 $self->log->error("Unable to open @{[$self->path]} for writing: $!");
270 return;
271 };
272 $access_log->syswrite( "$s\n" );
273 $access_log->close;
274 }
275  
276 =head1 SEE ALSO
277  
278 L<Jifty::Plugin::Recorder> for more verbose debugging information.
279  
280 =head1 COPYRIGHT AND LICENSE
281  
282 Copyright 2009 Best Practical Solutions
283  
284 This is free software and may be modified and distributed under the same terms as Perl itself.
285  
286 =cut
287  
288 1;