Skip to content

Commit

Permalink
added reopen method to Mojo::Log
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 1, 2012
1 parent 8a4f43d commit 74afaa6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.71 2012-04-01
- Added simple log rotation support with USR1 signal to Hypnotoad.
- Added reopen method to Mojo::Log.
- Improved Hypnotoad error handling.
- Improved documentation.
- Improved tests.
Expand Down
31 changes: 21 additions & 10 deletions lib/Mojo/Log.pm
Expand Up @@ -7,16 +7,7 @@ use IO::File;

has handle => sub {
my $self = shift;

# File
if (my $path = $self->path) {
croak qq/Can't open log file "$path": $!/
unless my $file = IO::File->new(">> $path");
binmode $file, ':utf8';
return $file;
}

# STDERR
return $self->{handle} if $self->reopen;
binmode STDERR, ':utf8';
return \*STDERR;
};
Expand Down Expand Up @@ -78,6 +69,20 @@ sub log {
return $self->emit(message => $level => @_);
}

sub reopen {
my $self = shift;

# No log file
return unless my $path = $self->path;

# Reopen log file
delete $self->{handle};
croak qq/Can't open log file "$path": $!/
unless my $file = IO::File->new(">> $path");
binmode $file, ':utf8';
return $self->{handle} = $file;
}

sub warn { shift->log(warn => @_) }

1;
Expand Down Expand Up @@ -258,6 +263,12 @@ Check for warn log level.
Emit C<message> event.
=head2 C<reopen>
$log->reopen;
Reopen C<handle> if C<path> is available, useful for log rotation.
=head2 C<warn>
$log = $log->warn('Dont do that Dave...');
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.')->reopen;
kill 'USR1', $_ for keys %{$self->{workers}};
};
$SIG{USR2} = sub { $self->{upgrade} ||= time };
$SIG{TTIN} = sub { $c->{workers}++ };
$SIG{TTOU} = sub {
Expand Down Expand Up @@ -352,6 +356,7 @@ sub _spawn {
$SIG{INT} = $SIG{TERM} = $SIG{CHLD} = $SIG{USR2} = $SIG{TTIN} = $SIG{TTOU} =
'DEFAULT';
$SIG{QUIT} = sub { $loop->max_connections(0) };
$SIG{USR1} = sub { $self->{log}->reopen };
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

0 comments on commit 74afaa6

Please sign in to comment.