| 1 | package Log::LTSV::Instance; | ||||||||||||||||||||
| 2 | 2 | ▶ | use 5.008001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use Time::Piece; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use File::RotateLogs; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use Log::LTSV::Instance::Flatten; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 2 | our $VERSION = "0.06"; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | my %LOG_LEVEL_MAP = ( | |||||||||||||||||||
| 14 | DEBUG => 1, | ||||||||||||||||||||
| 15 | INFO => 2, | ||||||||||||||||||||
| 16 | WARN => 3, | ||||||||||||||||||||
| 17 | CRITICAL => 4, | ||||||||||||||||||||
| 18 | ERROR => 99, | ||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 14 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=23.2
Pod
documented
| |||||||||||||||||||||
| 22 | 14 | my ($class, %args) = @_; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 14 | ▶ | my $level = $LOG_LEVEL_MAP{$args{level} || 'DEBUG'}; | ||||||||||||||||||
|
Condition100%2/2
$args{'level'} || 'DEBUG'
MC/DC100%1/1
$args{'level'} || 'DEBUG'
$args{'level'}
Truth table100%2/2
A
A= $args{'level'}
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 14 | ▶ | Carp::croak("level required ERROR or CRITICAL or WARN or INFO or DEBUG") unless $level; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 14 | my ($logger, $rotatelogs); | |||||||||||||||||||
| 29 | 14 | ▶ | if ($args{logger}) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 30 | 13 | $logger = $args{logger}; | |||||||||||||||||||
| 31 | } elsif (not defined $args{logfile}) { | ||||||||||||||||||||
| 32 | 0 | ▶ | $logger = sub { print @_ }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 33 | } else { | ||||||||||||||||||||
| 34 | my $rotatelogs = File::RotateLogs->new( | ||||||||||||||||||||
| 35 | logfile => $args{logfile}, | ||||||||||||||||||||
| 36 | defined $args{maxage} ? ( maxage => $args{maxage} ) : (), | ||||||||||||||||||||
| 37 | $args{linkname} ? ( linkname => $args{linkname} ) : (), | ||||||||||||||||||||
| 38 | 1 | ▶ | $args{rotationtime} ? ( rotationtime => $args{rotationtime} ) : (), | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||
| 40 | 1 | ▶ | $logger = sub { $rotatelogs->print(@_) }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 14 | my $flatten = Log::LTSV::Instance::Flatten->new; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | bless { | ||||||||||||||||||||
| 46 | rotatelogs => $rotatelogs, | ||||||||||||||||||||
| 47 | logger => $logger, | ||||||||||||||||||||
| 48 | level => $level, | ||||||||||||||||||||
| 49 | sticks => {}, | ||||||||||||||||||||
| 50 | 14 | ▶ | default_key => $args{default_key} || 'message', | ||||||||||||||||||
|
Condition100%2/2
$args{'default_key'} || 'message'
MC/DC100%1/1
$args{'default_key'} || 'message'
$args{'default_key'}
Truth table100%2/2
A
A= $args{'default_key'}
| |||||||||||||||||||||
| 51 | _flatten => $flatten, | ||||||||||||||||||||
| 52 | }, $class; | ||||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | ▶ | sub error { shift->print('ERROR', @_) } | ||||||||||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 56 | 10 | ▶ | sub crit { shift->print('CRITICAL', @_) } | ||||||||||||||||||
|
Subroutine
crit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 57 | 0 | ▶ | sub warn { shift->print('WARN', @_) } | ||||||||||||||||||
|
Subroutine
warn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 58 | 0 | ▶ | sub info { shift->print('INFO', @_) } | ||||||||||||||||||
|
Subroutine
info: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 59 | 2 | ▶ | sub debug { shift->print('DEBUG', @_) } | ||||||||||||||||||
|
Subroutine
debug: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 3 | ▶ | sub sticks { | ||||||||||||||||||
|
Subroutine
sticks: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 62 | 3 | my ($self, @args) = @_; | |||||||||||||||||||
| 63 | 3 | ▶ | while (@args) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 3 | my ($key, $value) = splice @args, 0, 2; | |||||||||||||||||||
| 65 | 3 | $self->{sticks}{$key} = $value; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 18 | ▶ | sub _escape { | ||||||||||||||||||
|
Subroutine
_escape: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 70 | 18 | my ($self, $val) = @_; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 18 | $val =~ s/\t/\\t/g; | |||||||||||||||||||
| 73 | 18 | $val =~ s/\n/\\n/g; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 18 | return $val; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 16 | ▶ | sub labeled_values { | ||||||||||||||||||
|
Subroutine
labeled_values: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 79 | 16 | my ($self, $key, $value) = @_; | |||||||||||||||||||
| 80 | 16 | my %lv = $self->{_flatten}->flatten($key, $value); | |||||||||||||||||||
| 81 | 16 | $lv{$_} = $self->_escape($lv{$_}) for ( keys %lv ); | |||||||||||||||||||
| 82 | 16 | map { join ':', $_, $lv{$_} } keys %lv; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 12 | ▶ | sub print { | ||||||||||||||||||
|
Subroutine
print: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||
| 86 | 12 | my ($self, $level, @args) = @_; | |||||||||||||||||||
| 87 | 12 | ▶ | return if ($LOG_LEVEL_MAP{$level} < $self->{level}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 11 | ▶ | if (ref $args[0] eq 'HASH') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 90 | 1 | @args = %{ $args[0] }; | |||||||||||||||||||
| 91 | ▶ | } elsif ( scalar @args == 1 && ref $args[0] eq '' ) { | |||||||||||||||||||
|
Condition66%2/3
scalar @args == 1 and ref $args[0] eq ""
MC/DC50%1/2
scalar @args == 1 and ref $args[0] eq ""
scalar @args == 1
ref $args[0] eq ""
Truth table66%2/3
A and B
A= scalar @args == 1B= ref $args[0] eq ""
| |||||||||||||||||||||
| 92 | 2 | @args = ( $self->{default_key} => $args[0] ); | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 11 | my @msgs; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 11 | push @msgs, sprintf("time:%s", localtime->datetime); | |||||||||||||||||||
| 98 | 11 | push @msgs, "log_level:$level"; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 11 | for my $key (keys %{ $self->{sticks} }) { | |||||||||||||||||||
| 101 | 3 | my $value = $self->{sticks}->{$key}; | |||||||||||||||||||
| 102 | 3 | ▶ | $value = $value->() if ref $value; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 3 | push @msgs, $self->labeled_values($key, $value); | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 11 | ▶ | while (@args) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 13 | my ($key, $value) = splice @args, 0, 2; | |||||||||||||||||||
| 108 | 13 | push @msgs, $self->labeled_values($key, $value); | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | 11 | my $ltsv = join "\t", @msgs; | |||||||||||||||||||
| 111 | 11 | $self->{logger}->($ltsv."\n"); | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 2 | 1; | |||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||