File Coverage

blib/lib/Log/Any/Adapter/Fille.pm
Criterion Covered Total %
statement 39 47 82.9
branch 0 6 0.0
condition n/a
subroutine 13 15 86.6
pod n/a
total 52 68 76.4


line stmt bran cond sub pod time code
1             package Log::Any::Adapter::Fille;
2              
3             #
4             # Advanced adapter for logging to files
5             #
6              
7 1     1   29104 use 5.008001;
  1         4  
  1         53  
8 1     1   7 use strict;
  1         2  
  1         60  
9 1     1   8 use warnings;
  1         6  
  1         45  
10 1     1   642 use utf8::all;
  1         56186  
  1         6  
11              
12 1     1   3164 use Config;
  1         1  
  1         41  
13 1     1   5 use Fcntl qw(:flock);
  1         1  
  1         129  
14 1     1   781 use POSIX;
  1         7099  
  1         10  
15 1     1   3498 use IO::File;
  1         9604  
  1         421  
16 1     1   648 use Time::HiRes qw(gettimeofday);
  1         1797  
  1         5  
17 1     1   1872 use Log::Any::Adapter::Util ();
  1         10357  
  1         42  
18              
19 1     1   14 use base qw/Log::Any::Adapter::Base/;
  1         2  
  1         992  
20              
21             our $VERSION = '0.03';
22              
23             #---
24              
25             # Log levels (names satisfy the official log names Log::Any)
26             my %levels = (
27             0 => 'EMERGENCY',
28             1 => 'ALERT',
29             2 => 'CRITICAL',
30             3 => 'ERROR',
31             4 => 'WARNING',
32             5 => 'NOTICE',
33             6 => 'INFO',
34             7 => 'DEBUG',
35             8 => 'TRACE',
36             );
37              
38             my $HAS_FLOCK = $Config{d_flock} || $Config{d_fcntl_can_lock} || $Config{d_lockf};
39              
40             sub new {
41 0     0     my ( $class, @args ) = @_;
42              
43 0           return $class->SUPER::new(@args);
44             }
45              
46             sub init {
47 0     0     my $self = shift;
48              
49 0 0         if ( exists $self->{log_level} ) {
50 0 0         $self->{log_level} = Log::Any::Adapter::Util::numeric_level( $self->{log_level} )
51             unless $self->{log_level} =~ /^\d+$/;
52             }
53             else {
54 0           $self->{log_level} = Log::Any::Adapter::Util::numeric_level('info');
55             }
56              
57 0 0         open( $self->{fh}, ">>", $self->{file} ) or die "Не удалось открыть файл '$self->{file}': $!";
58 0           $self->{fh}->autoflush(1);
59             }
60              
61             foreach my $method ( Log::Any::Adapter::Util::logging_methods() ) {
62 1     1   4868 no strict 'refs'; ## no critic (ProhibitNoStrict)
  1         2  
  1         311  
63              
64             my $method_level = Log::Any::Adapter::Util::numeric_level($method);
65              
66             *{$method} = sub {
67             my ( $self, $text ) = @_;
68              
69             return if $method_level > $self->{log_level};
70              
71             my ( $sec, $msec ) = gettimeofday;
72              
73             # Log line in "date time pid level message" format
74             my $msg = sprintf( "%s.%.6d %5d %s %s\n", strftime( "%Y-%m-%d %H:%M:%S", localtime($sec) ), $msec, $$, $levels{$method_level}, $text );
75              
76             flock( $self->{fh}, LOCK_EX ) if $HAS_FLOCK;
77             $self->{fh}->print($msg);
78             flock( $self->{fh}, LOCK_UN ) if $HAS_FLOCK;
79             }
80             }
81              
82             foreach my $method ( Log::Any::Adapter::Util::detection_methods() ) {
83 1     1   5 no strict 'refs'; ## no critic (ProhibitNoStrict)
  1         2  
  1         134  
84              
85             my $base = substr( $method, 3 );
86              
87             my $method_level = Log::Any::Adapter::Util::numeric_level($base);
88              
89             *{$method} = sub {
90             return !!( $method_level <= $_[0]->{log_level} );
91             }
92             }
93              
94             1;
95              
96             __END__