Skip to content

Commit

Permalink
the fatal log level is always active
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 26, 2015
1 parent 46e21e7 commit 4f5c1b3
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 189 deletions.
6 changes: 4 additions & 2 deletions Changes
Expand Up @@ -11,6 +11,7 @@
Mojolicious::Validator::Validation.
- Removed multi-name support from param method in Mojo::Parameters.
- Removed multi-name support from cookie and upload methods in Mojo::Message.
- Removed is_fatal, is_level and log methods from Mojo::Log.
- Removed auto_render method from Mojolicious::Routes.
- Removed deprecated object-oriented Mojo::Loader API.
- Removed deprecated accept_interval, lock and unlock attributes from
Expand All @@ -25,11 +26,11 @@
- Removed deprecated keep_alive_requests setting from Hypnotoad.
- Changed return values of all and find methods in
Mojo::UserAgent::CookieJar.
- Renamed template attribute in Mojo::Template to unparsed.
- Renamed extracting attribute in Mojo::UserAgent::CookieJar to collecting.
- Renamed types attribute in Mojolicious::Types to mapping.
- Renamed current attribute in Mojolicious::Routes::Match to position.
- Renamed pattern attribute in Mojolicious::Routes::Route to unparsed.
- Renamed template attribute in Mojo::Template to unparsed.
- Renamed extracting attribute in Mojo::UserAgent::CookieJar to collecting.
- Renamed all_contents, contents, following_siblings, next_sibling,
preceding_siblings and previous_sibling methods in Mojo::DOM to
descendant_nodes, child_nodes, following_nodes, next_node, preceding_nodes
Expand All @@ -38,6 +39,7 @@
and prepare.
- Renamed inject method in Mojo::UserAgent::Proxy to prepare.
- Renamed params method in Mojo::Parameters to pairs.
- Renamed match method in Mojolicious::Routes::Match to find.
- Renamed -A option of prefork command to -a.
- Added names method to Mojo::Parameters.
- Added failed and passed methods to Mojolicious::Validator::Validation.
Expand Down
61 changes: 19 additions & 42 deletions lib/Mojo/Log.pm
Expand Up @@ -32,40 +32,36 @@ sub append {
flock $handle, LOCK_UN;
}

sub debug { shift->log(debug => @_) }
sub error { shift->log(error => @_) }
sub fatal { shift->log(fatal => @_) }
sub info { shift->log(info => @_) }

sub is_debug { shift->is_level('debug') }
sub is_error { shift->is_level('error') }
sub is_fatal { shift->is_level('fatal') }
sub is_info { shift->is_level('info') }

sub is_level {
$LEVEL->{lc pop} >= $LEVEL->{$ENV{MOJO_LOG_LEVEL} || shift->level};
}

sub is_warn { shift->is_level('warn') }
sub debug { shift->_log(debug => @_) }
sub error { shift->_log(error => @_) }
sub fatal { shift->_log(fatal => @_) }
sub info { shift->_log(info => @_) }

sub log { shift->emit('message', lc shift, @_) }
sub is_debug { shift->_now('debug') }
sub is_error { shift->_now('error') }
sub is_info { shift->_now('info') }
sub is_warn { shift->_now('warn') }

sub new {
my $self = shift->SUPER::new(@_);
$self->on(message => \&_message);
return $self;
}

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

sub _format {
'[' . localtime(shift) . '] [' . shift() . '] ' . join("\n", @_, '');
}

sub _now { $LEVEL->{pop()} >= $LEVEL->{$ENV{MOJO_LOG_LEVEL} || shift->level} }

sub _log { shift->emit('message', shift, @_) }

sub _message {
my ($self, $level) = (shift, shift);

return unless $self->is_level($level);
return unless $self->_now($level);

my $max = $self->max_history_size;
my $history = $self->history;
Expand Down Expand Up @@ -194,34 +190,28 @@ Append message to L</"handle">.
$log = $log->debug('You screwed up, but that is ok');
$log = $log->debug('All', 'cool');
Log debug message.
Emit L</"message"> event and log debug message.
=head2 error
$log = $log->error('You really screwed up this time');
$log = $log->error('Wow', 'seriously');
Log error message.
Emit L</"message"> event and log error message.
=head2 fatal
$log = $log->fatal('Its over...');
$log = $log->fatal('Bye', 'bye');
Log fatal message.
Emit L</"message"> event and log fatal message.
=head2 info
$log = $log->info('You are bad, but you prolly know already');
$log = $log->info('Ok', 'then');
Log info message.
=head2 is_level
my $bool = $log->is_level('debug');
Check log level.
Emit L</"message"> event and log info message.
=head2 is_debug
Expand All @@ -235,12 +225,6 @@ Check for debug log level.
Check for error log level.
=head2 is_fatal
my $bool = $log->is_fatal;
Check for fatal log level.
=head2 is_info
my $bool = $log->is_info;
Expand All @@ -253,13 +237,6 @@ Check for info log level.
Check for warn log level.
=head2 log
$log = $log->log(debug => 'This should work');
$log = $log->log(debug => 'This', 'too');
Emit L</"message"> event.
=head2 new
my $log = Mojo::Log->new;
Expand All @@ -272,7 +249,7 @@ default logger.
$log = $log->warn('Dont do that Dave...');
$log = $log->warn('No', 'really');
Log warn message.
Emit L</"message"> event and log warn message.
=head1 SEE ALSO
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Routes.pm
Expand Up @@ -84,7 +84,7 @@ sub match {
}

# Check routes
$match->match($c => {method => $method, path => $path, websocket => $ws});
$match->find($c => {method => $method, path => $path, websocket => $ws});
return unless my $route = $match->endpoint;
$cache->set(
"$method:$path:$ws" => {endpoint => $route, stack => $match->stack});
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojolicious/Routes/Match.pm
Expand Up @@ -7,7 +7,7 @@ has [qw(endpoint root)];
has position => 0;
has stack => sub { [] };

sub match { $_[0]->_match($_[0]->root, $_[1], $_[2]) }
sub find { $_[0]->_match($_[0]->root, $_[1], $_[2]) }

sub path_for {
my ($self, $name, %values) = (shift, Mojo::Util::_options(@_));
Expand Down Expand Up @@ -113,7 +113,7 @@ Mojolicious::Routes::Match - Find routes
# Match
my $c = Mojolicious::Controller->new;
my $match = Mojolicious::Routes::Match->new(root => $r);
$match->match($c => {method => 'PUT', path => '/foo/bar'});
$match->find($c => {method => 'PUT', path => '/foo/bar'});
say $match->stack->[0]{controller};
say $match->stack->[0]{action};
Expand Down Expand Up @@ -164,11 +164,11 @@ Captured parameters with nesting history.
L<Mojolicious::Routes::Match> inherits all methods from L<Mojo::Base> and
implements the following new ones.
=head2 match
=head2 find
$match->match(Mojolicious::Controller->new, {method => 'GET', path => '/'});
$match->find(Mojolicious::Controller->new, {method => 'GET', path => '/'});
Match controller and options against L</"root"> to find appropriate
Match controller and options against L</"root"> to find an appropriate
L</"endpoint">.
=head2 path_for
Expand Down
14 changes: 0 additions & 14 deletions t/mojo/log.t
Expand Up @@ -74,9 +74,6 @@ is_deeply $msgs, [qw(error Test 1 2 3)], 'right message';
$msgs = [];
$log->fatal('Test', 1, 2, 3);
is_deeply $msgs, [qw(fatal Test 1 2 3)], 'right message';
$msgs = [];
$log->log('fatal', 'Test', 1, 2, 3);
is_deeply $msgs, [qw(fatal Test 1 2 3)], 'right message';

# History
$buffer = '';
Expand Down Expand Up @@ -105,48 +102,37 @@ ok !$history->[2], 'no more messages';

# "debug"
is $log->level('debug')->level, 'debug', 'right level';
ok $log->is_level('debug'), '"debug" log level is active';
ok $log->is_level('info'), '"info" log level is active';
ok $log->is_debug, '"debug" log level is active';
ok $log->is_info, '"info" log level is active';
ok $log->is_warn, '"warn" log level is active';
ok $log->is_error, '"error" log level is active';
ok $log->is_fatal, '"fatal" log level is active';

# "info"
is $log->level('info')->level, 'info', 'right level';
ok !$log->is_level('debug'), '"debug" log level is inactive';
ok $log->is_level('info'), '"info" log level is active';
ok !$log->is_debug, '"debug" log level is inactive';
ok $log->is_info, '"info" log level is active';
ok $log->is_warn, '"warn" log level is active';
ok $log->is_error, '"error" log level is active';
ok $log->is_fatal, '"fatal" log level is active';

# "warn"
is $log->level('warn')->level, 'warn', 'right level';
ok !$log->is_level('debug'), '"debug" log level is inactive';
ok !$log->is_level('info'), '"info" log level is inactive';
ok !$log->is_debug, '"debug" log level is inactive';
ok !$log->is_info, '"info" log level is inactive';
ok $log->is_warn, '"warn" log level is active';
ok $log->is_error, '"error" log level is active';
ok $log->is_fatal, '"fatal" log level is active';

# "error"
is $log->level('error')->level, 'error', 'right level';
ok !$log->is_debug, '"debug" log level is inactive';
ok !$log->is_info, '"info" log level is inactive';
ok !$log->is_warn, '"warn" log level is inactive';
ok $log->is_error, '"error" log level is active';
ok $log->is_fatal, '"fatal" log level is active';

# "fatal"
is $log->level('fatal')->level, 'fatal', 'right level';
ok !$log->is_debug, '"debug" log level is inactive';
ok !$log->is_info, '"info" log level is inactive';
ok !$log->is_warn, '"warn" log level is inactive';
ok !$log->is_error, '"error" log level is inactive';
ok $log->is_fatal, '"fatal" log level is active';

done_testing();
2 changes: 1 addition & 1 deletion t/mojolicious/exception_lite_app.t
Expand Up @@ -39,7 +39,7 @@ get '/logger' => sub {
my $c = shift;
my $level = $c->param('level');
my $msg = $c->param('message');
$c->app->log->log($level => $msg);
$c->app->log->$level($msg);
$c->render(text => "$level: $msg");
};

Expand Down

0 comments on commit 4f5c1b3

Please sign in to comment.