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   186800 use 5.008001;
  2         6  
2 2     2   10 use strict;
  2         2  
  2         105  
3 2     2   8 use warnings;
  2         2  
  2         183  
4              
5             package Log::Any::Adapter::File;
6              
7             # ABSTRACT: Simple adapter for logging to files
8             our $VERSION = '1.720';
9              
10 2     2   9 use Config;
  2         3  
  2         86  
11 2     2   8 use Fcntl qw/:flock/;
  2         3  
  2         258  
12 2     2   897 use IO::File;
  2         8738  
  2         204  
13 2     2   376 use Log::Any::Adapter::Util ();
  2         4  
  2         59  
14              
15 2     2   841 use Log::Any::Adapter::Base;
  2         5  
  2         633  
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 23 my ( $class, $file, @args ) = @_;
23 9         27 return $class->SUPER::new( file => $file, log_level => $trace_level, @args );
24             }
25              
26             sub init {
27 9     9 0 10 my $self = shift;
28 9 100 66     46 if ( exists $self->{log_level} && $self->{log_level} =~ /\D/ ) {
29 7         18 my $numeric_level = Log::Any::Adapter::Util::numeric_level( $self->{log_level} );
30 7 100       13 if ( !defined($numeric_level) ) {
31 1         6 require Carp;
32 1         342 Carp::carp( sprintf 'Invalid log level "%s". Defaulting to "%s"', $self->{log_level}, 'trace' );
33             }
34 7         17 $self->{log_level} = $numeric_level;
35             }
36 9 100       22 if ( !defined $self->{log_level} ) {
37 1         2 $self->{log_level} = $trace_level;
38             }
39 9         10 my $file = $self->{file};
40 9   100     22 my $binmode = $self->{binmode} || ':utf8';
41 9 100       22 $binmode = ":$binmode" unless substr($binmode,0,1) eq ':';
42 9 50       663 open( $self->{fh}, ">>$binmode", $file )
43             or die "cannot open '$file' for append: $!";
44 9         72 $self->{fh}->autoflush(1);
45             }
46              
47             foreach my $method ( Log::Any::Adapter::Util::logging_methods() ) {
48 2     2   12 no strict 'refs';
  2         15  
  2         282  
49             my $method_level = Log::Any::Adapter::Util::numeric_level( $method );
50             *{$method} = sub {
51 3     3   5 my ( $self, $text ) = @_;
52 3 50       6 return if $method_level > $self->{log_level};
53 3         85 my $msg = sprintf( "[%s] %s\n", scalar(localtime), $text );
54 3 50       28 flock($self->{fh}, LOCK_EX) if $HAS_FLOCK;
55 3         25 $self->{fh}->print($msg);
56 3 50       167 flock($self->{fh}, LOCK_UN) if $HAS_FLOCK;
57             }
58             }
59              
60             foreach my $method ( Log::Any::Adapter::Util::detection_methods() ) {
61 2     2   9 no strict 'refs';
  2         8  
  2         233  
62             my $base = substr($method,3);
63             my $method_level = Log::Any::Adapter::Util::numeric_level( $base );
64             *{$method} = sub {
65 10     10   40 return !!( $method_level <= $_[0]->{log_level} );
66             };
67             }
68              
69             1;
70              
71             __END__