File Coverage

blib/lib/Binance/API/Logger.pm
Criterion Covered Total %
statement 27 28 96.4
branch 7 8 87.5
condition 4 8 50.0
subroutine 6 6 100.0
pod 1 1 100.0
total 45 51 88.2


line stmt bran cond sub pod time code
1             package Binance::API::Logger;
2              
3             # MIT License
4             #
5             # Copyright (c) 2017 Lari Taskula
6             #
7             # Permission is hereby granted, free of charge, to any person obtaining a copy
8             # of this software and associated documentation files (the "Software"), to deal
9             # in the Software without restriction, including without limitation the rights
10             # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11             # copies of the Software, and to permit persons to whom the Software is
12             # furnished to do so, subject to the following conditions:
13             #
14             # The above copyright notice and this permission notice shall be included in all
15             # copies or substantial portions of the Software.
16             #
17             # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18             # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19             # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20             # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21             # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22             # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23             # SOFTWARE.
24              
25 3     3   91881 use strict;
  3         16  
  3         88  
26 3     3   16 use warnings;
  3         5  
  3         73  
27              
28 3     3   15 use Carp;
  3         13  
  3         182  
29              
30 3     3   1345 use Binance::Constants qw( :all );
  3         7  
  3         1269  
31              
32             =head1 NAME
33              
34             Binance::API::Logger -- Logger for L
35              
36             =head1 DESCRIPTION
37              
38             Provides a wrapper for your desired logger. Carps log calls higher than "debug"
39             level
40              
41             =head1 SYNOPSIS
42              
43             use Binance::API;
44              
45             my $logger = Binance::API::Logger->new($log4perl);
46              
47             $logger->warn("This is a warning");
48              
49             =head1 METHODS
50              
51             =cut
52              
53             =head2 new
54              
55             my $logger = Binance::API::Logger->new($log4perl);
56              
57             Instantiates a new C object.
58              
59             B
60              
61             =over
62              
63             =item A logger object that implements (at least) debug, warn,
64             error, fatal level logging.
65              
66             [OPTIONAL]
67              
68             =back
69              
70             B
71              
72             A C object.
73              
74             =cut
75              
76             sub new {
77 5     5 1 6523 my $class = shift;
78              
79 5         16 my $self = {
80             logger => undef,
81             };
82              
83 5 100       22 $self->{logger} = shift if @_;
84              
85 5         27 bless $self, $class;
86             }
87              
88             sub AUTOLOAD {
89 56     56   4263 my $self = shift;
90              
91 56         126 my $level = our $AUTOLOAD;
92 56         365 $level =~ s/.*://;
93              
94 56 100       411 return if $level eq 'DESTROY';
95              
96 51   50     217 my $message = shift || "";
97              
98 51         254 my $sub = (caller(1))[3];
99 51         1228 my $full_message = "[$sub] ". $message;
100              
101 51 100 66     428 if ($level eq 'debug' || $level eq 'trace') {
102 14         21 carp $full_message if DEBUG;
103             } else {
104 37         722 carp $full_message;
105             }
106              
107 51 50 33     11874 if ($self->{logger} && $self->{logger}->can($level)) {
108 0           $self->{logger}->$level($full_message);
109             }
110             }
111              
112             1;