Mojo::Log::Role::Clearable - Role for Mojo::Log with clearable log handle
use Mojo::Log; my $log = Mojo::Log->with_roles('+Clearable')->new(path => $path1); $log->info($message); # Logged to $path1 $log->path($path2); $log->debug($message); # Logged to $path2 $log->path(undef); $log->warn($message); # Logged to STDERR # Reopen filehandle after logrotate (if logrotate sends SIGUSR1) $SIG{USR1} = sub { $log->clear_handle }; # Apply to an existing Mojo::Log object $app->log->with_roles('+Clearable');
Mojo::Log is a simple logger class. It holds a filehandle once it writes to a log, and changing "path" in Mojo::Log does not open a new filehandle for logging. Mojo::Log::Role::Clearable is a role that provides a "clear_handle" method and automatically calls it when "path" in Mojo::Log is modified, so the logging handle is reopened at the new path. The "clear_handle" method can also be used to reopen the logging handle after logrotate.
Mojo::Log::Role::Clearable augments the following attributes.
$log = $log->path('/var/log/mojo.log'); # "handle" is now cleared
Log file path as in "path" in Mojo::Log. Augmented to call "clear_handle" when modified.
Mojo::Log::Role::Clearable composes the following methods.
$log->clear_handle;
Clears "handle" in Mojo::Log attribute, it will be reopened from the "path" in Mojo::Log attribute when next accessed.
Dan Book, dbook@cpan.org
dbook@cpan.org
Copyright 2015, Dan Book.
This library is free software; you may redistribute it and/or modify it undef the terms of the Artistic License version 2.0.
Mojo::Log, Mojo::Log::Clearable
To install Mojo::Log::Clearable, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::Log::Clearable
CPAN shell
perl -MCPAN -e shell install Mojo::Log::Clearable
For more information on module installation, please visit the detailed CPAN module installation guide.