LoginSignup
15
16

More than 5 years have passed since last update.

Log::MinimalでPerlのログ出力してみた

Posted at

インストール方法


>$ cpanm install Log::Minimal

ログレベル

レベル 対象者 内容
debug 開発者 開発中のデバッグ用出力。商用環境では出力しない。
info 開発者 開発者が運用中に調査等で参照する情報。不具合を見つけるための手がかりに使用。
warn 運用者/開発者 運用中に発生したシステムに関する問題。直ちには問題がない状態。
critical 運用者/開発者 サービスが継続できない障害。原因究明の情報

表示例

log.test.pl
use Log::Minimal;

# デバッグを出力する場合:1
$ENV{LM_DEBUG} = 1;

debugf( 'デバッグレベル出力される.' ); # $ENV{LM_DEBUG} がtrueの場合出力される
infof( 'インフォレベルが出力される.' );
warnf( 'ワーニングレベルが出力される.' );
critf( 'クリティカルレベルが出力される.' );

# フルスタックトレースをする場合
debugff( 'デバッグレベル出力される.' ); # $ENV{LM_DEBUG} がtrueの場合出力される
infoff( 'インフォレベルが出力される.' );
warnff( 'ワーニングレベルが出力される.' );
critff( 'クリティカルレベルが出力される.' ); 

# パラメータを設定する場合
debugf( 'This is %s', 'debug log.' ); # $ENV{LM_DEBUG} がtrueの場合出力される
infof( 'This is %s', 'information log.' );
warnf( 'This is %s', 'warning log.' );
critf( 'This is %s', 'critical log. ' );

実行結果


2015-04-24T00:11:32 [DEBUG] デバッグレベル出力される. at log.test.pl line 6
2015-04-24T00:11:32 [INFO] インフォレベルが出力される. at log.test.pl line 7
2015-04-24T00:11:32 [WARN] ワーニングレベルが出力される. at log.test.pl line 8
2015-04-24T00:11:32 [CRITICAL] クリティカルレベルが出力される. at log.test.pl line 9
2015-04-24T00:11:32 [DEBUG] デバッグレベル出力される. at log.test.pl line 12
2015-04-24T00:11:32 [INFO] インフォレベルが出力される. at log.test.pl line 13
2015-04-24T00:11:32 [WARN] ワーニングレベルが出力される. at log.test.pl line 14
2015-04-24T00:11:32 [CRITICAL] クリティカルレベルが出力される. at log.test.pl line 15
2015-04-24T00:11:32 [DEBUG] This is debug log. at log.test.pl line 18
2015-04-24T00:11:32 [INFO] This is information log. at log.test.pl line 19
2015-04-24T00:11:32 [WARN] This is warning log. at log.test.pl line 20
2015-04-24T00:11:32 [CRITICAL] This is critical log.   at log.test.pl line 21

シリアライズ

log.test.pl
use Log::Minimal;

my $info = {
    id   => 100,
    name => 'user_name',
    mail => 'mail_address'
};
local $Log::Minimal::AUTODUMP = 1;
infof( 'info:%s', $info );

実行結果


2015-04-21T23:59:59 [INFO] info:{'id' => 100,'mail' => 'mail_address','name' => 'user_name'} at log.test.pl line 9

ログフォーマットの変更

log.formatter.pm
package Log::Minimal;

$Log::Minimal::PRINT = sub {
    my ( $time, $type, $message, $trace, $raw_message ) = @_;
    print "$time ", ( $FORMAT->{$type}||"[$type] - " ), " $message at $trace\n";
};

1;

log.test.pl
use Log::Minimal;

require './log.formatter.pm';

infof( "フォーマット変更したよ" );

};

実行結果


2015-04-24T00:06:01 [INFO] -  フォーマット変更したよ at test.log2.pl line 5

ログ出力先の変更

output.pm
package Log::Minimal;

my $fh;

open ( $fh, '>>', 'application.log' );
local $SIG{ HUP } = sub {
    undef $fh;
    open ( $fh, '>>', 'application.log' );
};

$Log::Minimal::PRINT = sub {
    my ( $time, $type, $message, $trace, $raw_message ) = @_;
    print $fh "$time ", ( $FORMAT->{$type}||"[$type] - " ), " $message at $trace\n";
};

1;
test.pl
use Log::Minimal;

require './output.pm';

infof("出力先変更");
application.log
2015-04-24T00:30:47 [INFO] -  出力先変更 at test.pl line 5
15
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
16