Chiba.pm #3
2013/6/8
Mojoliciousの
ログ出力を改造する
いとちん @ayumu83s
perl歴は2年半くらい。
主にソーシャルゲームの開発をしています。
自己紹介
いとちん @ayumu83s
perl歴は2年半くらい。
主にソーシャルゲームの開発をしています。
自己紹介
開発業務の他にも・・・
開発業務の他にも・・・
開発業務の他にも・・・
開発業務の他にも・・・
開発業務の他にも・・・
俺じゃなくてもよくね?
ここから、本題。
Mojoliciousのログ
[Thu Jun 7 00:10:31 2013] [debug] 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] Routing to controller "MojoSample::
Controller::Example" andebug action "welcome".
[Thu Jun 7 01:21:42 2013] [debug] Example::welcome start
[Thu Jun 7 00:10:31 2013] [debug] id:1
[Thu Jun 7 00:10:31 2013] [debug] name:itou
[Thu Jun 7 00:10:31 2013] [debug] id:2
[Thu Jun 7 00:10:31 2013] [debug] name:ayumu
[Thu Jun 7 00:10:31 2013] [debug] id:3
[Thu Jun 7 00:10:31 2013] [debug] name:ayumu83s
[Thu Jun 7 01:21:42 2013] [debug] Example::welcome end
[Thu Jun 7 00:10:31 2013] [debug] 200 OK (0.002594s, 385.505/s).
Mojoliciousのログ
[Thu Jun 7 00:10:31 2013] [debug] 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] Routing to controller "MojoSample::
Controller::Example" andebug action "welcome".
[Thu Jun 7 01:21:42 2013] [debug] Example::welcome start
[Thu Jun 7 00:10:31 2013] [debug] id:1
[Thu Jun 7 00:10:31 2013] [debug] name:itou
[Thu Jun 7 00:10:31 2013] [debug] id:2
[Thu Jun 7 00:10:31 2013] [debug] name:ayumu
[Thu Jun 7 00:10:31 2013] [debug] id:3
[Thu Jun 7 00:10:31 2013] [debug] name:ayumu83s
[Thu Jun 7 01:21:42 2013] [debug] Example::welcome end
[Thu Jun 7 00:10:31 2013] [debug] 200 OK (0.002594s, 385.505/s).
Mojoliciousのログ
[Thu Jun 7 00:10:31 2013] [debug] 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] Routing to controller "MojoSample::
Controller::Example" andebug action "welcome".
[Thu Jun 7 01:21:42 2013] [debug] Example::welcome start
[Thu Jun 7 00:10:31 2013] [debug] id:1
[Thu Jun 7 00:10:31 2013] [debug] name:itou
[Thu Jun 7 00:10:31 2013] [debug] id:2
[Thu Jun 7 00:10:31 2013] [debug] name:ayumu
[Thu Jun 7 00:10:31 2013] [debug] id:3
[Thu Jun 7 00:10:31 2013] [debug] name:ayumu83s
[Thu Jun 7 01:21:42 2013] [debug] Example::welcome end
[Thu Jun 7 00:10:31 2013] [debug] 200 OK (0.002594s, 385.505/s).
パッケージ名とか
行数が欲しくね?
ググった
ググった → あった。
サブルーチンを上書きする作戦
# ここから
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";
};
}
# ここまで追加
サブルーチンを再定義する作戦
# ここから
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";
};
}
# ここまで追加
*[パッケージ名][関数名]に
無名関数を突っ込むと
再定義できる。
サブルーチンを上書きする作戦
# ここから
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する
そしてこうなった。
[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).
そしてこうなった。
[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).
我々の勝利である。
完
人のブログパクっただけじゃねーか
人のブログパクっただけじゃねーか
オブジェクト指向なやり方も
実装を試しました。
OOP的なやり方
OOP的なやり方
ざっくり概要。
OOP的なやり方
ざっくり概要。
1.Mojo::Logを派生したクラスを定義。
OOP的なやり方
ざっくり概要。
1.Mojo::Logを派生したクラスを定義。
↓
2.formatメソッドをオーバーライド。
ざっくり概要。
1.Mojo::Logを派生したクラスを定義。
↓
2.formatメソッドをオーバーライド。
↓
3.インスタンスを作成。
OOP的なやり方
ざっくり概要。
1.Mojo::Logを派生したクラスを定義。
↓
2.formatメソッドをオーバーライド。
↓
3.インスタンスを作成。
↓
4.logオブジェクトにぶっこみまーす。
OOP的なやり方
派生クラスの定義
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__
派生しました。
派生クラスの定義
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__
オーバーライド
しました。
インスタンスのぶっこみ
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');
:
ぶっこみました。
インスタンスのぶっこみ
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
に出力される。
ぶっこみました。
そしてこうなった。
[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).
そしてこうなった。
[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).
我々の大勝利である。
これでログを改造し放題!!
これでログを改造し放題!!
例1:ログ長いから短くする
my $now = localtime(time);
my $lv = substr($level, 0, 1);
return '[' . $now . "][$lv][$caller[0]:$caller[2]] " . join("n",
@lines) . "n";
[Thu Jun 7 00:10:31 2013][d][Mojolicious::Plugin::
RequestTimer:25] 200 OK (0.002594s, 385.505/s).
これでログを改造し放題!!
例2:木曜日なら「麺」を表示する。
my $now = localtime(time);
$now =~ s/Thu/麺/;
return '[' . $now . "][$level][$caller[0]:$caller[2]] " . join("n",
@lines) . "n";
[麺 Jun 7 00:10:31 2013][debug][Mojolicious::Plugin::
RequestTimer:25] 200 OK (0.002594s, 385.505/s).
まとめ
パッケージ名と行数が分かれば
デバッグが捗る!
ご静聴、
ありがとうございました。

Mojoliciousのログ出力を改造する in chiba.pm #3