Skip to content

Commit

Permalink
added simple log rotation support with USR1 signal to Hypnotoad
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 31, 2012
1 parent 759f012 commit de05199
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.71 2012-03-31
2.71 2012-04-01
- Added simple log rotation support with USR1 signal to Hypnotoad.
- Added close method to Mojo::Log.
- Improved Hypnotoad error handling.
- Improved documentation.
- Improved tests.
Expand Down
12 changes: 10 additions & 2 deletions lib/Mojo/Log.pm
Expand Up @@ -44,6 +44,8 @@ sub new {
return $self;
}

sub close { delete shift->{handle} }

# "Yes, I got the most! I win X-Mas!"
sub debug { shift->log(debug => @_) }
sub error { shift->log(error => @_) }
Expand Down Expand Up @@ -139,7 +141,7 @@ L<Mojo::Log> implements the following attributes.
my $handle = $log->handle;
$log = $log->handle(IO::File->new);
Logfile handle used by default C<message> event, defaults to opening the
Log file handle used by default C<message> event, defaults to opening the
value of C<path> or C<STDERR>.
=head2 C<level>
Expand Down Expand Up @@ -171,7 +173,7 @@ These levels are currently available:
my $path = $log->path
$log = $log->path('/var/log/mojo.log');
Logfile path used by C<handle>.
Log file path used by C<handle>.
=head1 METHODS
Expand All @@ -185,6 +187,12 @@ the following new ones.
Construct a new L<Mojo::Log> object and subscribe to C<message> event with
default logger.
=head2 C<close>
$log->close;
Close C<handle>, useful for log rotation.
=head2 C<debug>
$log = $log->debug('You screwed up, but that is ok');
Expand Down
13 changes: 13 additions & 0 deletions lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -105,6 +105,10 @@ sub run {
while ((my $pid = waitpid -1, WNOHANG) > 0) { $self->_reap($pid) }
};
$SIG{QUIT} = sub { $self->{finished} = $self->{graceful} = 1 };
$SIG{USR1} = sub {
$log->info('Reopening log file.')->close;
kill 'USR1', $_ for keys %{$self->{workers}};
};
$SIG{USR2} = sub { $self->{upgrade} ||= time };
$SIG{TTIN} = sub { $c->{workers}++ };
$SIG{TTOU} = sub {
Expand Down Expand Up @@ -351,6 +355,7 @@ sub _spawn {
# Clean worker environment
$SIG{INT} = $SIG{TERM} = $SIG{CHLD} = $SIG{USR2} = $SIG{TTIN} = $SIG{TTOU} =
'DEFAULT';
$SIG{USR1} = sub { $self->{daemon}->app->log->close };
$SIG{QUIT} = sub { $loop->max_connections(0) };
delete $self->{reader};
delete $self->{poll};
Expand Down Expand Up @@ -433,6 +438,10 @@ Increase worker pool by one.
Decrease worker pool by one.
=item C<USR1>
Reopen log file, useful for log rotation.
=item C<USR2>
Attempt zero downtime software upgrade (hot deployment) without losing any
Expand Down Expand Up @@ -466,6 +475,10 @@ Stop worker immediately.
Stop worker gracefully.
=item C<USR1>
Reopen log file, useful for log rotation.
=back
=head1 SETTINGS
Expand Down
6 changes: 5 additions & 1 deletion t/mojo/log.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

use Test::More tests => 45;
use Test::More tests => 48;

# "Don't let Krusty's death get you down, boy.
# People die all the time, just like that.
Expand All @@ -14,6 +14,10 @@ use Mojo::Log;
my $dir = File::Temp::tempdir(CLEANUP => 1);
my $path = catdir $dir, 'test.log';
my $log = Mojo::Log->new(level => 'debug', path => $path);
ok $log->handle, 'file has been opened';
ok !$log->handle(undef)->handle, 'handle has been replaced';
$log->close;
ok $log->handle, 'file has been reopened';
$log->debug('Just works.');
$log = Mojo::Log->new;
like(
Expand Down

0 comments on commit de05199

Please sign in to comment.