Skip to content

Commit

Permalink
Add shortcut for logger to controller, update examples and add pod
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Ramberg committed Aug 20, 2015
1 parent 6d1e9cf commit e876336
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 21 deletions.
17 changes: 12 additions & 5 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -13,6 +13,7 @@ use Time::HiRes ();
has [qw(app tx)];
has match =>
sub { Mojolicious::Routes::Match->new(root => shift->app->routes) };
has log => sub { shift->app->log };

# Reserved stash values
my %RESERVED = map { $_ => 1 } (
Expand Down Expand Up @@ -408,6 +409,12 @@ a L<Mojolicious> object.
# Generate path
my $path = $c->app->home->rel_file('templates/foo/bar.html.ep');
=head2 log
A shortcut for $c->app->log
$c->log->debug('Hello Mojo');

This comment has been minimized.

Copy link
@jhthorsen

jhthorsen Aug 20, 2015

Member

Not sure if this is really a shortcut, since you're caching the object in an attribute. I'm also not sure if this documentation format is according to the default attribute documentation. Shouldn't it rather be something like:

$log = $self->log;
$self = $self->log(Mojo::Log->new);

This comment has been minimized.

Copy link
@marcusramberg

marcusramberg Aug 20, 2015

Member

The example is copied from line 407. I'd argue that the caching is an implementation detail for performance, and not important for the end user, but I could of course be wrong.

=head2 match
my $m = $c->match;
Expand Down Expand Up @@ -441,7 +448,7 @@ underlying connection might get closed early.
# Perform non-blocking operation without knowing the connection status
my $tx = $c->tx;
Mojo::IOLoop->timer(2 => sub {
$c->app->log->debug($tx->is_finished ? 'Finished' : 'In progress');
$c->log->debug($tx->is_finished ? 'Finished' : 'In progress');
});
=head1 METHODS
Expand Down Expand Up @@ -551,26 +558,26 @@ establish the WebSocket connection.
# Do something after the transaction has been finished
$c->on(finish => sub {
my $c = shift;
$c->app->log->debug('We are done');
$c->log->debug('We are done');
});
# Receive WebSocket message
$c->on(message => sub {
my ($c, $msg) = @_;
$c->app->log->debug("Message: $msg");
$c->log->debug("Message: $msg");
});
# Receive JSON object via WebSocket message
$c->on(json => sub {
my ($c, $hash) = @_;
$c->app->log->debug("Test: $hash->{test}");
$c->log->debug("Test: $hash->{test}");
});
# Receive WebSocket "Binary" message
$c->on(binary => sub {
my ($c, $bytes) = @_;
my $len = length $bytes;
$c->app->log->debug("Received $len bytes");
$c->log->debug("Received $len bytes");
});
=head2 param
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -583,7 +583,7 @@ L<Mojolicious::Controller/"send">.
my $c = shift;

# Opened
$c->app->log->debug('WebSocket opened');
$c->log->debug('WebSocket opened');

# Increase inactivity timeout for connection a bit
$c->inactivity_timeout(300);
Expand All @@ -597,7 +597,7 @@ L<Mojolicious::Controller/"send">.
# Closed
$c->on(finish => sub {
my ($c, $code, $reason) = @_;
$c->app->log->debug("WebSocket closed with status $code");
$c->log->debug("WebSocket closed with status $code");
});
};

Expand Down Expand Up @@ -690,15 +690,15 @@ infrastructure requirements, since it reuses the HTTP protocol for transport.
$c->write;

# Subscribe to "message" event and forward "log" events to browser
my $cb = $c->app->log->on(message => sub {
my $cb = $c->log->on(message => sub {
my ($log, $level, @lines) = @_;
$c->write("event:log\ndata: [$level] @lines\n\n");
});

# Unsubscribe from "message" event again once we are done
$c->on(finish => sub {
my $c = shift;
$c->app->log->unsubscribe(message => $cb);
$c->log->unsubscribe(message => $cb);
});
};

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -665,7 +665,7 @@ action could do.

helper debug => sub {
my ($c, $str) = @_;
$c->app->log->debug($str);
$c->log->debug($str);
};

get '/' => sub {
Expand Down Expand Up @@ -1101,7 +1101,7 @@ applications, plugins make that very simple.
my ($self, $app) = @_;
$app->helper(debug => sub {
my ($c, $str) = @_;
$c->app->log->debug($str);
$c->log->debug($str);
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides/Tutorial.pod
Expand Up @@ -289,7 +289,7 @@ from actions to templates.
get '/secret' => sub {
my $c = shift;
my $user = $c->whois;
$c->app->log->debug("Request from $user");
$c->log->debug("Request from $user");
};

app->start;
Expand Down Expand Up @@ -896,7 +896,7 @@ the attribute L<Mojolicious/"mode">.

get '/' => sub {
my $c = shift;
$c->app->log->debug('Rendering mode specific message');
$c->log->debug('Rendering mode specific message');
$c->render(text => $msg);
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Renderer.pm
Expand Up @@ -230,7 +230,7 @@ sub _render_template {
}

# No handler
else { $c->app->log->error(qq{No handler for "$handler" available}) }
else { $c->log->error(qq{No handler for "$handler" available}) }
return undef;
}

Expand Down
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->$level($msg);
$c->log->$level($msg);
$c->render(text => "$level: $msg");
};

Expand Down
12 changes: 6 additions & 6 deletions t/mojolicious/renderer.t
Expand Up @@ -7,7 +7,7 @@ use Mojolicious;
# Partial rendering
my $app = Mojolicious->new(secrets => ['works']);
my $c = $app->build_controller;
$c->app->log->level('fatal');
$c->log->level('fatal');
is $c->render_to_string(text => 'works'), 'works', 'renderer is working';

# Normal rendering with default format
Expand Down Expand Up @@ -46,22 +46,22 @@ is_deeply [$renderer->render($c)], ['Hello Mojo!', 'test'],

# Unrecognized handler
my $log = '';
my $cb = $c->app->log->on(message => sub { $log .= pop });
my $cb = $c->log->on(message => sub { $log .= pop });
$c->stash->{handler} = 'not_defined';
is $renderer->render($c), undef, 'return undef for unrecognized handler';
like $log, qr/No handler for "not_defined" available/, 'right message';
$c->app->log->unsubscribe(message => $cb);
$c->log->unsubscribe(message => $cb);

# Default template name
$c->stash(controller => 'foo', action => 'bar');
is $c->app->renderer->template_for($c), 'foo/bar', 'right template name';

# Big cookie
$log = '';
$cb = $c->app->log->on(message => sub { $log .= pop });
$cb = $c->log->on(message => sub { $log .= pop });
$c->cookie(foo => 'x' x 4097);
like $log, qr/Cookie "foo" is bigger than 4096 bytes/, 'right message';
$c->app->log->unsubscribe(message => $cb);
$c->log->unsubscribe(message => $cb);

# Nested helpers
my $first = $app->build_controller;
Expand All @@ -82,7 +82,7 @@ is $first->helpers->myapp->defaults('foo'), 'bar', 'right result';
is $first->app->myapp->defaults('foo'), 'bar', 'right result';
my $app2 = Mojolicious->new(secrets => ['works']);
my $second = $app2->build_controller;
$second->app->log->level('fatal');
$second->log->level('fatal');
is $second->app->renderer->get_helper('myapp'), undef, 'no helper';
is $second->app->renderer->get_helper('myapp.defaults'), undef, 'no helper';
$second->app->helper('myapp.defaults' => sub {'nothing'});
Expand Down

0 comments on commit e876336

Please sign in to comment.