Skip to content

Commit

Permalink
changed: using Log::Log4perl for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ryhmrt committed Sep 13, 2009
1 parent 1c9f2aa commit 4651fc8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
22 changes: 21 additions & 1 deletion BassyBot.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,6 +109,8 @@ sub BUILD {
my $self = shift;
$self->util->tweet("一人で焼肉食いにいってくる。18000円だけどな!");
});

$self->logger->info("bassy-bot initialized.");
}

sub _build_actions {
Expand All @@ -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();

Expand Down
3 changes: 0 additions & 3 deletions TwitterUtil.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions bassy-bot
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use strict;
use warnings;

use Getopt::Long;
use Log::Log4perl;
use BassyBot;

my $username = 'bassytime';
Expand All @@ -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;


Expand Down
5 changes: 5 additions & 0 deletions bassy-bot-logger.conf
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4651fc8

Please sign in to comment.