File Coverage

blib/lib/Mojo/Log/Clearable.pm
Criterion Covered Total %
statement 7 7 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod 1 1 100.0
total 11 11 100.0


line stmt bran cond sub pod time code
1             package Mojo::Log::Clearable;
2              
3 1     1   32979 use Mojo::Base 'Mojo::Log';
  1         2  
  1         7  
4 1     1   83408 use Class::Method::Modifiers ();
  1         1078  
  1         95  
5              
6             our $VERSION = '0.003';
7              
8 3     3 1 1740 sub clear_handle { delete shift->{handle} };
9             Class::Method::Modifiers::before 'path' => sub { $_[0]->clear_handle if @_ > 1 };
10              
11             =head1 NAME
12              
13             Mojo::Log::Clearable - Mojo::Log with clearable log handle
14              
15             =head1 SYNOPSIS
16              
17             use Mojo::Log::Clearable;
18             my $log = Mojo::Log::Clearable->new(path => $path1);
19             $log->info($message); # Logged to $path1
20             $log->path($path2);
21             $log->debug($message); # Logged to $path2
22             $log->path(undef);
23             $log->warn($message); # Logged to STDERR
24            
25             # Reopen filehandle after logrotate (if logrotate sends SIGUSR1)
26             $SIG{USR1} = sub { $log->clear_handle };
27              
28             =head1 DESCRIPTION
29              
30             L is a simple logger class. It holds a filehandle once it writes to
31             a log, and changing L does not open a new filehandle for logging.
32             L subclasses L to provide a L
33             method and to automatically call it when setting L so the logging
34             handle is reopened at the new path. The L method can also be
35             used to reopen the logging handle after logrotate.
36              
37             =head1 EVENTS
38              
39             L inherits all events from L.
40              
41             =head1 ATTRIBUTES
42              
43             L inherits all attributes from L and
44             implements the following new ones.
45              
46             =head2 path
47              
48             my $path = $log->path;
49             $log = $log->path('/var/log/mojo.log');
50              
51             Log file path used by L. Reopens the handle when set.
52              
53             =head1 METHODS
54              
55             L inherits all methods from L and implements
56             the following new ones.
57              
58             =head2 clear_handle
59              
60             $log->clear_handle;
61              
62             Clears L attribute, it will be reopened from the L
63             attribute when next accessed.
64              
65             =head1 AUTHOR
66              
67             Dan Book, C
68              
69             =head1 COPYRIGHT AND LICENSE
70              
71             Copyright 2015, Dan Book.
72              
73             This library is free software; you may redistribute it and/or modify it undef
74             the terms of the Artistic License version 2.0.
75              
76             =head1 SEE ALSO
77              
78             L
79              
80             =cut
81              
82             1;