File Coverage

blib/lib/Prancer/Plugin/Log4perl.pm
Criterion Covered Total %
statement 24 30 80.0
branch 0 2 0.0
condition n/a
subroutine 8 10 80.0
pod 0 2 0.0
total 32 44 72.7


line stmt bran cond sub pod time code
1             package Prancer::Plugin::Log4perl;
2              
3 1     1   15918 use strict;
  1         1  
  1         47  
4 1     1   7 use warnings FATAL => 'all';
  1         2  
  1         48  
5              
6 1     1   420 use version;
  1         1525  
  1         6  
7             our $VERSION = '1.00';
8              
9 1     1   479 use Prancer::Plugin;
  1         16068  
  1         29  
10 1     1   6 use parent qw(Prancer::Plugin Exporter);
  1         1  
  1         2  
11              
12 1     1   761 use Log::Log4perl ();
  1         36338  
  1         27  
13 1     1   6 use Try::Tiny;
  1         1  
  1         93  
14 1     1   4 use Carp;
  1         1  
  1         179  
15              
16             our @EXPORT_OK = qw(logger);
17             our %EXPORT_TAGS = ('all' => [ @EXPORT_OK ]);
18              
19             # even though this *should* work automatically, it was not
20             our @CARP_NOT = qw(Prancer Try::Tiny);
21              
22             sub load {
23 0     0 0   my $class = shift;
24              
25             # initialize the logger if necessary
26 0 0         if (!Log::Log4perl->initialized()) {
27 0           Log::Log4perl->init(\qq|
28             log4perl.rootLogger = INFO, stdout
29             log4perl.appender.stdout = Log::Dispatch::Screen
30             log4perl.appender.stdout.stderr = 0
31             log4perl.appender.stdout.layout = Log::Log4perl::Layout::PatternLayout
32             log4perl.appender.stdout.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS} %5p [%c{1}:%M:%L] - %m%n
33             |);
34             }
35              
36 0           return bless({}, $class);
37             }
38              
39             sub logger {
40 0     0 0   my $package = (caller())[0];
41 0           return Log::Log4perl->get_logger($package);
42             }
43              
44             1;
45              
46             =head1 NAME
47              
48             Prancer::Plugin::Log4perl
49              
50             =head1 SYNOPSIS
51              
52             This plugin connects your L application to L and
53             exports a keyword to access the configured logger. You don't I this
54             module to log things but it certainly makes it easier.
55              
56             There is very minimal configuration required to get started with this module.
57             To enable the logger you only need to do this:
58              
59             use Prancer::Plugin::Log4perl qw(logger);
60              
61             Prancer::Plugin::Log4perl->load();
62              
63             logger->info("hello, logger here");
64             logger->fatal("something done broke");
65              
66             By default, this plugin will initialize L with a very basic
67             configuration to avoid warnings when used. You can override the configuration
68             by loading your own before calling C on this plugin. This plugin's
69             C implementation simply calls Cinitialized()> to see
70             if it should load its own. For example, you might do this:
71              
72             use Prancer::Plugin::Log4perl qw(logger);
73              
74             Log::Log4perl::init('/etc/log4perl.conf');
75             Prancer::Plugin::Log4perl->load();
76              
77             The C keyword gets you direct access to an instance of the logger and
78             you can always call static methods on L and interact with the
79             logger that way, too.
80              
81             =head1 COPYRIGHT
82              
83             Copyright 2014 Paul Lockaby. All rights reserved.
84              
85             This library is free software; you can redistribute it and/or modify it under
86             the same terms as Perl itself.
87              
88             =head1 SEE ALSO
89              
90             =over
91              
92             =item L
93             =item L
94             =item L
95             =item L
96              
97             =back
98              
99             =cut