blib/lib/Mojolicious/Plugin/Syslog.pm

filter: Statementstmt 100% 51 / 51 Branchbran 70.0% 14 / 20 Conditioncond 47.3% 18 / 38 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 70.5% 103 / 146 CC 25 SCAR 17.0
CC
25
Cov
76%
_add_syslog
13.6
register
7.3
_add_access_log
6.1
CRAP 5.5
↑ Summary
1 package Mojolicious::Plugin::Syslog;
2 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Sys::Syslog qw(:standard :macros);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.06';
7  
8 2 my %PRIORITY = (
9 debug => LOG_DEBUG,
10 error => LOG_ERR,
11 fatal => LOG_CRIT,
12 info => LOG_INFO,
13 warn => LOG_WARNING,
14 );
15  
16 4 ▶sub register {
Subroutine register: called CC=6 SCAR=19.9
Pod documented
17 4 my ($self, $app, $config) = @_;
18  
19 $self->_add_syslog($app, %$config)
20 if $config->{enable} // $ENV{MOJO_SYSLOG_ENABLE}
21 4 ▶ // $app->mode ne 'development';
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"enable"} // $ENV{"MOJO_SYSLOG_ENABLE"}) // $app->mode ne "development" 1 3 4
Condition40%2/5
($config->{"enable"} // $ENV{"MOJO_SYSLOG_ENABLE"}) // $app->mode ne "development"
l!l
40
$config->{"enable"} // $ENV{"MOJO_SYSLOG_ENABLE"}
l!l&&r!l&&!r
040
MC/DC0%0/2
($config->{"enable"} // $ENV{"MOJO_SYSLOG_ENABLE"}) // $app->mode ne "development"
$config->{"enable"} $ENV{"MOJO_SYSLOG_ENABLE"}
Truth table33%1/3
A // B
A= $config->{"enable"}B= $ENV{"MOJO_SYSLOG_ENABLE"}
ABresult
1X1
011
000
22  
23 $self->_add_access_log($app, %$config)
24 4 ▶ if $config->{access_log} // $ENV{MOJO_SYSLOG_ACCESS_LOG};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"access_log"} // $ENV{"MOJO_SYSLOG_ACCESS_LOG"} 4 0 4
Condition66%2/3
$config->{"access_log"} // $ENV{"MOJO_SYSLOG_ACCESS_LOG"}
l!l&&r!l&&!r
310
MC/DC0%0/2
$config->{"access_log"} // $ENV{"MOJO_SYSLOG_ACCESS_LOG"}
$config->{"access_log"} $ENV{"MOJO_SYSLOG_ACCESS_LOG"}
Truth table66%2/3
A // B
A= $config->{"access_log"}B= $ENV{"MOJO_SYSLOG_ACCESS_LOG"}
ABresult
1X1
011
000
25 }
26  
27 4 ▶sub _add_access_log {
Subroutine _add_access_log: called CC=6 SCAR=18.1
28 4 my ($self, $app, %config) = @_;
29  
30 4 ▶ my $log_format = $config{access_log} || $ENV{MOJO_SYSLOG_ACCESS_LOG} || 'v1';
Condition60%3/5
$config{'access_log'} || $ENV{'MOJO_SYSLOG_ACCESS_LOG'} || 'v1'
l!l
40
$config{"access_log"} or $ENV{"MOJO_SYSLOG_ACCESS_LOG"}
l!l&&r!l&&!r
310
MC/DC0%0/2
$config{'access_log'} || $ENV{'MOJO_SYSLOG_ACCESS_LOG'} || 'v1'
$config{"access_log"} $ENV{"MOJO_SYSLOG_ACCESS_LOG"}
Truth table66%2/3
A or B
A= $config{"access_log"}B= $ENV{"MOJO_SYSLOG_ACCESS_LOG"}
ABresult
1X1
011
000
31 4 ▶ $log_format = '%H "%P" (%I) %C %M (%Ts)' if $log_format =~ /^v?1$/;
Branch: 1 branch
BranchTrueFalseTotal
if $log_format =~ /^v?1$/u 1 3 4
32 4 ▶ $log_format = '%R %H %U %C "%F" "%A" (%Ts)' if $log_format =~ /^v?2$/;
Branch: 1 branch
BranchTrueFalseTotal
if $log_format =~ /^v?2$/u 1 3 4
33  
34 $app->hook(
35 6 ▶ before_routes => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 6 shift->helpers->timing->begin(__PACKAGE__);
37 }
38 4 );
39  
40 my %extractors = (
41 2 ▶ A => sub { $_[1]->headers->user_agent || '' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->headers->user_agent or "" 2 0 2
42 6 ▶ C => sub { $_[2]->code },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 2 ▶ F => sub { $_[1]->headers->referrer || '' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->headers->referrer or "" 2 0 2
44 6 ▶ H => sub { $_[1]->method },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 2 ▶ I => sub { $_[1]->request_id },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 2 ▶ M => sub { $_[2]->message || $_[2]->default_message($_[2]->code) },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2]->message or $_[2]->default_message($_[2]->code) 0 2 2
47 5 ▶ P => sub { $_[1]->url->path->to_abs_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 2 ▶ R => sub { $_[0]->tx->remote_address },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 3 ▶ T => sub { $_[0]->helpers->timing->elapsed(__PACKAGE__) // 0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]->helpers->timing->elapsed(__PACKAGE__) // 0
l!l
30
MC/DC0%0/1
$_[0]->helpers->timing->elapsed(__PACKAGE__) // 0
$_[0]->helpers->timing->elapsed(__PACKAGE__)
Truth table50%1/2
A
A= $_[0]->helpers->timing->elapsed(__PACKAGE__)
Aresult
11
00
50 2 ▶ U => sub { $_[1]->url->to_abs->to_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 4 );
52  
53 4 ▶ my $app_log = $log_format =~ m!\%I\b! && $app->log;
Condition66%2/3
$log_format =~ /\%I\b/u && $app->log
!ll&&!rl&&r
202
MC/DC50%1/2
$log_format =~ /\%I\b/u && $app->log
$log_format =~ /\%I\b/u $app->log
Truth table66%2/3
A && B
A= $log_format =~ /\%I\b/uB= $app->log
ABresult
0X0
100
111
54  
55 4 my $re = join '|', sort keys %extractors;
56 4 $re = qr{\%($re)};
57  
58 $app->hook(
59 6 ▶ after_dispatch => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
60 6 my $c = shift;
61 6 ▶ my $log = $app_log || $c->log;
Condition66%2/3
$app_log || $c->log
l!l&&r!l&&!r
240
MC/DC0%0/2
$app_log || $c->log
$app_log $c->log
Truth table66%2/3
A || B
A= $app_logB= $c->log
ABresult
1X1
011
000
62 6 my ($req, $res) = ($c->req, $c->res);
63 6 ▶ my $level = $res->is_server_error ? 'warn' : 'info';
Branch: 1 branch
BranchTrueFalseTotal
$res->is_server_error ? : 1 5 6
64 6 my $message = $log_format;
65 6 $message =~ s!$re!$extractors{$1}->($c, $req, $res)!ge;
66 6 $log->$level($message);
67 }
68 4 );
69 }
70  
71 1 ▶sub _add_syslog {
Subroutine _add_syslog: called CC=8 SCAR=26.1
72 1 my ($self, $app, %config) = @_;
73  
74 1 ▶ $config{facility} ||= $ENV{MOJO_SYSLOG_FACILITY} || LOG_USER;
Condition33%2/6
$config{"facility"} ||= $ENV{"MOJO_SYSLOG_FACILITY"} || 8
l!l&&r!l&&!r
010
$ENV{'MOJO_SYSLOG_FACILITY'} || 8
l!l
100
MC/DC0%0/2
$config{"facility"} ||= $ENV{"MOJO_SYSLOG_FACILITY"} || 8
$config{"facility"} $ENV{'MOJO_SYSLOG_FACILITY'}
Truth table33%1/3
A ||= B
A= $config{"facility"}B= $ENV{'MOJO_SYSLOG_FACILITY'}
ABresult
1X1
011
000
75 1 ▶ $config{ident} ||= $ENV{MOJO_SYSLOG_IDENT} || $app->moniker;
Condition33%2/6
$config{"ident"} ||= $ENV{"MOJO_SYSLOG_IDENT"} || $app->moniker
l!l&&r!l&&!r
010
$ENV{'MOJO_SYSLOG_IDENT'} || $app->moniker
l!l&&r!l&&!r
100
MC/DC0%0/3
$config{"ident"} ||= $ENV{"MOJO_SYSLOG_IDENT"} || $app->moniker
$config{"ident"} $ENV{'MOJO_SYSLOG_IDENT'} $app->moniker
Truth table0%0/4
A ||= B || C
A= $config{"ident"}B= $ENV{'MOJO_SYSLOG_IDENT'}C= $app->moniker
ABCresult
1XX1
01X1
0011
0000
76 1 ▶ $config{logopt} ||= $ENV{MOJO_SYSLOG_LOGOPT} || 'ndelay,pid';
Condition40%2/5
$config{"logopt"} ||= $ENV{"MOJO_SYSLOG_LOGOPT"} || "ndelay,pid"
l!l&&r!l&&!r
010
$ENV{'MOJO_SYSLOG_LOGOPT'} || 'ndelay,pid'
l!l
10
MC/DC0%0/2
$config{"logopt"} ||= $ENV{"MOJO_SYSLOG_LOGOPT"} || "ndelay,pid"
$config{"logopt"} $ENV{'MOJO_SYSLOG_LOGOPT'}
Truth table33%1/3
A ||= B
A= $config{"logopt"}B= $ENV{'MOJO_SYSLOG_LOGOPT'}
ABresult
1X1
011
000
77  
78 1 openlog @config{qw(ident logopt facility)};
79 1 ▶ $app->log->unsubscribe('message') if $config{only_syslog};
Branch: 1 branch
BranchTrueFalseTotal
if $config{"only_syslog"} 1 0 1
80 1 $app->log->unsubscribe(message => \&_syslog);
81 1 $app->log->on(message => \&_syslog);
82 }
83  
84 4 ▶sub _syslog {
Subroutine _syslog: called CC=2 SCAR=7.2
85 4 my ($log, $level, @msg) = @_;
86 4 ▶ syslog $PRIORITY{$level}, '%s', join ' ', @msg if $PRIORITY{$level};
Branch: 1 branch
BranchTrueFalseTotal
if $PRIORITY{$level} 4 0 4
87 }
88  
89 2 1;
90  
91 =encoding utf8
92  
93 =head1 NAME
94  
95 Mojolicious::Plugin::Syslog - A plugin for enabling a Mojolicious app to log to syslog
96  
97 =head1 SYNOPSIS
98  
99 use Mojolicious::Lite;
100 plugin syslog => {facility => 'local0'};
101  
102 =head1 DESCRIPTION
103  
104 L<Mojolicious::Plugin::Syslog> is a L<Mojolicious> plugin for making
105 L<Mojo::Log> use L<Sys::Syslog> in addition (or instead) of file logging.
106  
107 This can be useful when starting Hypnotoad through Systemd, but want simple
108 logging of error messages to syslog.
109  
110 This plugin can also be used for only access logging, as an alternative to
111 L<Mojolicious::Plugin::AccessLog>. This is done by forcing L</enable> to
112 "0" and enabling L</access_log>.
113  
114 =head1 METHODS
115  
116 =head2 register
117  
118 $app->plugin(syslog => \%config);
119 $self->register($app, \%config);
120  
121 Used to register the plugin in your L<Mojolicious> application. Available
122 config parameters are:
123  
124 =over 2
125  
126 =item * access_log
127  
128 Used to enable logging of access to resources with a route enpoint. This means
129 that static files will not be logged, even if this option is enabled. It is
130 also possible to set the default value using the C<MOJO_SYSLOG_ACCESS_LOG>
131 environment variable.
132  
133 This can be "v1", "v2" or a custom format. The default is currently "v1", but
134 that might change in the future.
135  
136 .---------------------------------------.
137 | Version | Format |
138 |---------|-----------------------------|
139 | v1 | %H "%P" (%I) %C %M (%Ts) |
140 | v2 | %R %H %U %C "%F" "%A" (%Ts) |
141 '---------------------------------------'
142  
143 Supported log variables:
144  
145 .----------------------------------------------------.
146 | Variable | Value |
147 |----------|-----------------------------------------|
148 | %A | User-Agent request header |
149 | %C | Response status code, ex "200" |
150 | %F | Referer request header |
151 | %H | HTTP request method, ex "GET", "POST" |
152 | %I | Mojolicious request ID |
153 | %M | Response message, ex OK |
154 | %P | Request URL path |
155 | %R | Remote address |
156 | %T | Time in seconds for this request |
157 | %U | Absolute request URL, without user info |
158 '----------------------------------------------------'
159  
160 =item * enable
161  
162 Need to be true to activate this plugin. Will use the "MOJO_SYSLOG_ENABLE"
163 environment variable or default to true if L<Mojolicious/mode> is something
164 else than "development"
165  
166 =item * facility
167  
168 The syslog facility to use. Default to "MOJO_SYSLOG_FACILITY" environment
169 variable or default to "user".
170  
171 The default is EXPERIMENTAL.
172  
173 =item * ident
174  
175 The syslog ident to use. Default to "MOJO_SYSLOG_IDENT" environment variable or
176 L<Mojolicious/moniker>.
177  
178 =item * only_syslog
179  
180 Set this to true to disabled the default L<Mojo::Log> logging to file/stderr.
181  
182 =back
183  
184 =head1 AUTHOR
185  
186 Jan Henning Thorsen
187  
188 =head1 COPYRIGHT AND LICENSE
189  
190 Copyright (C) 2019, Jan Henning Thorsen.
191  
192 This program is free software, you can redistribute it and/or modify it under
193 the terms of the Artistic License version 2.0.
194  
195 =cut
↑ Summary