15. サブルーチンを上書きする作戦
# ここから
if ($self->log->is_level('debug')) {
no warnings 'redefine';
*Mojo::Log::format = sub {
my ($self, $level, @lines) = @_;
my @caller = caller(4);
my $caller = join ' ', $caller[0], $caller[2];
return
'['
. localtime(time)
. "] [$level] [$caller] "
. join("n", @lines) . "n";
};
}
# ここまで追加
16. サブルーチンを再定義する作戦
# ここから
if ($self->log->is_level('debug')) {
no warnings 'redefine';
*Mojo::Log::format = sub {
my ($self, $level, @lines) = @_;
my @caller = caller(4);
my $caller = join ' ', $caller[0], $caller[2];
return
'['
. localtime(time)
. "] [$level] [$caller] "
. join("n", @lines) . "n";
};
}
# ここまで追加
*[パッケージ名][関数名]に
無名関数を突っ込むと
再定義できる。
17. サブルーチンを上書きする作戦
# ここから
if ($self->log->is_level('debug')) {
no warnings 'redefine';
*Mojo::Log::format = sub {
my ($self, $level, @lines) = @_;
my @caller = caller(4);
my $caller = join ' ', $caller[0], $caller[2];
return
'['
. localtime(time)
. "] [$level] [$caller] "
. join("n", @lines) . "n";
};
}
# ここまで追加
スタック情報から
実行したパッケージ名とかを
GETする
18. そしてこうなった。
[Thu Jun 7 00:10:31 2013][debug][Mojolicious::Plugin::RequestTimer:38] GET / (Mozilla/5.0
(Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.
0.1453.110 Safari/537.36).
[Thu Jun 7 00:10:31 2013][debug][Mojolicious::Routes:171] Routing to controller "MojoSample::
Controller::Example" andebug action "welcome".
[Thu Jun 7 01:21:42 2013][debug][MojoSample::Controller::Example:15] Example::welcome start
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:17] id:1
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:18] name:itou
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:17] id:2
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:18] name:ayumu
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:17] id:3
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:18] name:ayumu83s
[Thu Jun 7 01:21:42 2013][debug][MojoSample::Controller::Example:22] Example::welcome end
[Thu Jun 7 00:10:31 2013][debug][Mojolicious::Plugin::RequestTimer:25] 200 OK (0.002594s,
385.505/s).
19. そしてこうなった。
[Thu Jun 7 00:10:31 2013][debug][Mojolicious::Plugin::RequestTimer:38] GET / (Mozilla/5.0
(Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.
0.1453.110 Safari/537.36).
[Thu Jun 7 00:10:31 2013][debug][Mojolicious::Routes:171] Routing to controller "MojoSample::
Controller::Example" andebug action "welcome".
[Thu Jun 7 01:21:42 2013][debug][MojoSample::Controller::Example:15] Example::welcome start
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:17] id:1
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:18] name:itou
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:17] id:2
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:18] name:ayumu
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:17] id:3
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:18] name:ayumu83s
[Thu Jun 7 01:21:42 2013][debug][MojoSample::Controller::Example:22] Example::welcome end
[Thu Jun 7 00:10:31 2013][debug][Mojolicious::Plugin::RequestTimer:25] 200 OK (0.002594s,
385.505/s).
我々の勝利である。
29. 派生クラスの定義
package MojoSample::LogEx;
use strict;
use warnings;
use parent qw/Mojo::Log/;
sub format {
my ($self, $level, @lines) = @_;
my @caller = caller(4);
return '[' . localtime(time) . "][$level][$caller[0]:$caller[2]]" . join("n", @lines)
. "n";
}
1;
__END__
派生しました。
30. 派生クラスの定義
package MojoSample::LogEx;
use strict;
use warnings;
use parent qw/Mojo::Log/;
sub format {
my ($self, $level, @lines) = @_;
my @caller = caller(4);
return '[' . localtime(time) . "][$level][$caller[0]:$caller[2]]" . join("n", @lines)
. "n";
}
1;
__END__
オーバーライド
しました。
31. インスタンスのぶっこみ
sub startup {
my $self = shift;
my $home = $self->home;
$self->log(MojoSample::LogEx->new(
path => $self->log->path,
level => $self->log->level
));
$self->log->debug('hello');
:
ぶっこみました。
32. インスタンスのぶっこみ
sub startup {
my $self = shift;
my $home = $self->home;
$self->log(MojoSample::LogEx->new(
path => $self->log->path,
level => $self->log->level
));
$self->log->debug('hello');
:
出力先とログレベルを
既定クラスに合わせる。
path指定しないと、/var/log/mojo.log
に出力される。
ぶっこみました。
33. そしてこうなった。
[Thu Jun 7 00:10:31 2013][debug][Mojolicious::Plugin::RequestTimer:38] GET / (Mozilla/5.0
(Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.
0.1453.110 Safari/537.36).
[Thu Jun 7 00:10:31 2013][debug][Mojolicious::Routes:171] Routing to controller "MojoSample::
Controller::Example" andebug action "welcome".
[Thu Jun 7 01:21:42 2013][debug][MojoSample::Controller::Example:15] Example::welcome start
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:17] id:1
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:18] name:itou
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:17] id:2
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:18] name:ayumu
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:17] id:3
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:18] name:ayumu83s
[Thu Jun 7 01:21:42 2013][debug][MojoSample::Controller::Example:22] Example::welcome end
[Thu Jun 7 00:10:31 2013][debug][Mojolicious::Plugin::RequestTimer:25] 200 OK (0.002594s,
385.505/s).
34. そしてこうなった。
[Thu Jun 7 00:10:31 2013][debug][Mojolicious::Plugin::RequestTimer:38] GET / (Mozilla/5.0
(Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.
0.1453.110 Safari/537.36).
[Thu Jun 7 00:10:31 2013][debug][Mojolicious::Routes:171] Routing to controller "MojoSample::
Controller::Example" andebug action "welcome".
[Thu Jun 7 01:21:42 2013][debug][MojoSample::Controller::Example:15] Example::welcome start
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:17] id:1
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:18] name:itou
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:17] id:2
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:18] name:ayumu
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:17] id:3
[Thu Jun 7 00:10:31 2013][debug][MojoSample::Controller::Example:18] name:ayumu83s
[Thu Jun 7 01:21:42 2013][debug][MojoSample::Controller::Example:22] Example::welcome end
[Thu Jun 7 00:10:31 2013][debug][Mojolicious::Plugin::RequestTimer:25] 200 OK (0.002594s,
385.505/s).
我々の大勝利である。