From e821356a6475c0a87aade0b63b7288192998a567 Mon Sep 17 00:00:00 2001 From: eleksir Date: Fri, 7 May 2021 12:49:40 +0300 Subject: [PATCH] =?UTF-8?q?Fix=20logging,=20update=20sample=20config,=20im?= =?UTF-8?q?plement=20!horse=20!=D0=BB=D0=BE=D1=88=D0=B0=D0=B4=D0=BA=D0=B0?= =?UTF-8?q?=20!frog=20!=D0=BB=D1=8F=D0=B3=D1=83=D1=88=D0=BA=D0=B0=20!snail?= =?UTF-8?q?=20!=D1=83=D0=BB=D0=B8=D1=82=D0=BA=D0=B0=20commands?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Make logging use features of Log::Any and log as it should be done via this module + Add loglevel parameter to sample config + Implement !horse !лошадка !frog !лягушка !snail !улитка commands --- bin/aleesa-jabber-bot | 43 +++++++++++++++++++++------ data/sample_config.json | 1 + lib/BotLib.pm | 61 ++++++++++++++++++++++---------------- lib/BotLib/Buni.pm | 4 +-- lib/BotLib/Conf.pm | 5 ++-- lib/BotLib/Drink.pm | 4 +-- lib/BotLib/Fortune.pm | 9 +++--- lib/BotLib/Fox.pm | 6 ++-- lib/BotLib/Friday.pm | 5 ++-- lib/BotLib/Image.pm | 35 +++++++++++++++++++++- lib/BotLib/Image/Flickr.pm | 31 +++++++++---------- lib/BotLib/Karma.pm | 10 +++---- lib/BotLib/Kitty.pm | 6 ++-- lib/BotLib/Lat.pm | 5 ++-- lib/BotLib/Monkeyuser.pm | 4 +-- lib/BotLib/Weather.pm | 7 +++-- lib/JabberBot.pm | 8 ++--- 17 files changed, 159 insertions(+), 85 deletions(-) diff --git a/bin/aleesa-jabber-bot b/bin/aleesa-jabber-bot index 11005e5..642ce51 100755 --- a/bin/aleesa-jabber-bot +++ b/bin/aleesa-jabber-bot @@ -29,6 +29,7 @@ use Cwd::utf8 qw (chdir abs_path); use POSIX qw (setgid setuid setsid); use File::Path qw (make_path); use File::Basename qw (basename dirname); +use Log::Any qw ($log); use Log::Any::Adapter; use Log::Any::For::Std; use BotLib::Conf qw (LoadConf); @@ -43,6 +44,7 @@ my $runAsUid = $UID; my $piddir = '/var/run/' . basename (abs_path $PROGRAM_NAME); my $C = LoadConf (); +my $loglevel = $C->{'loglevel'} // 'notice'; my $logfile; if (defined $C->{'log'}) { @@ -88,7 +90,19 @@ if ($UID == 0) { if (defined $logfile) { # loglevel warning suppresses logging from CHI. - Log::Any::Adapter->set ('File', $logfile, 'log_level'=>'warning'); + Log::Any::Adapter->set ('File', $logfile, log_level => $loglevel); + + # Some annoying CHI logging events come with category set to CHI::Driver and log_level set to debug. + # Until we use start using Log::Any native features and set whole loglevel accordingly, lets lower + # verbosity paticulary for CHI. + Log::Any::Adapter->set ( + { + category => 'CHI::Driver' + }, + 'File', + $logfile, + log_level => 'warn' + ); } else { # no log defined so set loglevel to the least verbose Log::Any::Adapter->set ('File', '/dev/null', 'log_level'=>'emergency'); @@ -101,14 +115,24 @@ daemonize (); my $apath = abs_path $PROGRAM_NAME; unless (defined $apath) { - die "Unable to get abs_path: $OS_ERROR\n"; + $log->fatal ("[FATAL] Unable to get abs_path: $OS_ERROR"); + exit 1; } my $pidfile = sprintf '%s/%s.pid', $piddir, basename $apath; -warn "Creating pidfile at $pidfile\n"; -open (my $pid, '>', $pidfile) or die "Error opening $pidfile: $OS_ERROR\n"; -print $pid $PID or die "Error writing to $pidfile: $OS_ERROR\n"; -close $pid; ## no critic (InputOutput::RequireCheckedSyscalls) +$log->info ("[INFO] Creating pidfile at $pidfile"); + +open (my $pid, '>', $pidfile) or do { + $log->fatal ("[FATAL] Error opening $pidfile: $OS_ERROR"); + exit 1; +}; + +print $pid $PID or do { + $log->fatal ("[FATAL] Error writing to $pidfile: $OS_ERROR"); + exit 1; +}; + +close $pid; ## no critic (InputOutput::RequireCheckedSyscalls) local $SIG{INT} = \&__signal_handler; local $SIG{TERM} = \&__signal_handler; @@ -140,18 +164,19 @@ sub __fork () { my $pid = fork; unless (defined $pid) { - die "Can't fork: $OS_ERROR\n"; + $log->fatal ("[FATAL] Can't fork: $OS_ERROR"); + exit 1; } if ($pid != 0) { - exit; + exit 0; } return; } sub __signal_handler ($) { - warn "Caught a signal $OS_ERROR\n"; + $log->info ("[INFO] Caught a signal $OS_ERROR"); if (-f $pidfile) { unlink $pidfile; diff --git a/data/sample_config.json b/data/sample_config.json index 4fc8064..3b42027 100644 --- a/data/sample_config.json +++ b/data/sample_config.json @@ -29,6 +29,7 @@ "appid": "get_it_on_openweathermap.org" }, "log" : "/var/log/aleesa-jabber-bot/aleesa-jabber-bot.log", + "loglevel": "info", "lat" : { "brain" : "data/lat/brain.sqlite", "src" : "lat.txt" diff --git a/lib/BotLib.pm b/lib/BotLib.pm index ba31092..9b2c0d9 100644 --- a/lib/BotLib.pm +++ b/lib/BotLib.pm @@ -16,7 +16,7 @@ use BotLib::Fisher qw (Fish); use BotLib::Fortune qw (Fortune); use BotLib::Fox qw (Fox); use BotLib::Friday qw (Friday); -use BotLib::Image qw (Rabbit Owl); +use BotLib::Image qw (Rabbit Owl Frog Horse Snail); use BotLib::Karma qw (KarmaGet); use BotLib::Kitty qw (Kitty); use BotLib::Lat qw (Lat); @@ -184,31 +184,34 @@ sub Command { } elsif (substr ($text, 1) eq 'help' || substr ($text, 1) eq 'помощь') { $reply = <<"EOL"; -${csign}help | ${csign}помощь - это сообщение -${csign}buni - комикс-стрип hapi buni -${csign}bunny - кролик -${csign}rabbit | ${csign}кролик - кролик -${csign}cat | ${csign}кис - кошечка -${csign}dig | ${csign}копать - заняться археологией -${csign}drink | ${csign}праздник - какой сегодня праздник? -${csign}fish | ${csign}fisher - порыбачить -${csign}рыба | ${csign}рыбка | ${csign}рыбалка - порыбачить -${csign}f | ${csign}ф - рандомная фраза из сборника цитат fortune_mod -${csign}fortune | ${csign}фортунка - рандомная фраза из сборника цитат fortune_mod -${csign}fox | ${csign}лис - лисичка -${csign}friday | ${csign}пятница - а не пятница ли сегодня? -${csign}karma фраза - посмотреть карму фразы -${csign}карма фраза - посмотреть карму фразы -фраза++ | фраза-- - повысить или понизить карму фразы -${csign}lat | ${csign}лат - сгенерировать фразу из крылатого латинского выражения -${csign}monkeyuser - комикс-стрип MonkeyUser -${csign}owl | ${csign}сова - сова -${csign}ping | ${csign}пинг - попинговать бота -${csign}some_brew - выдать соответствующий напиток, бармен может налить rum, ром, vodka, водку, beer, пиво, tequila, текила, whisky, виски, absinthe, абсент -${csign}ver | ${csign}version - написать что-то про версию ПО -${csign}версия - написать что-то про версию ПО -${csign}w город | ${csign}п город - погода в городе -${csign}xkcd - комикс-стрип с xkcb.ru +${csign}help | ${csign}помощь - это сообщение +${csign}buni - комикс-стрип hapi buni +${csign}bunny - кролик +${csign}rabbit | ${csign}кролик - кролик +${csign}cat | ${csign}кис - кошечка +${csign}dig | ${csign}копать - заняться археологией +${csign}drink | ${csign}праздник - какой сегодня праздник? +${csign}fish | ${csign}fisher - порыбачить +${csign}рыба | ${csign}рыбка | ${csign}рыбалка - порыбачить +${csign}f | ${csign}ф - рандомная фраза из сборника цитат fortune_mod +${csign}fortune | ${csign}фортунка - рандомная фраза из сборника цитат fortune_mod +${csign}fox | ${csign}лис - лисичка +${csign}friday | ${csign}пятница - а не пятница ли сегодня? +${csign}frog | ${csign}лягушка - лягушка +${csign}horse | ${csign}лошадь | ${csign}лошадка - лошадка +${csign}karma фраза - посмотреть карму фразы +${csign}карма фраза - посмотреть карму фразы +фраза++ | фраза-- - повысить или понизить карму фразы +${csign}lat | ${csign}лат - сгенерировать фразу из крылатого латинского выражения +${csign}monkeyuser - комикс-стрип MonkeyUser +${csign}owl | ${csign}сова - сова +${csign}ping | ${csign}пинг - попинговать бота +${csign}snail | ${csign}улитк а - улитка +${csign}some_brew - выдать соответствующий напиток, бармен может налить rum, ром, vodka, водку, beer, пиво, tequila, текила, whisky, виски, absinthe, абсент +${csign}ver | ${csign}version - написать что-то про версию ПО +${csign}версия - написать что-то про версию ПО +${csign}w город | ${csign}п город - погода в городе +${csign}xkcd - комикс-стрип с xkcb.ru EOL } elsif (substr ($text, 1) eq 'lat' || substr ($text, 1) eq 'лат') { @@ -217,6 +220,12 @@ EOL $reply = Kitty (); } elsif (substr ($text, 1) eq 'fox' || substr ($text, 1) eq 'лис') { $reply = Fox (); + } elsif (substr ($text, 1) eq 'frog' || substr ($text, 1) eq 'лягушка') { + $reply = Frog (); + } elsif (substr ($text, 1) eq 'horse' || substr ($text, 1) eq 'лошадь' || substr ($text, 1) eq 'лошадка') { + $reply = Horse (); + } elsif (substr ($text, 1) eq 'snail' || substr ($text, 1) eq 'улитка') { + $reply = Snail (); } elsif (substr ($text, 1) eq 'dig' || substr ($text, 1) eq 'копать') { $reply = Dig ($chattername); } elsif (substr ($text, 1) eq 'fish' || substr ($text, 1) eq 'fisher' || substr ($text, 1) eq 'рыба' || substr ($text, 1) eq 'рыбка' || substr ($text, 1) eq 'рыбалка') { diff --git a/lib/BotLib/Buni.pm b/lib/BotLib/Buni.pm index 0fee379..4c5ab41 100644 --- a/lib/BotLib/Buni.pm +++ b/lib/BotLib/Buni.pm @@ -6,9 +6,9 @@ use warnings; use utf8; use open qw (:std :utf8); use English qw ( -no_match_vars ); -use Carp qw (cluck); use HTTP::Tiny; use HTML::TokeParser; +use Log::Any qw ($log); use version; our $VERSION = qw (1.0); use Exporter qw (import); @@ -47,7 +47,7 @@ sub Buni { } while ($#{$a[0]} > 1); } } else { - cluck sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason}; + $log->warn (sprintf '[WARN] Server return status %s with message: %s', $r->{status}, $r->{reason}); } return $ret; diff --git a/lib/BotLib/Conf.pm b/lib/BotLib/Conf.pm index 9193cb4..0624101 100644 --- a/lib/BotLib/Conf.pm +++ b/lib/BotLib/Conf.pm @@ -8,6 +8,7 @@ use utf8; use open qw (:std :utf8); use English qw ( -no_match_vars ); use JSON::XS; +use Log::Any qw ($log); use version; our $VERSION = qw (1.0); use Exporter qw (import); @@ -22,12 +23,12 @@ sub LoadConf { unless ($readlen) { close $CH; ## no critic (InputOutput::RequireCheckedSyscalls - die "[FATA] Unable to read $c: $OS_ERROR\n"; + $log->fata ("[FATA] Unable to read $c: $OS_ERROR"); } if ($readlen != $len) { close $CH; ## no critic (InputOutput::RequireCheckedSyscalls - die "[FATA] File $c is $len bytes on disk, but we read only $readlen bytes\n"; + $log->fata ("[FATA] File $c is $len bytes on disk, but we read only $readlen bytes"); } close $CH; ## no critic (InputOutput::RequireCheckedSyscalls diff --git a/lib/BotLib/Drink.pm b/lib/BotLib/Drink.pm index 9254989..65c91c2 100644 --- a/lib/BotLib/Drink.pm +++ b/lib/BotLib/Drink.pm @@ -7,12 +7,12 @@ use utf8; use open qw (:std :utf8); use English qw ( -no_match_vars ); use Encode; -use Carp qw (cluck); use CHI; use CHI::Driver::BerkeleyDB; use DateTime; use HTTP::Tiny; use HTML::TokeParser; +use Log::Any qw ($log); use POSIX qw (strftime); use BotLib::Conf qw (LoadConf); @@ -66,7 +66,7 @@ sub Drink { $ret = join "\n", @holiday; } } else { - cluck sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason}; + $log->warn (sprintf '[WARN] Server return status %s with message: %s', $r->{status}, $r->{reason}); return undef; } diff --git a/lib/BotLib/Fortune.pm b/lib/BotLib/Fortune.pm index cbf87dc..4e5de18 100644 --- a/lib/BotLib/Fortune.pm +++ b/lib/BotLib/Fortune.pm @@ -7,11 +7,12 @@ use warnings; use utf8; use open qw (:std :utf8); use English qw ( -no_match_vars ); -use Carp qw (croak carp); -use SQLite_File; +use Carp qw (croak); +use File::Path qw (make_path); +use Log::Any qw ($log); use Math::Random::Secure qw (irand); use MIME::Base64; -use File::Path qw (make_path); +use SQLite_File; use BotLib::Conf qw (LoadConf); use version; our $VERSION = qw (1.0); @@ -69,7 +70,7 @@ sub Fortune () { my $backingfile = sprintf '%s/fortune.sqlite', $dir; tie my @array, 'SQLite_File', $backingfile || do { - carp "Unable to tie to $backingfile: $OS_ERROR\n"; + $log->error ("[ERROR] Unable to tie to $backingfile: $OS_ERROR"); return ''; }; diff --git a/lib/BotLib/Fox.pm b/lib/BotLib/Fox.pm index c4b980d..7f6b2d7 100644 --- a/lib/BotLib/Fox.pm +++ b/lib/BotLib/Fox.pm @@ -6,9 +6,9 @@ use warnings; use utf8; use open qw (:std :utf8); use English qw ( -no_match_vars ); -use Carp qw (carp); use JSON::XS; use HTTP::Tiny; +use Log::Any qw ($log); use version; our $VERSION = qw (1.0); use Exporter qw (import); @@ -35,7 +35,7 @@ sub Fox { }; unless (defined $jfox) { - carp "[ERROR] Unable to decode JSON: $EVAL_ERROR"; + $log->warn ("[WARN] Unable to decode JSON: $EVAL_ERROR"); } else { if ($jfox->{image}) { $jfox->{image} =~ s/\\//xmsg; @@ -43,7 +43,7 @@ sub Fox { } } } else { - carp sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason}; + $log->warn (sprintf '[WARN] Server return status %s with message: %s', $r->{status}, $r->{reason}); } return $ret; diff --git a/lib/BotLib/Friday.pm b/lib/BotLib/Friday.pm index 195ba9c..8fa4ac9 100644 --- a/lib/BotLib/Friday.pm +++ b/lib/BotLib/Friday.pm @@ -7,8 +7,9 @@ use warnings; use utf8; use open qw (:std :utf8); use English qw ( -no_match_vars ); -use Carp qw (carp croak); +use Carp qw (croak); use File::Path qw (make_path); +use Log::Any qw ($log); use Math::Random::Secure qw (irand); use SQLite_File; use BotLib::Conf qw (LoadConf); @@ -80,7 +81,7 @@ sub Friday () { my $backingfile = sprintf '%s/%s.sqlite', $dir, $dow[$today]; tie my @array, 'SQLite_File', $backingfile || do { - carp "[ERROR] Unable to tie to $backingfile: $OS_ERROR\n"; + $log->error ("[ERROR] Unable to tie to $backingfile: $OS_ERROR"); return ''; }; diff --git a/lib/BotLib/Image.pm b/lib/BotLib/Image.pm index e5622dd..2c5af44 100644 --- a/lib/BotLib/Image.pm +++ b/lib/BotLib/Image.pm @@ -9,7 +9,7 @@ use BotLib::Image::Flickr qw (FlickrByTags); use version; our $VERSION = qw (1.0); use Exporter qw (import); -our @EXPORT_OK = qw (Rabbit Owl); +our @EXPORT_OK = qw (Rabbit Owl Frog Horse Snail); sub Rabbit { # rabbit, but bunny @@ -32,6 +32,39 @@ sub Owl { } } +sub Frog { + # unable to find any one-line ascii art for frog :( + my $url = FlickrByTags ('frog,toad,amphibian'); + + if (defined $url) { + return $url; + } else { + return 'Нету лягушек, все свалили.'; + } +} + +sub Horse { + # unable to find any one-line ascii art for frog :( + my $url = FlickrByTags ('horse,equine,mammal'); + + if (defined $url) { + return $url; + } else { + return 'Нету коняшек, все разбежались.'; + } +} + +sub Snail { + # unable to find any one-line ascii art for frog :( + my $url = FlickrByTags ('snail,slug'); + + if (defined $url) { + return $url; + } else { + return 'Нету улиток, все расползлись.'; + } +} + 1; # vim: set ft=perl noet ai ts=4 sw=4 sts=4: diff --git a/lib/BotLib/Image/Flickr.pm b/lib/BotLib/Image/Flickr.pm index d33678e..da40752 100644 --- a/lib/BotLib/Image/Flickr.pm +++ b/lib/BotLib/Image/Flickr.pm @@ -11,6 +11,7 @@ use Digest::HMAC_SHA1 qw (hmac_sha1); use File::Path qw (make_path); use JSON::XS; use HTTP::Tiny; +use Log::Any qw ($log); use Math::Random::Secure qw (irand); use MIME::Base64; use SQLite_File; @@ -338,19 +339,19 @@ sub flickrSearchByText { if (defined $response) { return $response; } else { - cluck "Flickr api returns incorrect json: $r->{content}"; + $log->warn ("[WARN] Flickr api returns incorrect json: $r->{content}"); return undef; } } else { - cluck "Flickr api returns incorrect json: $r->{content}"; + $log->warn ("[WARN] Flickr api returns incorrect json: $r->{content}"); return undef; } } else { - cluck "Flickr api returns incorrect json: $r->{content}"; + $log->warn ("[WARN] Flickr api returns incorrect json: $r->{content}"); return undef; } } else { - cluck "HTTP status code not 200: $r->{status}, $r->{content}"; + $log->warn ("[WARN] HTTP status code not 200: $r->{status}, $r->{content}"); return undef; } } @@ -447,19 +448,19 @@ sub flickrSearchByTags { if (defined $response) { return $response; } else { - cluck "Flickr api returns incorrect json: $r->{content}"; + $log->warn ("[WARN] Flickr api returns incorrect json: $r->{content}"); return undef; } } else { - cluck "Flickr api returns incorrect json: $r->{content}"; + $log->warn ("[WARN] Flickr api returns incorrect json: $r->{content}"); return undef; } } else { - cluck "Flickr api returns incorrect json: $r->{content}"; + $log->warn ("[WARN] Flickr api returns incorrect json: $r->{content}"); return undef; } } else { - cluck "HTTP status code not 200: $r->{status}, $r>{contenr}"; + $log->warn ("[WARN] HTTP status code not 200: $r->{status}, $r>{content}"); return undef; } } @@ -539,7 +540,7 @@ sub FlickrTestLogin { } tie my %secret, 'SQLite_File', $backingfile || do { - say "[ERROR] Unable to tie to $backingfile: $OS_ERROR"; ## no critic (InputOutput::RequireCheckedSyscalls) + say "Unable to tie to $backingfile: $OS_ERROR"; ## no critic (InputOutput::RequireCheckedSyscalls) return 0; }; @@ -560,13 +561,13 @@ sub FlickrByTags { unless (-d $dir) { make_path ($dir) or do { - say "Unable to create $dir: $OS_ERROR"; ## no critic (InputOutput::RequireCheckedSyscalls) + $log->error ("[ERROR] Unable to create $dir: $OS_ERROR"); return 0; }; } tie my %secret, 'SQLite_File', $backingfile || do { - say "[ERROR] Unable to tie to $backingfile: $OS_ERROR"; ## no critic (InputOutput::RequireCheckedSyscalls) + $log->error ("[ERROR] Unable to tie to $backingfile: $OS_ERROR"); return 0; }; @@ -587,7 +588,7 @@ sub FlickrByTags { $item = sprintf 'https://live.staticflickr.com/%s/%s_%s_z.jpg', $item->{server}, $item->{id}, $item->{secret}; return $item; } else { - cluck 'Flickr api returns empty search result list'; + $log->notice ('[NOTICE] Flickr api returns empty search result list'); return; } } else { @@ -601,13 +602,13 @@ sub FlickrByText { unless (-d $dir) { make_path ($dir) or do { - say "Unable to create $dir: $OS_ERROR"; ## no critic (InputOutput::RequireCheckedSyscalls) + $log->error ("[ERROR] Unable to create $dir: $OS_ERROR"); return 0; }; } tie my %secret, 'SQLite_File', $backingfile || do { - say "[ERROR] Unable to tie to $backingfile: $OS_ERROR"; ## no critic (InputOutput::RequireCheckedSyscalls) + $log->error ("[ERROR] Unable to tie to $backingfile: $OS_ERROR"); return 0; }; @@ -628,7 +629,7 @@ sub FlickrByText { $item = sprintf 'https://live.staticflickr.com/%s/%s_%s_z.jpg', $item->{server}, $item->{id}, $item->{secret}; return $item; } else { - cluck 'Flickr api returns empty search result list'; + $log->notice ('[NOTICE] Flickr api returns empty search result list'); return; } } else { diff --git a/lib/BotLib/Karma.pm b/lib/BotLib/Karma.pm index bd475a0..8f2790b 100644 --- a/lib/BotLib/Karma.pm +++ b/lib/BotLib/Karma.pm @@ -6,9 +6,9 @@ use warnings; use utf8; use open qw (:std :utf8); use English qw ( -no_match_vars ); -use Carp qw (carp); use DB_File; use File::Path qw (make_path); +use Log::Any qw ($log); use BotLib::Conf qw (LoadConf); use BotLib::Util qw (utf2sha1 trim); @@ -34,14 +34,14 @@ sub KarmaSet (@) { unless (-d $karmadir) { make_path ($karmadir) or do { - carp "Unable to create $karmadir: $OS_ERROR"; + $log->error ("[ERROR] Unable to create $karmadir: $OS_ERROR"); return sprintf 'Карма %s составляет 0', $phrase; }; } # init hash, store phrase and score tie my %karma, 'DB_File', $karmafile || do { - carp "[ERROR] Something nasty happen when karma ties to its data: $OS_ERROR"; + $log->error ("[ERROR] Something nasty happen when karma ties to its data: $OS_ERROR"); return sprintf 'Карма %s составляет 0', $phrase; }; @@ -94,14 +94,14 @@ sub KarmaGet (@) { unless (-d $karmadir) { make_path ($karmadir) or do { - carp "Unable to create $karmadir: $OS_ERROR"; + $log->error ("Unable to create $karmadir: $OS_ERROR"); return sprintf 'Карма %s составляет 0', $phrase; }; } # init hash, store phrase and score tie my %karma, 'DB_File', $karmafile || do { - carp "[ERROR] Something nasty happen when karma ties to its data: $OS_ERROR"; + $log->error ("[ERROR] Something nasty happen when karma ties to its data: $OS_ERROR"); return sprintf 'Карма %s составляет 0', $phrase; }; diff --git a/lib/BotLib/Kitty.pm b/lib/BotLib/Kitty.pm index a27d384..3825e2b 100644 --- a/lib/BotLib/Kitty.pm +++ b/lib/BotLib/Kitty.pm @@ -6,9 +6,9 @@ use warnings; use utf8; use open qw (:std :utf8); use English qw ( -no_match_vars ); -use Carp qw (carp); use JSON::XS; use HTTP::Tiny; +use Log::Any qw ($log); use version; our $VERSION = qw (1.0); use Exporter qw (import); @@ -35,14 +35,14 @@ sub Kitty { }; unless (defined $jcat) { - carp "[ERROR] Unable to decode JSON: $EVAL_ERROR"; + $log->error ("[ERROR] Unable to decode JSON: $EVAL_ERROR"); } else { if ($jcat->[0]->{url}) { $ret = $jcat->[0]->{url}; } } } else { - carp sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason}; + $log->error (sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason}); } return $ret; diff --git a/lib/BotLib/Lat.pm b/lib/BotLib/Lat.pm index c40eda1..f8ef84f 100644 --- a/lib/BotLib/Lat.pm +++ b/lib/BotLib/Lat.pm @@ -11,6 +11,7 @@ use Carp qw (carp); use File::Basename qw (dirname); use File::Path qw (make_path); use Hailo; +use Log::Any qw ($log); use BotLib::Conf qw (LoadConf); use version; our $VERSION = qw (1.0); @@ -49,12 +50,12 @@ sub Lat () { my $braindir = dirname $brain; unless (-d $braindir) { - carp "[ERROR] No lat module data: $braindir is absent! Train lat first."; + $log->error ("[ERROR] No lat module data: $braindir is absent! Train lat first."); return ''; } unless (-f $brain) { - carp "[ERROR] No lat module data: $brain is absent! Train lat first."; + $log->error ("[ERROR] No lat module data: $brain is absent! Train lat first."); return ''; } diff --git a/lib/BotLib/Monkeyuser.pm b/lib/BotLib/Monkeyuser.pm index ab64408..1ec70ac 100644 --- a/lib/BotLib/Monkeyuser.pm +++ b/lib/BotLib/Monkeyuser.pm @@ -6,9 +6,9 @@ use warnings; use utf8; use open qw (:std :utf8); use English qw ( -no_match_vars ); -use Carp qw (cluck); use HTTP::Tiny; use HTML::TokeParser; +use Log::Any qw ($log); use Math::Random::Secure qw (irand); use version; our $VERSION = qw (1.0); @@ -51,7 +51,7 @@ sub Monkeyuser { $ret = sprintf 'https://www.monkeyuser.com%s', $link[irand (1 + $#link)]; } } else { - cluck sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason}; + $log->warn (sprintf '[WARN] Monkeyuser server return status %s with message: %s', $r->{status}, $r->{reason}); } return $ret; diff --git a/lib/BotLib/Weather.pm b/lib/BotLib/Weather.pm index fee713a..caaede8 100644 --- a/lib/BotLib/Weather.pm +++ b/lib/BotLib/Weather.pm @@ -11,6 +11,7 @@ use CHI; use CHI::Driver::BerkeleyDB; use HTTP::Tiny; use JSON::XS; +use Log::Any qw ($log); use BotLib::Conf qw (LoadConf); use BotLib::Util qw (trim urlencode); @@ -33,7 +34,7 @@ sub Weather { my $appid = $c->{openweathermap}->{appid}; unless (defined $appid) { - carp 'No appid specified for openweathermap'; + $log->warn ('[WARN] No appid specified for openweathermap'); return undef; } @@ -60,11 +61,11 @@ sub Weather { }; unless (defined $fc) { - carp "[WARN] openweathermap returns corrupted json: $EVAL_ERROR"; + $log->warn ("[WARN] openweathermap returns corrupted json: $EVAL_ERROR"); return undef; }; } else { - carp sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason}; + $log->warn (sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason}); return undef; } diff --git a/lib/JabberBot.pm b/lib/JabberBot.pm index b5ab347..941d4ba 100644 --- a/lib/JabberBot.pm +++ b/lib/JabberBot.pm @@ -9,9 +9,9 @@ use warnings; use utf8; use open qw (:std :utf8); use English qw ( -no_match_vars ); -use Carp qw (carp); use File::Path qw (make_path); use Hailo; +use Log::Any qw ($log); use Net::Jabber::Bot; use BotLib qw (Command RandomCommonPhrase RealJID); use BotLib::Conf qw (LoadConf); @@ -68,7 +68,7 @@ sub __new_bot_message { unless (-d $braindir) { make_path ($braindir) or do { - carp "[ERROR] Unable to create $braindir: $OS_ERROR"; + $log->error ("[ERROR] Unable to create $braindir: $OS_ERROR"); return; }; } @@ -79,9 +79,9 @@ sub __new_bot_message { ); if ($hash{'type'} eq 'groupchat') { - carp "[INFO] Initialized brain for chat $hash{'reply_to'}: $brainname"; + $log->info ("[INFO] Initialized brain for chat $hash{'reply_to'}: $brainname"); } else { - carp "[INFO] Initialized brain for chat $jid{'jid'}: $brainname"; + $log->info ("[INFO] Initialized brain for chat $jid{'jid'}: $brainname"); } }