Skip to content

Commit

Permalink
removed reopen method from Mojo::Log again
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 1, 2012
1 parent aeddb36 commit f89d0c3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 52 deletions.
2 changes: 0 additions & 2 deletions Changes
@@ -1,8 +1,6 @@
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: 10 additions & 21 deletions lib/Mojo/Log.pm
Expand Up @@ -7,7 +7,16 @@ use IO::File;

has handle => sub {
my $self = shift;
return $self->{handle} if $self->reopen;

# 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
binmode STDERR, ':utf8';
return \*STDERR;
};
Expand Down Expand Up @@ -69,20 +78,6 @@ 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 @@ -263,12 +258,6 @@ Check for warn log level.
Emit C<message> event.
=head2 C<reopen>
my $success = $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: 0 additions & 13 deletions lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -105,10 +105,6 @@ 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 @@ -356,7 +352,6 @@ 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 @@ -438,10 +433,6 @@ Increase worker pool by one.
Decrease worker pool by one.
=item C<USR1>
Reopen log file server-wide.
=item C<USR2>
Attempt zero downtime software upgrade (hot deployment) without losing any
Expand Down Expand Up @@ -475,10 +466,6 @@ Stop worker immediately.
Stop worker gracefully.
=item C<USR1>
Reopen worker log file.
=back
=head1 SETTINGS
Expand Down
17 changes: 1 addition & 16 deletions t/mojo/log.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

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

# "Don't let Krusty's death get you down, boy.
# People die all the time, just like that.
Expand All @@ -22,21 +22,6 @@ like(
'right content'
);

# Reopen log file
$path = catdir $dir, 'test2.log';
$log = Mojo::Log->new->level('debug')->path($path);
ok $log->handle, 'handle has been opened';
ok !$log->handle(undef)->handle, 'handle has been replaced';
ok $log->reopen, 'handle has been reopened';
ok $log->handle, 'handle is open';
$log->info('Works again.');
undef $log;
like(
Mojo::Asset::File->new(path => $path)->slurp,
qr/^\[.*\] \[info\] Works again\.\n$/,
'right content'
);

# Formatting
$log = Mojo::Log->new;
like $log->format(debug => 'Test 123.'), qr/^\[.*\] \[debug\] Test 123\.\n$/,
Expand Down

0 comments on commit f89d0c3

Please sign in to comment.