| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::ConsoleLogger; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1373
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
5
|
|
|
4
|
1
|
|
|
1
|
|
200
|
use Mojo::ByteStream; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
34
|
|
|
5
|
1
|
|
|
1
|
|
15
|
use Mojo::JSON qw(decode_json encode_json); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
114
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = 0.06; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has logs => sub { |
|
10
|
|
|
|
|
|
|
return { |
|
11
|
|
|
|
|
|
|
fatal => [], |
|
12
|
|
|
|
|
|
|
info => [], |
|
13
|
|
|
|
|
|
|
debug => [], |
|
14
|
|
|
|
|
|
|
error => [], |
|
15
|
|
|
|
|
|
|
}; |
|
16
|
|
|
|
|
|
|
}; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub register { |
|
19
|
1
|
|
|
1
|
1
|
29
|
my ($plugin, $app) = @_; |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# override Mojo::Log->log |
|
22
|
1
|
|
|
1
|
|
4
|
no strict 'refs'; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
590
|
|
|
23
|
1
|
|
|
|
|
1
|
my $stash = \%{"Mojo::Log::"}; |
|
|
1
|
|
|
|
|
2
|
|
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Mojolicious 6 renames Mojo::Log::log to Mojo::Log::_log |
|
26
|
1
|
50
|
|
|
|
4
|
my $logsub = (defined &Mojo::Log::_log) ? "_log" : "log"; |
|
27
|
1
|
|
|
|
|
3
|
my $orig = delete $stash->{$logsub}; |
|
28
|
|
|
|
|
|
|
|
|
29
|
1
|
|
|
|
|
4
|
*{"Mojo::Log::$logsub"} = sub { |
|
30
|
9
|
|
|
9
|
|
10973
|
push @{$plugin->logs->{$_[1]}} => $_[-1]; |
|
|
9
|
|
|
|
|
19
|
|
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Original Mojo::Log->log |
|
33
|
9
|
|
|
|
|
40
|
$orig->(@_); |
|
34
|
1
|
|
|
|
|
3
|
}; |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$app->hook( |
|
37
|
|
|
|
|
|
|
after_dispatch => sub { |
|
38
|
2
|
|
|
2
|
|
12344
|
my $self = shift; |
|
39
|
|
|
|
|
|
|
# Patched Nov 23, 2014 to work with JSON |
|
40
|
2
|
50
|
|
|
|
5
|
return if $self->res->headers->content_type eq 'application/json'; |
|
41
|
2
|
|
|
|
|
41
|
my $logs = $plugin->logs; |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Leave static content untouched |
|
44
|
2
|
100
|
|
|
|
10
|
return if $self->stash('mojo.static'); |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Do not allow if not development mode |
|
47
|
1
|
50
|
|
|
|
9
|
return if $self->app->mode ne 'development'; |
|
48
|
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
9
|
my $str = "\n\n\n"; |
|
77
|
|
|
|
|
|
|
|
|
78
|
1
|
|
|
|
|
3
|
$self->res->body($self->res->body . $str); |
|
79
|
|
|
|
|
|
|
} |
|
80
|
1
|
|
|
|
|
7
|
); |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _format_msg { |
|
84
|
12
|
|
|
12
|
|
208
|
my $msg = shift; |
|
85
|
|
|
|
|
|
|
|
|
86
|
12
|
100
|
|
|
|
35
|
return ref($msg) |
|
87
|
|
|
|
|
|
|
? "console.log(" . encode_json($msg) . "); " |
|
88
|
|
|
|
|
|
|
: "console.log(" . Mojo::ByteStream->new($msg)->quote . "); "; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 NAME |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Mojolicious::Plugin::ConsoleLogger - Console logging in your browser |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
L pushes Mojolicious session, stash, config, and log messages to the browser's console tool. |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
* Logging operates only in development mode. |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 USAGE |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
use Mojolicious::Lite; |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
plugin 'ConsoleLogger'; |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
get '/' => sub { |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
app->log->debug("Here I am!"); |
|
112
|
|
|
|
|
|
|
app->log->error("This is bad"); |
|
113
|
|
|
|
|
|
|
app->log->fatal("This is really bad"); |
|
114
|
|
|
|
|
|
|
app->log->info("This isn't bad at all"); |
|
115
|
|
|
|
|
|
|
app->log->info({json => 'structure'}); |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
shift->render(text => 'Ahm in ur browzers, logginz ur console'); |
|
118
|
|
|
|
|
|
|
}; |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
app->start; |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 METHODS |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
L inherits all methods from |
|
125
|
|
|
|
|
|
|
L and implements the following new ones. |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 C |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
$plugin->register; |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Register condition in L application. |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
L |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 DEVELOPMENT |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
L |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 VERSION |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
0.06 |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 CREDITS |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Implementation stolen from L |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 AUTHORS |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Glen Hinkle tempire@cpan.org |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Andrew Kirkpatrick |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Zhenyi Zhou |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |