File Coverage

blib/lib/Log/Any/Adapter/File.pm
Criterion Covered Total %
statement 52 52 100.0
branch 12 16 75.0
condition 4 5 80.0
subroutine 14 14 100.0
pod 0 2 0.0
total 82 89 92.1


line stmt bran cond sub pod time code
1 2     2   141934 use 5.008001;
  2         7  
2 2     2   8 use strict;
  2         3  
  2         50  
3 2     2   8 use warnings;
  2         3  
  2         216  
4              
5             package Log::Any::Adapter::File;
6              
7             # ABSTRACT: Simple adapter for logging to files
8             our $VERSION = '1.718';
9              
10 2     2   12 use Config;
  2         4  
  2         85  
11 2     2   9 use Fcntl qw/:flock/;
  2         3  
  2         332  
12 2     2   1059 use IO::File;
  2         8257  
  2         255  
13 2     2   408 use Log::Any::Adapter::Util ();
  2         4  
  2         46  
14              
15 2     2   1075 use Log::Any::Adapter::Base;
  2         5  
  2         902  
16             our @ISA = qw/Log::Any::Adapter::Base/;
17              
18             my $HAS_FLOCK = $Config{d_flock} || $Config{d_fcntl_can_lock} || $Config{d_lockf};
19              
20             my $trace_level = Log::Any::Adapter::Util::numeric_level('trace');
21             sub new {
22 9     9 0 32 my ( $class, $file, @args ) = @_;
23 9         39 return $class->SUPER::new( file => $file, log_level => $trace_level, @args );
24             }
25              
26             sub init {
27 9     9 0 15 my $self = shift;
28 9 100 66     68 if ( exists $self->{log_level} && $self->{log_level} =~ /\D/ ) {
29 7         26 my $numeric_level = Log::Any::Adapter::Util::numeric_level( $self->{log_level} );
30 7 100       19 if ( !defined($numeric_level) ) {
31 1         10 require Carp;
32 1         344 Carp::carp( sprintf 'Invalid log level "%s". Defaulting to "%s"', $self->{log_level}, 'trace' );
33             }
34 7         24 $self->{log_level} = $numeric_level;
35             }
36 9 100       27 if ( !defined $self->{log_level} ) {
37 1         3 $self->{log_level} = $trace_level;
38             }
39 9         17 my $file = $self->{file};
40 9   100     33 my $binmode = $self->{binmode} || ':utf8';
41 9 100       32 $binmode = ":$binmode" unless substr($binmode,0,1) eq ':';
42 9 50       857 open( $self->{fh}, ">>$binmode", $file )
43             or die "cannot open '$file' for append: $!";
44 9         71 $self->{fh}->autoflush(1);
45             }
46              
47             foreach my $method ( Log::Any::Adapter::Util::logging_methods() ) {
48 2     2   15 no strict 'refs';
  2         3  
  2         384  
49             my $method_level = Log::Any::Adapter::Util::numeric_level( $method );
50             *{$method} = sub {
51 3     3   7 my ( $self, $text ) = @_;
52 3 50       11 return if $method_level > $self->{log_level};
53 3         148 my $msg = sprintf( "[%s] %s\n", scalar(localtime), $text );
54 3 50       46 flock($self->{fh}, LOCK_EX) if $HAS_FLOCK;
55 3         25 $self->{fh}->print($msg);
56 3 50       240 flock($self->{fh}, LOCK_UN) if $HAS_FLOCK;
57             }
58             }
59              
60             foreach my $method ( Log::Any::Adapter::Util::detection_methods() ) {
61 2     2   12 no strict 'refs';
  2         4  
  2         265  
62             my $base = substr($method,3);
63             my $method_level = Log::Any::Adapter::Util::numeric_level( $base );
64             *{$method} = sub {
65 10     10   80 return !!( $method_level <= $_[0]->{log_level} );
66             };
67             }
68              
69             1;
70              
71             __END__