blib/lib/Nginx/Log/Entry.pm

filter: Statementstmt 27.8% 17 / 61 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 22.7% 5 / 22 Podpod 100% 17 / 17 totaltotal 36.7% 39 / 106 CC 4 SCAR 10.2
CC
4
Cov
25%
new
6.0
get_datetime_obj
6.0
was_request_successful
6.0
CRAP 2.8
↑ Summary
1 package Nginx::Log::Entry;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Nginx::ParseLog;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use HTTP::BrowserDetect;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = 0.05;
9  
10 =head1 NAME
11  
12 Nginx::Log::Entry - This class represents a single line from the Nginx combined access log (the default access log format). It provides methods to extract information from the log entry, such as the browser, operating system, request type, ip address and more. If you want to gather statistics about an Nginx log, consider using L<Nginx::Log::Statistics> which uses this class.
13  
14 =cut
15  
16 =head1 SYNOPSIS
17  
18 use Nginx::Log::Entry;
19 my $entry = Nginx::Log::Entry->new(q{66.108.215.71 - - [24/Mar/2013:10:00:58 -0400] "GET / HTTP/1.1" 200 727 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0"});
20 print $entry->get_ip;
21 # 66.108.215.71
22
23 print $entry->get_os;
24 # Linux
25  
26 =cut
27  
28 =head1 METHODS
29  
30 =head2 new
31  
32 Instantiates a new entry object, requires a line from the Nginx access.log as a string argument.
33  
34 =cut
35  
36 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
37 0 my ( $class, $log_line ) = @_;
38 0 ▶ die "Error: no log string was passed to new" unless $log_line;
Branch: 1 branch
BranchTrueFalseTotal
unless $log_line 0 0 0
39 0 my $self = Nginx::ParseLog::parse($log_line);
40 0 $self->{detector} = HTTP::BrowserDetect->new( $self->{user_agent} );
41 0 return bless $self, $class;
42 }
43  
44 =head2 get_ip
45  
46 Returns the requestor's ip address.
47  
48 =cut
49  
50 0 ▶sub get_ip {
Subroutine get_ip: not called CC=1 SCAR=6.9
Pod documented
51 0 my $self = shift;
52 0 return $self->{ip};
53 }
54  
55 =head2 get_datetime_obj
56  
57 Returns a L<Time::Piece> object of the request datetime.
58  
59 =cut
60  
61 0 ▶sub get_datetime_obj {
Subroutine get_datetime_obj: not called CC=2 SCAR=17.9
Pod documented
62 0 my $self = shift;
63 0 ▶ unless ( exists $self->{datetime_obj} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"datetime_obj"}) 0 0 0
64 0 my $date_string = substr( $self->{time}, 0, -6 );
65 $self->{datetime_obj} =
66 0 Time::Piece->strptime( $date_string, "%d/%b/%Y:%H:%M:%S" );
67 }
68 0 return $self->{datetime_obj};
69 }
70  
71 =head2 get_timezone
72  
73 Returns the timezone GMT modifier, e.g. -400.
74  
75 =cut
76  
77 0 ▶sub get_timezone {
Subroutine get_timezone: not called CC=1 SCAR=6.9
Pod documented
78 0 my $self = shift;
79 0 return substr( $self->{time}, -5 );
80 }
81  
82 =head2 was_robot
83  
84 Returns 1 if the useragent string was a known robot, else returns 0.
85  
86 =cut
87  
88 0 ▶sub was_robot {
Subroutine was_robot: not called CC=1 SCAR=6.9
Pod documented
89 0 my $self = shift;
90 0 return $self->{detector}->robot;
91 }
92  
93 =head2 get_status
94  
95 Returns the http status number of the request.
96  
97 =cut
98  
99 0 ▶sub get_status {
Subroutine get_status: not called CC=1 SCAR=6.9
Pod documented
100 0 my $self = shift;
101 0 return $self->{status};
102 }
103  
104 =head2 get_request
105  
106 Returns the request string.
107  
108 =cut
109  
110 0 ▶sub get_request {
Subroutine get_request: not called CC=1 SCAR=6.9
Pod documented
111 0 my $self = shift;
112 0 return $self->{request};
113 }
114  
115 =head2 get_request_type
116  
117 Returns the http request type, e.g. GET.
118  
119 =cut
120  
121 0 ▶sub get_request_type {
Subroutine get_request_type: not called CC=1 SCAR=6.9
Pod documented
122 0 my $self = shift;
123 0 my @request = split( ' ', $self->get_request );
124 0 return $request[0];
125 }
126  
127 =head2 get_request_url
128  
129 Returns the requested url (excluding the base).
130  
131 =cut
132  
133 0 ▶sub get_request_url {
Subroutine get_request_url: not called CC=1 SCAR=6.9
Pod documented
134 0 my $self = shift;
135 0 my @request = split( ' ', $self->get_request );
136 0 return $request[1];
137 }
138  
139 =head2 get_request_http_version
140  
141 Returns http/1 or http/1.1.
142  
143 =cut
144  
145 0 ▶sub get_request_http_version {
Subroutine get_request_http_version: not called CC=1 SCAR=6.9
Pod documented
146 0 my $self = shift;
147 0 my @request = split( ' ', $self->get_request );
148 0 return $request[2];
149 }
150  
151 =head2 was_request_successful
152  
153 Returns 1 if the http status is a 200 series number (e.g. 200, 201, 202 etc), else returns 0.
154  
155 =cut
156  
157 0 ▶sub was_request_successful {
Subroutine was_request_successful: not called CC=2 SCAR=17.9
Pod documented
158 0 my $self = shift;
159 0 my $status = $self->get_status;
160 0 ▶ return substr( $status, 0, 1 ) == 2 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
substr($status, 0, 1) == 2 ? : 0 0 0
161 }
162  
163 =head2 get_useragent
164  
165 Returns the useragent string.
166  
167 =cut
168  
169 0 ▶sub get_useragent {
Subroutine get_useragent: not called CC=1 SCAR=6.9
Pod documented
170 0 my $self = shift;
171 0 return $self->{user_agent};
172 }
173  
174 =head2 get_os
175  
176 Returns the operating system, e.g. Windows.
177  
178 =cut
179  
180 0 ▶sub get_os {
Subroutine get_os: not called CC=1 SCAR=6.9
Pod documented
181 0 my $self = shift;
182 0 return $self->{detector}->os_string;
183 }
184  
185 =head2 get_browser
186  
187 Returns the browser type, e.g. Firefox.
188  
189 =cut
190  
191 0 ▶sub get_browser {
Subroutine get_browser: not called CC=1 SCAR=6.9
Pod documented
192 0 my $self = shift;
193 0 return $self->{detector}->browser_string;
194 }
195  
196 =head2 get_referer
197  
198 Returns the referer, e.g. google.com.
199  
200 =cut
201  
202 0 ▶sub get_referer {
Subroutine get_referer: not called CC=1 SCAR=6.9
Pod documented
203 0 my $self = shift;
204 0 return $self->{referer};
205 }
206  
207 =head2 get_bytes
208  
209 Returns the number of bytes sent, e.g. 754.
210  
211 =cut
212  
213 0 ▶sub get_bytes {
Subroutine get_bytes: not called CC=1 SCAR=6.9
Pod documented
214 0 my $self = shift;
215 0 return $self->{bytes_send};
216 }
217  
218 =head2 get_remote_user
219  
220 Returns the remote username. This is usually not set, and if not, returns '-' instead.
221  
222 =cut
223  
224 0 ▶sub get_remote_user {
Subroutine get_remote_user: not called CC=1 SCAR=6.9
Pod documented
225 0 my $self = shift;
226 0 return $self->{remote_user};
227 }
228  
229 =head1 AUTHOR
230  
231 David Farrell, C<< <davidnmfarrell at gmail.com> >>, L<perltricks.com|http://perltricks.com>
232  
233 =head1 BUGS
234  
235 Please report any bugs or feature requests to C<bug-nginx-log-statistics at rt.cpan.org>, or through
236 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Nginx-Log-Entry>. I will be notified, and then you'll
237 automatically be notified of progress on your bug as I make changes.
238  
239 =head1 SUPPORT
240  
241 You can find documentation for this module with the perldoc command.
242  
243 perldoc Nginx::Log::Entry
244  
245  
246 You can also look for information at:
247  
248 =over 4
249  
250 =item * RT: CPAN's request tracker (report bugs here)
251  
252 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Nginx-Log-Entry>
253  
254 =item * AnnoCPAN: Annotated CPAN documentation
255  
256 L<http://annocpan.org/dist/Nginx-Log-Entry>
257  
258 =item * CPAN Ratings
259  
260 L<http://cpanratings.perl.org/d/Nginx-Log-Entry>
261  
262 =item * Search CPAN
263  
264 L<http://search.cpan.org/dist/Nginx-Log-Entry/>
265  
266 =back
267  
268  
269 =head1 ACKNOWLEDGEMENTS
270  
271  
272 =head1 LICENSE AND COPYRIGHT
273  
274 Copyright 2013 David Farrell.
275  
276 This program is free software; you can redistribute it and/or modify it
277 under the terms of the the Artistic License (2.0). You may obtain a
278 copy of the full license at:
279  
280 L<http://www.perlfoundation.org/artistic_license_2_0>
281  
282 Any use, modification, and distribution of the Standard or Modified
283 Versions is governed by this Artistic License. By using, modifying or
284 distributing the Package, you accept this license. Do not use, modify,
285 or distribute the Package, if you do not accept this license.
286  
287 If your Modified Version has been derived from a Modified Version made
288 by someone other than you, you are nevertheless required to ensure that
289 your Modified Version complies with the requirements of this license.
290  
291 This license does not grant you the right to use any trademark, service
292 mark, tradename, or logo of the Copyright Holder.
293  
294 This license includes the non-exclusive, worldwide, free-of-charge
295 patent license to make, have made, use, offer to sell, sell, import and
296 otherwise transfer the Package with respect to any patent claims
297 licensable by the Copyright Holder that are necessarily infringed by the
298 Package. If you institute patent litigation (including a cross-claim or
299 counterclaim) against any party alleging that the Package constitutes
300 direct or contributory patent infringement, then this Artistic License
301 to you shall terminate on the date that such litigation is filed.
302  
303 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
304 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
305 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
306 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
307 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
308 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
309 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
310 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311  
312 =cut
313  
314 2 1;
↑ Summary