File Coverage

blib/lib/App/Sv/Log.pm
Criterion Covered Total %
statement 15 39 38.4
branch 0 12 0.0
condition 0 5 0.0
subroutine 5 11 45.4
pod 0 1 0.0
total 20 68 29.4


line stmt bran cond sub pod time code
1             package App::Sv::Log;
2              
3 1     1   7 use strict;
  1         2  
  1         46  
4 1     1   8 use warnings;
  1         2  
  1         38  
5              
6 1     1   7 use Carp 'croak';
  1         1  
  1         69  
7 1     1   5 use POSIX;
  1         2  
  1         8  
8 1     1   3743 use AnyEvent::Log;
  1         4047  
  1         336  
9              
10             # Loggers
11              
12             sub new {
13 0     0 0   my ($class, $self) = @_;
14            
15 0 0         $self = ref $self eq 'HASH' ? $self : {};
16 0           bless $self, $class;
17            
18 0           return $self->_logger();
19             }
20              
21             sub _logger {
22 0     0     my $self = shift;
23            
24 0           my $ctx; $ctx = AnyEvent::Log::Ctx->new(
25             title => 'app-sv',
26 0     0     fmt_cb => sub { $self->_log_format(@_) }
27 0           );
28            
29             # set output
30 0 0 0       if ($self->{file}) {
    0          
    0          
31 0           $ctx->log_to_file($self->{file});
32             }
33             elsif (-t \*STDOUT && -t \*STDIN) {
34 0     0     $ctx->log_cb(sub { print @_ });
  0            
35             }
36             elsif (-t \*STDERR) {
37 0     0     $ctx->log_cb(sub { print STDERR @_ });
  0            
38             }
39            
40             # set log level
41 0 0         if ($ENV{SV_DEBUG}) {
    0          
42 0           $ctx->level(8);
43             }
44             elsif ($self->{level}) {
45 0           $ctx->level($self->{level});
46             }
47             else {
48 0           $ctx->level(5);
49             }
50            
51 0           return $ctx;
52             }
53              
54             sub _log_format {
55 0     0     my ($self, $ts, $ctx, $lvl, $msg) = @_;
56            
57 0   0       my $ts_fmt = $self->{ts_format} || "%Y-%m-%dT%H:%M:%S%z";
58 0           my @levels = qw(0 fatal alert crit error warn note info debug trace);
59 0           $ts = POSIX::strftime($ts_fmt, localtime((int $ts)[0]));
60            
61 0           return "$ts $levels[$lvl] $$ $msg\n"
62             }
63              
64             1;