Skip to content

Commit

Permalink
added real-time logging to not found and exception pages
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 13, 2014
1 parent 701af99 commit a8bb214
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/Mojolicious.pm
Expand Up @@ -82,6 +82,20 @@ sub new {
$self->log->path($home->rel_file("log/$mode.log"))
if -w $home->rel_file('log');

# Live log in development mode
$r->websocket('/mojo/livelog' => sub {
my $c = shift;

Mojo::IOLoop->stream($c->tx->connection)->timeout(300);

my $cb = $c->app->log->on(message => sub {
my ($log, $level, @lines) = @_;
$c->send(qq/[@{[scalar localtime time]}] [$level] @{[join "\n", @lines]}/);
});

$c->on(finish => sub { shift->app->log->unsubscribe(message => $cb)});
}) if $mode eq 'development';

$self->plugin($_)
for qw(HeaderCondition DefaultHelpers TagHelpers EPLRenderer EPRenderer);

Expand Down
7 changes: 7 additions & 0 deletions lib/Mojolicious/templates/exception.development.html.ep
Expand Up @@ -245,6 +245,13 @@
% }
</table>
</div>
%= javascript begin
var ws = new WebSocket('<%= url_for('mojolivelog')->to_abs %>');
ws.onmessage = function(event) {
$('#log tr:first-child').remove();
$('#log table').append('<tr><td class="striped value"><pre>' + event.data + '</pre></td></tr>');
};
% end
% }
</div>
<div id="footer">
Expand Down
7 changes: 7 additions & 0 deletions lib/Mojolicious/templates/not_found.development.html.ep
Expand Up @@ -125,6 +125,13 @@
% }
</table>
</div>
%= javascript begin
var ws = new WebSocket('<%= url_for('mojolivelog')->to_abs %>');
ws.onmessage = function(event) {
$('#log tr:first-child').remove();
$('#log table').append('<tr><td class="striped value"><pre>' + event.data + '</pre></td></tr>');
};
% end
% }
</div>
<div id="footer">
Expand Down

0 comments on commit a8bb214

Please sign in to comment.