diff --git a/BassyBot.pm b/BassyBot.pm index 59db715..18ccdf8 100644 --- a/BassyBot.pm +++ b/BassyBot.pm @@ -7,15 +7,27 @@ use utf8; use Moose; extends 'TwitterBot'; use Time::ParseDate; +use Log::Log4perl; $BassyBot::VERSION = '0.2'; +has 'logger' => ( + isa => 'Log::Log4perl::Logger', + is => 'ro', + lazy => 1, + builder => '_build_logger', +); + +sub _build_logger { + return Log::Log4perl->get_logger(__PACKAGE__); +} + my @ACTIONS = ( # print out message sub { my $self = shift; my $tweet = shift; - print "Searching reaction for ", &_format_tweet($tweet), "\n"; + $self->logger->info("Searching reaction for ". &_format_tweet($tweet)); return (); }, # ignore myself @@ -97,6 +109,8 @@ sub BUILD { my $self = shift; $self->util->tweet("一人で焼肉食いにいってくる。18000円だけどな!"); }); + + $self->logger->info("bassy-bot initialized."); } sub _build_actions { @@ -117,6 +131,12 @@ sub _format_tweet { return "$ago ago: $tweet->{id} <$tweet->{user}{screen_name}/$tweet->{user}{id}> $tweet->{text}"; } +sub tweet { + my $self = shift; + $self->logger->info("> $_[0]".($_[1] ? " in_reply_to_status_id => $_[1]" : "")); + $self->util->tweet(@_); +} + no Moose; __PACKAGE__->meta->make_immutable(); diff --git a/TwitterUtil.pm b/TwitterUtil.pm index 4c8a324..dad3b3c 100644 --- a/TwitterUtil.pm +++ b/TwitterUtil.pm @@ -78,9 +78,6 @@ sub tweet { my $message = shift; my $in_reply_to_status_id = shift; my %new_status = (status => $message); - print "> $message"; - print " in_reply_to_status_id => $in_reply_to_status_id" if $in_reply_to_status_id; - print "\n"; $new_status{in_reply_to_status_id} = $in_reply_to_status_id if $in_reply_to_status_id; $self->twitter->update(\%new_status); } diff --git a/bassy-bot b/bassy-bot index 00518fa..3029949 100644 --- a/bassy-bot +++ b/bassy-bot @@ -4,6 +4,7 @@ use strict; use warnings; use Getopt::Long; +use Log::Log4perl; use BassyBot; my $username = 'bassytime'; @@ -22,6 +23,8 @@ die "--pass='YourPassword' plz.\n" unless $password; binmode STDOUT, ':encoding(shiftjis)' if $sjis; +Log::Log4perl->init("bassy-bot-logger.conf"); + BassyBot->new(username => $username, password => $password, ssl => ! $nossl)->start; diff --git a/bassy-bot-logger.conf b/bassy-bot-logger.conf new file mode 100644 index 0000000..7b9afb5 --- /dev/null +++ b/bassy-bot-logger.conf @@ -0,0 +1,5 @@ +log4perl.rootLogger=TRACE, CONSOLE + +log4perl.appender.CONSOLE=Log::Log4perl::Appender::ScreenColoredLevels +log4perl.appender.CONSOLE.layout=PatternLayout +log4perl.appender.CONSOLE.layout.ConversionPattern=[%r] %F %L %c - %m%n