File Coverage

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


line stmt bran cond sub pod time code
1             package Mojo::Log::Role::Clearable;
2              
3 2     2   1296 use Role::Tiny;
  2         5  
  2         11  
4              
5             our $VERSION = '1.000';
6              
7             requires 'handle', 'path';
8 6     6 1 3274 sub clear_handle { delete shift->{handle} }
9             before 'path' => sub { $_[0]->clear_handle if @_ > 1 };
10              
11             1;
12              
13             =head1 NAME
14              
15             Mojo::Log::Role::Clearable - Role for Mojo::Log with clearable log handle
16              
17             =head1 SYNOPSIS
18              
19             use Mojo::Log;
20             my $log = Mojo::Log->with_roles('+Clearable')->new(path => $path1);
21             $log->info($message); # Logged to $path1
22             $log->path($path2);
23             $log->debug($message); # Logged to $path2
24             $log->path(undef);
25             $log->warn($message); # Logged to STDERR
26            
27             # Reopen filehandle after logrotate (if logrotate sends SIGUSR1)
28             $SIG{USR1} = sub { $log->clear_handle };
29            
30             # Apply to an existing Mojo::Log object
31             $app->log->with_roles('+Clearable');
32              
33             =head1 DESCRIPTION
34              
35             L is a simple logger class. It holds a filehandle once it writes to
36             a log, and changing L does not open a new filehandle for
37             logging. L is a role that provides a
38             L method and automatically calls it when L
39             is modified, so the logging handle is reopened at the new path. The
40             L method can also be used to reopen the logging handle after
41             logrotate.
42              
43             =head1 ATTRIBUTES
44              
45             L augments the following attributes.
46              
47             =head2 path
48              
49             $log = $log->path('/var/log/mojo.log'); # "handle" is now cleared
50              
51             Log file path as in L. Augmented to call
52             L when modified.
53              
54             =head1 METHODS
55              
56             L composes the following methods.
57              
58             =head2 clear_handle
59              
60             $log->clear_handle;
61              
62             Clears L attribute, it will be reopened from the
63             L 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, L