| File: | lib/Mojolicious/Plugin/ConsoleLogger.pm |
| Coverage: | 98.1% |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package Mojolicious::Plugin::ConsoleLogger; | ||||||
| 2 | |||||||
| 3 | 1 1 1 | 422 1 11 | use Mojo::Base 'Mojolicious::Plugin'; | ||||
| 4 | 1 1 1 | 102 1 13 | use Mojo::ByteStream; | ||||
| 5 | 1 1 1 | 30 1 10 | use Mojo::JSON; | ||||
| 6 | |||||||
| 7 | our $VERSION = 0.01; | ||||||
| 8 | |||||||
| 9 | has logs => sub { | ||||||
| 10 | return { | ||||||
| 11 | fatal => [], | ||||||
| 12 | info => [], | ||||||
| 13 | debug => [], | ||||||
| 14 | error => [], | ||||||
| 15 | }; | ||||||
| 16 | }; | ||||||
| 17 | |||||||
| 18 | sub register { | ||||||
| 19 | 1 | 1 | 48 | my ($plugin, $app) = @_; | |||
| 20 | |||||||
| 21 | # override Mojo::Log->log | ||||||
| 22 | 1 1 1 | 110 1 374 | no strict 'refs'; | ||||
| 23 | 1 1 | 1 4 | my $stash = \%{"Mojo::Log::"}; | ||||
| 24 | 1 | 4 | my $orig = delete $stash->{"log"}; | ||||
| 25 | |||||||
| 26 | 1 | 4 | *{"Mojo::Log::log"} = sub { | ||||
| 27 | 17 17 | 78448 137 | push @{$plugin->logs->{$_[1]}} => $_[-1]; | ||||
| 28 | |||||||
| 29 | # Original Mojo::Log->log | ||||||
| 30 | 17 | 219 | $orig->(@_); | ||||
| 31 | 1 | 4 | }; | ||||
| 32 | |||||||
| 33 | $app->hook( | ||||||
| 34 | after_dispatch => sub { | ||||||
| 35 | 3 | 18031 | my $self = shift; | ||||
| 36 | 3 | 30 | my $logs = $plugin->logs; | ||||
| 37 | |||||||
| 38 | # leave static content untouched | ||||||
| 39 | 3 | 41 | return if $self->stash('mojo.static'); | ||||
| 40 | |||||||
| 41 | 2 | 26 | my $str = "\n<!-- Mojolicious logging -->\n<script>"; | ||||
| 42 | |||||||
| 43 | 2 | 17 | for (sort keys %$logs) { | ||||
| 44 | 8 8 | 8 19 | next if !@{$logs->{$_}}; | ||||
| 45 | 8 | 16 | $str .= "console.group(\"$_\"); "; | ||||
| 46 | 8 8 8 | 7 6 20 | $str .= _format_msg($_) for @{$logs->{$_}}; | ||||
| 47 | 8 | 414 | $str .= "console.groupEnd(\"$_\"); "; | ||||
| 48 | } | ||||||
| 49 | |||||||
| 50 | 2 | 18 | $str .= "</script>\n"; | ||||
| 51 | |||||||
| 52 | 2 | 15 | $self->res->body($self->res->body . $str); | ||||
| 53 | } | ||||||
| 54 | 1 | 13 | ); | ||||
| 55 | } | ||||||
| 56 | |||||||
| 57 | sub _format_msg { | ||||||
| 58 | 24 | 820 | my $msg = shift; | ||||
| 59 | |||||||
| 60 | 24 | 65 | return "console.log(" . Mojo::JSON->new->encode($_) . "); " if ref $msg; | ||||
| 61 | |||||||
| 62 | 21 | 120 | return "console.log(" . Mojo::ByteStream->new($_)->quote . "); "; | ||||
| 63 | } | ||||||
| 64 | |||||||
| 65 | 1; | ||||||
| 66 | |||||||
| 67 - 124 | =head1 NAME
Mojolicious::Plugin::ConsoleLogger - Console logging in your browser
=head1 DESCRIPTION
L<Mojolicious::Plugin::ConsoleLogger> pushes Mojolicious log messages to your browser's console tool.
=head1 USAGE
use Mojolicious::Lite;
plugin 'console_logger';
get '/' => sub {
app->log->debug("Here I am!");
app->log->error("This is bad");
app->log->fatal("This is really bad");
app->log->info("This isn't bad at all");
shift->render(text => 'Ahm in ur browzers, logginz ur console');
};
app->start;
=head1 METHODS
L<Mojolicious::Plugin::ConsoleLogger> inherits all methods from
L<Mojolicious::Plugin> and implements the following new ones.
=head2 C<register>
$plugin->register;
Register condition in L<Mojolicious> application.
=head1 SEE ALSO
L<Mojolicious>
=head1 DEVELOPMENT
L<http://github.com/tempire/mojolicious-plugin-consolelogger>
=head1 VERSION
0.01
=head1 CREDITS
Implementation stolen from L<Plack::Middleware::ConsoleLogger>
=head1 AUTHOR
Glen Hinkle tempire@cpan.org
=cut | ||||||