File Coverage

blib/lib/Object/PadX/Log/Log4perl.pm
Criterion Covered Total %
statement 17 18 94.4
branch 5 6 83.3
condition 2 3 66.6
subroutine 7 7 100.0
pod 2 2 100.0
total 33 36 91.6


line stmt bran cond sub pod time code
1             package Object::PadX::Log::Log4perl;
2             our $VERSION = '0.001';
3 1     1   182845 use v5.26;
  1         12  
4 1     1   5 use Object::Pad;
  1         2  
  1         7  
5              
6             # ABSTRACT: A logger role for Object::Pad based classes based on Log::Log4perl
7              
8             role Object::PadX::Log::Log4perl;
9 1     1   448 use Log::Log4perl;
  1         2  
  1         6  
10              
11             field $logger;
12              
13 15     15 1 29 method logger {
        15      
14 15 100       63 return $logger if $logger;
15 2         10 $logger = Log::Log4perl->get_logger(ref($self));
16 2         428 return $logger;
17             }
18              
19 17     17 1 2330 method log {
        17      
20 17         29 my $cat = shift;
21 17 50 66     65 if ($cat && $cat =~ m/^(\.|::)/) {
    100          
22 0         0 return Log::Log4perl->get_logger(ref($self) . $cat);
23             }
24             elsif ($cat) {
25 2         9 return Log::Log4perl->get_logger($cat);
26             }
27             else {
28 15         34 return $self->logger;
29             }
30             }
31              
32             1;
33              
34             __END__
35              
36             =pod
37              
38             =encoding UTF-8
39              
40             =head1 NAME
41              
42             Object::PadX::Log::Log4perl - A logger role for Object::Pad based classes based on Log::Log4perl
43              
44             =head1 VERSION
45              
46             version 0.001
47              
48             =head1 SYNOPSIS
49              
50             package MyClass;
51             use v5.26;
52             use Object::Pad;
53              
54             class MyClass :does(Object::PadX::Log::Log4perl)
55              
56             method foo {
57             $self->log->info("Foo called");
58             }
59              
60             =head1 DESCRIPTION
61              
62             A logging role building a very lightweight wrapper to L<Log::Log4perl> for use
63             with your L<Object::Pad> classes. The initialization of the Log4perl instance
64             must be performed prior to logging the first log message. Otherwise the
65             default initialization will happen, probably not doing the things you expect.
66              
67             The logger needs to be setup before using the logger, which could happen in the
68             main application:
69              
70             package main;
71             use Log::Log4perl qw(:easy);
72             use MyClass;
73              
74             BEGIN { Log::Log4perl->easy_init() }
75              
76             my $myclass = MyClass->new();
77             $myclass->log->info("In my class"); # Access the log of the object
78             $myclass->dummy; # Will log "Dummy log entry"
79              
80             Using the logger within a class is as simple as consuming a role:
81              
82             =head1 METHODS
83              
84             =head2 logger
85              
86             The C<logger> attribute holds the L<Log::Log4perl> object that implements all
87             logging methods for the defined log levels, such as C<debug> or C<error>.
88              
89             =head2 log
90              
91             Basically the same as logger, but also allowing to change the log category for
92             this log message.
93              
94             if ($myapp->log->is_debug()) {
95             $myapp->log->debug("Woot"); # category is class myapp
96             }
97              
98             $myapp->log("FooBar")->info("Foobar"); # category FooBar
99             $myapp->log->info("Yihaa"); # category class again myapp
100             $myapp->log(".FooBar")->info("Foobar"); # category myapp.FooBar
101             $myapp->log("::FooBar")->info("Foobar"); # category myapp.FooBar
102              
103             =head1 PRIOR ART
104              
105             This code has been mostly ported/inspired from L<MooseX::Log::Log4perl>.
106             Copyright (c) 2008-2016, Roland Lammel <lammel@cpan.org>, http://www.quikit.at
107              
108             =head1 AUTHOR
109              
110             Wesley Schwengle <waterkip@cpan.org>
111              
112             =head1 COPYRIGHT AND LICENSE
113              
114             This software is copyright (c) 2023 by Wesley Schwengle.
115              
116             This is free software; you can redistribute it and/or modify it under
117             the same terms as the Perl 5 programming language system itself.
118              
119             =cut