File Coverage

blib/lib/Log/Any/Adapter/Callback.pm
Criterion Covered Total %
statement 12 30 40.0
branch 0 18 0.0
condition 0 3 0.0
subroutine 4 9 44.4
pod 0 1 0.0
total 16 61 26.2


line stmt bran cond sub pod time code
1             package Log::Any::Adapter::Callback;
2              
3 1     1   23308 use strict;
  1         3  
  1         39  
4 1     1   6 use warnings;
  1         3  
  1         35  
5              
6 1     1   936 use Log::Any::Adapter::Util qw(make_method);
  1         12862  
  1         89  
7 1     1   9 use base qw(Log::Any::Adapter::Base);
  1         2  
  1         1704  
8              
9             our $VERSION = '0.09'; # VERSION
10              
11             my @logging_methods = Log::Any->logging_methods;
12             my %logging_levels;
13             for my $i (0..@logging_methods-1) {
14             $logging_levels{$logging_methods[$i]} = $i;
15             }
16              
17             sub _default_level {
18 0 0 0 0     return $ENV{LOG_LEVEL}
19             if $ENV{LOG_LEVEL} && $logging_levels{$ENV{LOG_LEVEL}};
20 0 0         return 'trace' if $ENV{TRACE};
21 0 0         return 'debug' if $ENV{DEBUG};
22 0 0         return 'info' if $ENV{VERBOSE};
23 0 0         return 'error' if $ENV{QUIET};
24 0           'warning';
25             }
26              
27             my ($logging_cb, $detection_cb);
28             sub init {
29 0     0 0   my ($self) = @_;
30 0 0         $logging_cb = $self->{logging_cb}
31             or die "Please provide logging_cb when initializing ".__PACKAGE__;
32 0 0         if ($self->{detection_cb}) {
33 0           $detection_cb = $self->{detection_cb};
34             } else {
35 0     0     $detection_cb = sub { 1 };
  0            
36             }
37 0 0         if (!defined($self->{min_level})) { $self->{min_level} = _default_level() }
  0            
38             }
39              
40             for my $method (Log::Any->logging_methods()) {
41             make_method(
42             $method,
43             sub {
44 0     0     my $self = shift;
45 0 0         return if $logging_levels{$method} <
46             $logging_levels{ $self->{min_level} };
47 0           $logging_cb->($method, $self, @_);
48             });
49             }
50              
51             for my $method (Log::Any->detection_methods()) {
52             make_method(
53             $method,
54             sub {
55 0     0     $detection_cb->($method, @_);
56             });
57             }
58              
59             1;
60             # ABSTRACT: Send Log::Any logs to a subroutine
61              
62             __END__
63              
64             =pod
65              
66             =encoding UTF-8
67              
68             =head1 NAME
69              
70             Log::Any::Adapter::Callback - Send Log::Any logs to a subroutine
71              
72             =head1 VERSION
73              
74             version 0.09
75              
76             =head1 SYNOPSIS
77              
78             # say, let's POST each log message to an HTTP API server
79             use LWP::UserAgent;
80             my $ua = LWP::UserAgent->new;
81              
82             use Log::Any::Adapter;
83             Log::Any::Adapter->set('Callback',
84             min_level => 'warn',
85             logging_cb => sub {
86             my ($method, $self, $format, @params) = @_;
87             $ua->post("https://localdomain/log", level=>$method, Content=>$format);
88             sleep 1; # don't overload the server
89             },
90             detection_cb => sub { ... }, # optional, default is: sub { 1 }
91             );
92              
93             =head1 DESCRIPTION
94              
95             This adapter lets you specify callback subroutine to be called by Log::Any's
96             logging methods (like $log->debug(), $log->error(), etc) and detection methods
97             (like $log->is_warning(), $log->is_fatal(), etc.).
98              
99             This adapter is used for customized logging, and is mostly a convenient
100             construct to save a few lines of code. You could achieve the same effect by
101             creating a full Log::Any adapter class.
102              
103             Your logging callback subroutine will be called with these arguments:
104              
105             ($method, $self, $format, @params)
106              
107             where $method is the name of method (like "debug") and ($self, $format, @params)
108             are given by Log::Any.
109              
110             =for Pod::Coverage init
111              
112             =head1 SEE ALSO
113              
114             L<Log::Any>
115              
116             =head1 HOMEPAGE
117              
118             Please visit the project's homepage at L<https://metacpan.org/release/Log-Any-Adapter-Callback>.
119              
120             =head1 SOURCE
121              
122             Source repository is at L<https://github.com/sharyanto/perl-Log-Any-Adapter-Callback>.
123              
124             =head1 BUGS
125              
126             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Log-Any-Adapter-Callback>
127              
128             When submitting a bug or request, please include a test-file or a
129             patch to an existing test-file that illustrates the bug or desired
130             feature.
131              
132             =head1 AUTHOR
133              
134             Steven Haryanto <stevenharyanto@gmail.com>
135              
136             =head1 COPYRIGHT AND LICENSE
137              
138             This software is copyright (c) 2014 by Steven Haryanto.
139              
140             This is free software; you can redistribute it and/or modify it under
141             the same terms as the Perl 5 programming language system itself.
142              
143             =cut