File Coverage

blib/lib/WWW/Suffit/Plugin/Syslog.pm
Criterion Covered Total %
statement 18 30 60.0
branch 0 2 0.0
condition 0 12 0.0
subroutine 6 8 75.0
pod 1 1 100.0
total 25 53 47.1


line stmt bran cond sub pod time code
1             package WWW::Suffit::Plugin::Syslog;
2 1     1   215275 use strict;
  1         3  
  1         41  
3 1     1   12 use warnings;
  1         2  
  1         87  
4 1     1   630 use utf8;
  1         290  
  1         8  
5              
6             =encoding utf8
7              
8             =head1 NAME
9              
10             WWW::Suffit::Plugin::Syslog - A plugin for enabling logging to syslog for Suffit API servers
11              
12             =head1 SYNOPSIS
13              
14             # in your startup
15             $self->plugin('WWW::Suffit::Plugin::Syslog');
16              
17             =head1 DESCRIPTION
18              
19             This plugin for enabling logging to syslog for Suffit API servers
20              
21             =head1 METHODS
22              
23             Internal methods
24              
25             =head2 register
26              
27             Do not use directly. It is called by Mojolicious.
28              
29             =head1 OPTIONS
30              
31             =head2 enable
32              
33             Need to be true to activate this plugin.
34             Default to true if "mode" in Mojolicious is something else than "development"
35              
36             =head2 facility
37              
38             The syslog facility to use. Default to "user"
39              
40             =head2 ident
41              
42             The syslog ident to use. Default to "moniker" in Mojolicious
43              
44             =head1 SEE ALSO
45              
46             L, L, L
47              
48             =head1 AUTHOR
49              
50             Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE
51              
52             =head1 COPYRIGHT
53              
54             Copyright (C) 1998-2026 D&D Corporation
55              
56             =head1 LICENSE
57              
58             This program is distributed under the terms of the Artistic License Version 2.0
59              
60             See the C file or L for details
61              
62             =cut
63              
64 1     1   796 use Mojo::Base 'Mojolicious::Plugin';
  1         13631  
  1         11  
65              
66             our $VERSION = '1.01';
67              
68 1     1   2576 use Sys::Syslog qw//;
  1         30954  
  1         61  
69              
70             use constant {
71 1         448 LOGOPTS => 'ndelay,pid', # For Sys::Syslog
72             SEPARATOR => ' ',
73             LOGFORMAT => '%s',
74 1     1   32 };
  1         3  
75             my %LOGLEVELS = (
76             'debug' => Sys::Syslog::LOG_DEBUG, # debug-level message
77             'info' => Sys::Syslog::LOG_INFO, # informational message
78             'warn' => Sys::Syslog::LOG_WARNING, # warning conditions
79             'error' => Sys::Syslog::LOG_ERR, # error conditions
80             'fatal' => Sys::Syslog::LOG_CRIT, # critical conditions
81             );
82              
83             sub register {
84 0     0 1   my ($self, $app, $config) = @_;
85 0 0 0       return 1 unless $config->{enable} // $app->mode ne 'development';
86              
87             # Correct plugin config
88 0   0       $config->{facility} ||= Sys::Syslog::LOG_USER;
89 0   0       $config->{ident} ||= $app->moniker;
90 0   0       $config->{logopt} ||= LOGOPTS;
91              
92             # Open sys log socket
93 0           Sys::Syslog::openlog($config->{ident}, $config->{logopt}, $config->{facility});
94              
95             # Unsubscribe
96 0           $app->log->unsubscribe('message');
97 0           $app->log->unsubscribe(message => \&_to_syslog);
98              
99             # Subscribe
100 0           $app->log->on(message => \&_to_syslog);
101             }
102             sub _to_syslog {
103 0     0     my ($log, $level, @msg) = @_;
104 0   0       my $lvl = $LOGLEVELS{$level} // Sys::Syslog::LOG_DEBUG;
105 0           Sys::Syslog::syslog($lvl, LOGFORMAT, join(SEPARATOR, @msg));
106             }
107              
108             1;
109              
110             __END__