File Coverage

blib/lib/OTRS/OPM/Installer/Logger.pm
Criterion Covered Total %
statement 37 37 100.0
branch 2 2 100.0
condition n/a
subroutine 10 10 100.0
pod 0 2 0.0
total 49 51 96.0


line stmt bran cond sub pod time code
1             package OTRS::OPM::Installer::Logger;
2             $OTRS::OPM::Installer::Logger::VERSION = '0.02';
3             # ABSTRACT: A simple logger for OTRS::OPM::Installer
4              
5 12     12   249569 use strict;
  12         60  
  12         330  
6 12     12   61 use warnings;
  12         29  
  12         301  
7              
8 12     12   1214 use Moo;
  12         37821  
  12         81  
9 12     12   7459 use IO::All;
  12         42866  
  12         122  
10 12     12   3080 use File::Temp;
  12         55647  
  12         940  
11 12     12   3977 use Time::Piece;
  12         90430  
  12         846  
12              
13             my $file = File::Temp->new->filename;
14              
15             has log => ( is => 'ro', lazy => 1, default => sub { $file } );
16              
17             for my $level (qw/notice info debug warn error/) {
18 12     12   2361 no strict 'refs';
  12         32  
  12         3341  
19             *{"OTRS::OPM::Installer::Logger::$level"} = sub {
20 4     4   31127 shift->print( $level, @_ );
21             };
22             }
23              
24             sub print {
25 4     4 0 27 my ($self, $tag, %attr) = @_;
26              
27             my $attrs = join " ", map{
28 4         41 my $escaped = $attr{$_};
  8         24  
29 8         24 $escaped =~ s{\\}{\\\\}g;
30 8         23 $escaped =~ s{"}{\\"}g;
31              
32 8         46 sprintf '%s="%s"', $_, $escaped
33             }sort keys %attr;
34              
35 4         25 my $date = localtime;
36 4         322 my $message = sprintf "[%s] [%s %s] %s \n", uc $tag, $date->ymd, $date->hms, $attrs;
37 4         282 $message >> io $self->log;
38             }
39              
40             sub BUILD {
41 3     3 0 6681 my ($self, $param) = @_;
42              
43 3 100       17 if ( $param->{path} ) {
44 1         4 $file = $param->{path};
45             }
46              
47 3         17 my $date = localtime;
48 3         269 my $message = sprintf "[DEBUG] [%s %s] Start installation...\n", $date->ymd, $date->hms;
49              
50 3         336 $message > io $self->log
51             }
52            
53             1;
54              
55             __END__
56              
57             =pod
58              
59             =encoding UTF-8
60              
61             =head1 NAME
62              
63             OTRS::OPM::Installer::Logger - A simple logger for OTRS::OPM::Installer
64              
65             =head1 VERSION
66              
67             version 0.02
68              
69             =head1 SYNOPSIS
70              
71             use OTRS::OPM::Installer::Logger;
72            
73             my $logger = OTRS::OPM::Installer::Logger->new; # creates a new temporary file
74            
75             # or
76             my $logger = OTRS::OPM::Installer::Logger->new(
77             path => 'my_otrs_installer.log',
78             );
79              
80             $logger->debug( message => 'message' );
81             $logger->error( type => 'cpan', message => 'Cannot install module' );
82             $logger->notice( message => 'test' );
83             $logger->info( any_key => 'a value' );
84             $logger->warn( module => 'test', text => 'a warning' );
85              
86             =head1 AUTHOR
87              
88             Renee Baecker <reneeb@cpan.org>
89              
90             =head1 COPYRIGHT AND LICENSE
91              
92             This software is Copyright (c) 2017 by Renee Baecker.
93              
94             This is free software, licensed under:
95              
96             The Artistic License 2.0 (GPL Compatible)
97              
98             =cut