| 1 | package Mojolicious::Plugin::ChromeLogger; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use Mojo::ByteStream qw/b/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Mojo::JSON qw/encode_json/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 1 | our $VERSION = 0.06; | |||||||||||
| 8 | |||||||||||||
| 9 | 1 | ▶ | has logs => sub { return [] }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 1 | my %types_map = ( | |||||||||||
| 12 | 'debug' => '', | ||||||||||||
| 13 | 'info' => 'info', | ||||||||||||
| 14 | 'warn' => 'warn', | ||||||||||||
| 15 | 'error' => 'error', | ||||||||||||
| 16 | 'fatal' => 'error', | ||||||||||||
| 17 | ); | ||||||||||||
| 18 | |||||||||||||
| 19 | 1 | ▶ | sub register { | ||||||||||
|
Subroutine
register: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||
| 20 | 1 | my ( $self, $app, $opts ) = @_; | |||||||||||
| 21 | |||||||||||||
| 22 | 1 | ▶ | $opts->{show_session} //= 1; | ||||||||||
|
Condition50%1/2
$opts->{"show_session"} //= 1
MC/DC0%0/1
$opts->{"show_session"} //= 1
$opts->{"show_session"}
Truth table50%1/2
A
A= $opts->{"show_session"}
| |||||||||||||
| 23 | 1 | ▶ | $opts->{show_stash} //= 1; | ||||||||||
|
Condition50%1/2
$opts->{"show_stash"} //= 1
MC/DC0%0/1
$opts->{"show_stash"} //= 1
$opts->{"show_stash"}
Truth table50%1/2
A
A= $opts->{"show_stash"}
| |||||||||||||
| 24 | 1 | ▶ | $opts->{show_config} //= 0; | ||||||||||
|
Condition50%1/2
$opts->{"show_config"} //= 0
MC/DC0%0/1
$opts->{"show_config"} //= 0
$opts->{"show_config"}
Truth table50%1/2
A
A= $opts->{"show_config"}
| |||||||||||||
| 25 | |||||||||||||
| 26 | # We do use monkey patch instead of inheriting Mojo::Log to be compatible with Log::Any::Adapter::Mojo | ||||||||||||
| 27 | 1 | $self->_monkey_patch_logger(); | |||||||||||
| 28 | |||||||||||||
| 29 | $app->hook( | ||||||||||||
| 30 | 1 | ▶ | after_dispatch => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.9
| |||||||||||||
| 31 | 1 | my ($c) = @_; | |||||||||||
| 32 | 1 | my $logs = $self->logs; | |||||||||||
| 33 | |||||||||||||
| 34 | # Leave static content untouched | ||||||||||||
| 35 | 1 | ▶ | return if $c->stash('mojo.static'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | |||||||||||||
| 37 | # Do not allow if not development mode | ||||||||||||
| 38 | 1 | ▶ | return if $c->app->mode ne 'development'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | |||||||||||||
| 40 | 1 | my $data = { | |||||||||||
| 41 | version => $VERSION, | ||||||||||||
| 42 | columns => [ 'log', 'backtrace', 'type' ], | ||||||||||||
| 43 | rows => [] | ||||||||||||
| 44 | }; | ||||||||||||
| 45 | |||||||||||||
| 46 | 1 | my $rows = $data->{rows}; | |||||||||||
| 47 | |||||||||||||
| 48 | # Start main group | ||||||||||||
| 49 | 1 | my $main_group = 'Mojolicious: ' . $c->req->method . ' ' . $c->req->url->path->to_string; | |||||||||||
| 50 | 1 | push @$rows, [[ $main_group ], undef, 'groupCollapsed']; | |||||||||||
| 51 | |||||||||||||
| 52 | # Add session | ||||||||||||
| 53 | 1 | ▶ | if ( $opts->{show_session} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 1 | push @$rows, [[ { '___class_name' => 'Session', %{$c->session} }], undef, '']; | |||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | # Add config | ||||||||||||
| 58 | 1 | ▶ | if ( $opts->{show_config} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | 0 | push @$rows, [[ { '___class_name' => 'Config', %{$c->config} }], undef, '']; | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | # Add stash | ||||||||||||
| 63 | 1 | ▶ | if ( $opts->{show_stash} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 1 | my %clean_stash = map { $_ => $c->stash($_) } grep { $_ !~ /^(?:mojo\.|config$)/ } keys %{ $c->stash }; | |||||||||||
| 65 | 1 | push @$rows, [[ { '___class_name' => 'Stash', %clean_stash }], undef, '']; | |||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | # Logs: fatal, info, debug, error | ||||||||||||
| 69 | 1 | push @$rows, [[ 'logs' ], undef, 'group']; | |||||||||||
| 70 | |||||||||||||
| 71 | 1 | foreach my $msg (@$logs) { | |||||||||||
| 72 | 5 | push @$rows, [ $msg->[1], $msg->[2], $types_map{ $msg->[0] } ]; | |||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | 1 | push @$rows, [[ 'logs' ], undef, 'groupEnd']; | |||||||||||
| 76 | |||||||||||||
| 77 | # End main group | ||||||||||||
| 78 | 1 | push @$rows, [[ $main_group ], undef, 'groupEnd']; | |||||||||||
| 79 | |||||||||||||
| 80 | 1 | my $json = encode_json($data); | |||||||||||
| 81 | 1 | my $final_data = b($json)->b64_encode(''); | |||||||||||
| 82 | 1 | $c->res->headers->add( 'X-ChromeLogger-Data' => $final_data ); | |||||||||||
| 83 | |||||||||||||
| 84 | 1 | $self->logs( [] ); | |||||||||||
| 85 | } | ||||||||||||
| 86 | 1 | ); | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | 1 | ▶ | sub _monkey_patch_logger { | ||||||||||
|
Subroutine
_monkey_patch_logger: called CC=2 SCAR=6.9
| |||||||||||||
| 90 | 1 | my ($self) = @_; | |||||||||||
| 91 | |||||||||||||
| 92 | 1 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 93 | 1 | my $stash = \%{"Mojo::Log::"}; | |||||||||||
| 94 | |||||||||||||
| 95 | 1 | foreach my $level (qw/debug info warn error fatal/) { | |||||||||||
| 96 | 5 | my $orig = delete $stash->{$level}; | |||||||||||
| 97 | |||||||||||||
| 98 | 5 | ▶ | *{"Mojo::Log::$level"} = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 99 | 5 | my ($package, $filename, $line) = caller; | |||||||||||
| 100 | 5 | push @{ $self->logs }, [ $level, [ $_[-1] ], "at $filename:$line" ]; | |||||||||||
| 101 | 5 | $orig->(@_); | |||||||||||
| 102 | 5 | }; | |||||||||||
| 103 | } | ||||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | 1 | 1; | |||||||||||
| 107 | |||||||||||||
| 108 | =head1 NAME | ||||||||||||
| 109 | |||||||||||||
| 110 | Mojolicious::Plugin::ChromeLogger - Pushes Mojolicious logs, stash, session, config to Google Chrome console | ||||||||||||
| 111 | |||||||||||||
| 112 | =head1 DESCRIPTION | ||||||||||||
| 113 | |||||||||||||
| 114 | L<Mojolicious::Plugin::ChromeLogger> pushes Mojolicious log messages, stash, session and config to Google Chrome console. Works with all types of responses(including JSON). | ||||||||||||
| 115 | To view logs in Google Chrome you should install ChromeLogger extenstion. Logging works only in development mode. | ||||||||||||
| 116 | |||||||||||||
| 117 | See details here http://craig.is/writing/chrome-logger | ||||||||||||
| 118 | |||||||||||||
| 119 | =head1 USAGE | ||||||||||||
| 120 | |||||||||||||
| 121 | use Mojolicious::Lite; | ||||||||||||
| 122 | |||||||||||||
| 123 | plugin 'ChromeLogger'; | ||||||||||||
| 124 | # or with options - plugin 'ChromeLogger' => {show_config => 1}; | ||||||||||||
| 125 | |||||||||||||
| 126 | get '/' => sub { | ||||||||||||
| 127 | my $self = shift; | ||||||||||||
| 128 | |||||||||||||
| 129 | app->log->debug('Some debug here'); | ||||||||||||
| 130 | app->log->info('Some info here'); | ||||||||||||
| 131 | app->log->warn('Some warn here'); | ||||||||||||
| 132 | app->log->error('Some error here'); | ||||||||||||
| 133 | app->log->fatal('Some fatal here'); | ||||||||||||
| 134 | |||||||||||||
| 135 | $self->render( text => 'Open Google Chrome console' ); | ||||||||||||
| 136 | }; | ||||||||||||
| 137 | |||||||||||||
| 138 | app->start; | ||||||||||||
| 139 | |||||||||||||
| 140 | =head1 CONFIG | ||||||||||||
| 141 | |||||||||||||
| 142 | =head2 C<show_config> | ||||||||||||
| 143 | |||||||||||||
| 144 | push config to ChromeLogger (default 0) | ||||||||||||
| 145 | |||||||||||||
| 146 | By default we do not show config. It is usually static and can contain confidential data. | ||||||||||||
| 147 | |||||||||||||
| 148 | =head2 C<show_stash> | ||||||||||||
| 149 | |||||||||||||
| 150 | push stash to ChromeLogger (default 1) | ||||||||||||
| 151 | |||||||||||||
| 152 | =head2 C<show_session> | ||||||||||||
| 153 | |||||||||||||
| 154 | push session to ChromeLogger (default 1) | ||||||||||||
| 155 | |||||||||||||
| 156 | =head1 SEE ALSO | ||||||||||||
| 157 | |||||||||||||
| 158 | L<Mojolicious::Plugin::ConsoleLogger> | ||||||||||||
| 159 | |||||||||||||
| 160 | =head1 DEVELOPMENT | ||||||||||||
| 161 | |||||||||||||
| 162 | L<https://github.com/koorchik/Mojolicious-Plugin-ChromeLogger> | ||||||||||||
| 163 | |||||||||||||
| 164 | =head1 CREDITS | ||||||||||||
| 165 | |||||||||||||
| 166 | Inspired by L<Mojolicious::Plugin::ConsoleLogger> | ||||||||||||
| 167 | |||||||||||||
| 168 | =head1 AUTHORS | ||||||||||||
| 169 | |||||||||||||
| 170 | Viktor Turskyi koorchik@cpan.org | ||||||||||||
| 171 | |||||||||||||
| 172 | =cut | ||||||||||||