Make logging use features of Log::Any and log as it should be done via this module. Cleanup sample config.

This commit is contained in:
eleksir
2021-05-04 18:04:32 +03:00
parent 095cb57a18
commit 096877bee2
15 changed files with 104 additions and 78 deletions

View File

@@ -29,6 +29,7 @@ no Cwd;
use Cwd::utf8 qw (chdir abs_path); use Cwd::utf8 qw (chdir abs_path);
use File::Basename qw (basename dirname); use File::Basename qw (basename dirname);
use File::Path qw (make_path); use File::Path qw (make_path);
use Log::Any qw ($log);
use Log::Any::Adapter; use Log::Any::Adapter;
use Log::Any::For::Std; use Log::Any::For::Std;
use POSIX qw (setgid setuid setsid); use POSIX qw (setgid setuid setsid);
@@ -44,6 +45,7 @@ my $runAsUid = $UID;
my $piddir = '/var/run/' . basename (abs_path ($PROGRAM_NAME)); my $piddir = '/var/run/' . basename (abs_path ($PROGRAM_NAME));
my $C = LoadConf (); my $C = LoadConf ();
my $loglevel = $C->{'loglevel'} // 'notice';
my $logfile; my $logfile;
if (defined $C->{'log'}) { if (defined $C->{'log'}) {
@@ -92,7 +94,7 @@ if ($UID == 0) {
} }
if (defined $logfile) { if (defined $logfile) {
Log::Any::Adapter->set ('File', $logfile); 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. # 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 # Until we use start using Log::Any native features and set whole loglevel accordingly, lets lower
@@ -116,14 +118,24 @@ daemonize ();
my $apath = abs_path $PROGRAM_NAME; my $apath = abs_path $PROGRAM_NAME;
unless (defined $apath) { unless (defined $apath) {
die "[FATAL] Unable to get abs_path: $OS_ERROR\n"; $log->fatal ("[FATAL] Unable to get abs_path: $OS_ERROR\n");
exit 1;
}; };
# write pidfile # write pidfile
my $pidfile = sprintf '%s/%s.pid', $piddir, basename $apath; my $pidfile = sprintf '%s/%s.pid', $piddir, basename $apath;
warn "[INFO] Creating pidfile at $pidfile\n"; $log->info ("[INFO] Creating pidfile at $pidfile");
open my $MYPID, '>', $pidfile or die "[FATAL] Error opening $pidfile: $OS_ERROR\n";
print {$MYPID} $PID or die "[FATAL] Error writing to $pidfile: $OS_ERROR\n"; open my $MYPID, '>', $pidfile or do {
$log->fatal ("[FATAL] Error opening $pidfile: $OS_ERROR");
exit 1;
};
print {$MYPID} $PID or do {
$log->fatal ("[FATAL] Error writing to $pidfile: $OS_ERROR");
exit 1;
};
close $MYPID; ## no critic (InputOutput::RequireCheckedSyscalls close $MYPID; ## no critic (InputOutput::RequireCheckedSyscalls
local $SIG{INT} = \&__signal_handler; local $SIG{INT} = \&__signal_handler;
@@ -157,7 +169,8 @@ sub __fork {
my $pid = fork; my $pid = fork;
unless (defined $pid) { unless (defined $pid) {
die "[FATAL] Can't fork: $OS_ERROR\n"; $log->fatal ("[FATAL] Can't fork: $OS_ERROR");
exit 1;
} }
if ($pid != 0) { if ($pid != 0) {
@@ -168,7 +181,7 @@ sub __fork {
} }
sub __signal_handler { sub __signal_handler {
warn "[INFO] Caught a signal $OS_ERROR\n"; $log->info ("[INFO] Caught a signal $OS_ERROR");
if (-f $pidfile) { if (-f $pidfile) {
unlink $pidfile; unlink $pidfile;

View File

@@ -1,10 +1,5 @@
{ {
"admin" : { # Cachedir used not only for caches but for some settings and stats too.
# store per-channel (per-conversation) settings
"dir" : "data/admin"
},
# cachedir used not only for caches but for somesettings and stats too
"cachedir" : "data/cache", "cachedir" : "data/cache",
"fortune" : { "fortune" : {
@@ -18,16 +13,15 @@
}, },
"image": { "image": {
"dir" : "data/image",
"imgur" : { "imgur" : {
# see README.md about these settings - auth'ed search settings # See README.md about these settings - auth'ed search settings.
"client-id" : "imgur api client id", "client-id" : "imgur api client id",
"secret" : "secret for client id", "secret" : "secret for client id",
"access_token" : "access token for querying imgur api", "access_token" : "access token for querying imgur api",
"refresh_token" : "token for refreshing access token" "refresh_token" : "token for refreshing access token"
}, },
"flickr" : { "flickr" : {
# see README.md about these settings - auth'ed search settings # See README.md about these settings - auth'ed search settings.
"consumer_key" : "md5-like_hash", "consumer_key" : "md5-like_hash",
"consumer_secret" : "some_hash-like_string", "consumer_secret" : "some_hash-like_string",
"verifier": "some_hash-like_string" "verifier": "some_hash-like_string"
@@ -39,24 +33,31 @@
"src": "data/lat.txt" "src": "data/lat.txt"
}, },
# required for debugging reasons, if commented out or not exist, no debug logging appears at all # Required for debugging reasons, if commented out or not exist, no debug logging appears at all.
"log" : "/var/log/aleesa-telegram-bot/debug.log", "log" : "/var/log/aleesa-telegram-bot/aleesa.log",
# openwatermap plugin # Verbosity can be trace, debug, info (inform), notice, warning (warn), error (err), critical (crit, fatal), alert, emergency.
# Currently implemented debug, info, notice, warn, error, fatal. Notice is default logging level.
"loglevel": "notice",
# Openwatermap plugin.
"openweathermap" : { "openweathermap" : {
"appid" : "md5sum-like_string", "appid" : "md5sum-like_string",
}, },
# stuff for telegram itself # Stuff for telegram itself.
"telegrambot" : { "telegrambot" : {
# directory with per-conversation "brains"
# Directory with per-conversation "brains".
"braindir" : "data/telegrambot-brains", "braindir" : "data/telegrambot-brains",
# prefix-sign for all commands. in many irc-bots (eggdrop/windrop) it is "." (dot), in sulci it was "!"
# in telegram commands begin with "/" on the start of first (and only) line. # Prefix-sign for all commands. In many irc-bots (eggdrop/windrop) it is "." (dot), in sulci it was "!".
# In telegram commands begin with "/" on the start of first (and only) line. In case of this bot you can define what to use.
"csign" : "!", "csign" : "!",
"token" : "Long_string_given_by_@BotFather" "token" : "Long_string_given_by_@BotFather"
}, },
# unprivileged user to run this bot from # Unprivileged user to run this bot from.
"user" : "unit", "user" : "unit",
} }

View File

@@ -184,7 +184,7 @@ sub Command {
sleep (irand (2) + 1); sleep (irand (2) + 1);
$msg->replyMd ($reply); $msg->replyMd ($reply);
return; return;
} elsif (substr ($text, 1) eq 'хэлп') { } elsif (substr ($text, 1) eq 'хэлп' || substr ($text, 1) eq 'хелп') {
$reply = 'HALP!!!11'; $reply = 'HALP!!!11';
} elsif (substr ($text, 1) eq 'help' || substr ($text, 1) eq 'помощь') { } elsif (substr ($text, 1) eq 'help' || substr ($text, 1) eq 'помощь') {
$reply = << "MYHELP"; $reply = << "MYHELP";

View File

@@ -8,6 +8,7 @@ use open qw (:std :utf8);
use English qw ( -no_match_vars ); use English qw ( -no_match_vars );
use Carp qw (cluck); use Carp qw (cluck);
use HTML::TokeParser; use HTML::TokeParser;
use Log::Any qw ($log);
use Mojo::UserAgent; use Mojo::UserAgent;
use version; our $VERSION = qw (1.0); use version; our $VERSION = qw (1.0);
@@ -48,7 +49,7 @@ sub Buni {
} while ($#{$a[0]} > 1); } while ($#{$a[0]} > 1);
} }
} else { } else {
cluck sprintf 'Server return status %s with message: %s', $r->code, $r->message; $log->warn (sprintf '[WARN] Bunicomic server return status %s with message: %s', $r->code, $r->message);
} }
return $ret; return $ret;

View File

@@ -12,6 +12,7 @@ use English qw ( -no_match_vars );
use Encode; use Encode;
use Carp qw (cluck); use Carp qw (cluck);
use HTML::TokeParser; use HTML::TokeParser;
use Log::Any qw ($log);
use Mojo::Log; use Mojo::Log;
use Mojo::UserAgent::Cached; use Mojo::UserAgent::Cached;
use POSIX qw (strftime); use POSIX qw (strftime);
@@ -103,7 +104,7 @@ sub Drink {
$ret = join "\n", @holyday; $ret = join "\n", @holyday;
} }
} else { } else {
cluck sprintf 'Server return status %s with message: %s', $r->code, $r->message; $log->warn (sprintf '[WARN] Kakoysegodnyaprazdnik server return status %s with message: %s', $r->code, $r->message);
} }
return $ret; return $ret;

View File

@@ -9,6 +9,7 @@ use open qw (:std :utf8);
use English qw ( -no_match_vars ); use English qw ( -no_match_vars );
use Carp qw (cluck croak); use Carp qw (cluck croak);
use File::Path qw (make_path); use File::Path qw (make_path);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand); use Math::Random::Secure qw (irand);
use SQLite_File; use SQLite_File;
use BotLib::Conf qw (LoadConf); use BotLib::Conf qw (LoadConf);
@@ -69,7 +70,7 @@ sub Fortune () {
my $backingfile = sprintf '%s/fortune.sqlite', $dir; my $backingfile = sprintf '%s/fortune.sqlite', $dir;
tie my @array, 'SQLite_File', $backingfile || do { tie my @array, 'SQLite_File', $backingfile || do {
cluck "[ERROR] Unable to tie to $backingfile: $OS_ERROR"; $log->error ("[ERROR] Unable to tie to $backingfile: $OS_ERROR");
return ''; return '';
}; };

View File

@@ -9,6 +9,7 @@ use open qw (:std :utf8);
use English qw ( -no_match_vars ); use English qw ( -no_match_vars );
use Carp qw (cluck croak); use Carp qw (cluck croak);
use File::Path qw (make_path); use File::Path qw (make_path);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand); use Math::Random::Secure qw (irand);
use SQLite_File; use SQLite_File;
use BotLib::Conf qw (LoadConf); use BotLib::Conf qw (LoadConf);
@@ -80,7 +81,7 @@ sub Friday () {
my $backingfile = sprintf '%s/%s.sqlite', $dir, $dow[$today]; my $backingfile = sprintf '%s/%s.sqlite', $dir, $dow[$today];
tie my @array, 'SQLite_File', $backingfile || do { tie my @array, 'SQLite_File', $backingfile || do {
cluck "[ERROR] Unable to tie to $backingfile: $OS_ERROR\n"; $log->error ("[ERROR] Unable to tie to $backingfile: $OS_ERROR\n");
return ''; return '';
}; };

View File

@@ -7,6 +7,7 @@ use utf8;
use open qw (:std :utf8); use open qw (:std :utf8);
use English qw ( -no_match_vars ); use English qw ( -no_match_vars );
use Carp qw (cluck); use Carp qw (cluck);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand); use Math::Random::Secure qw (irand);
use Mojo::UserAgent; use Mojo::UserAgent;
use BotLib::Image::Flickr qw (FlickrByTags); use BotLib::Image::Flickr qw (FlickrByTags);
@@ -38,15 +39,15 @@ sub Kitty {
}; };
unless (defined $jcat) { unless (defined $jcat) {
cluck "[ERROR] Unable to decode JSON: $EVAL_ERROR"; $log->warn ("[WARN] Unable to decode JSON from thecatapi: $EVAL_ERROR");
} else { } else {
if ($jcat->[0]->{url}) { if ($jcat->[0]->{url}) {
my @cats = ('龴ↀ◡ↀ龴', '=^..^=', '≧◔◡◔≦ ','^ↀᴥↀ^' ); my @cats = ('龴ↀ◡ↀ龴', '=^..^=', '≧◔◡◔≦ ','^ↀᴥↀ^');
$ret = sprintf '[%s](%s)', $cats[irand ($#cats + 1)], $jcat->[0]->{url}; $ret = sprintf '[%s](%s)', $cats[irand ($#cats + 1)], $jcat->[0]->{url};
} }
} }
} else { } else {
cluck sprintf 'Server return status %s with message: %s', $r->code, $r->message; $log->warn (sprintf '[WARN] Thecatapi server return status %s with message: %s', $r->code, $r->message);
} }
return $ret; return $ret;
@@ -73,7 +74,7 @@ sub Fox {
}; };
unless (defined $jfox) { unless (defined $jfox) {
cluck "[ERROR] Unable to decode JSON: $EVAL_ERROR"; $log->warn ("[WARN] Unable to decode JSON from randomfox: $EVAL_ERROR");
} else { } else {
if ($jfox->{image}) { if ($jfox->{image}) {
$jfox->{image} =~ s/\\//xmsg; $jfox->{image} =~ s/\\//xmsg;
@@ -81,7 +82,7 @@ sub Fox {
} }
} }
} else { } else {
cluck sprintf 'Server return status %s with message: %s', $r->code, $r->message; $log->warn (sprintf '[WARN] Randomfox server return status %s with message: %s', $r->code, $r->message);
} }
return $ret; return $ret;
@@ -108,7 +109,7 @@ sub Oboobs {
}; };
unless (defined $joboobs) { unless (defined $joboobs) {
cluck "[ERROR] Unable to decode JSON: $EVAL_ERROR"; $log->warn ("[WARN] Unable to decode JSON from oboobs: $EVAL_ERROR");
} else { } else {
if ($joboobs->[0]->{preview}) { if ($joboobs->[0]->{preview}) {
my @boobs = ('(. )( .)', '( . Y . )', '(o)(o)', '( @ )( @ )', '(.)(.)'); my @boobs = ('(. )( .)', '( . Y . )', '(o)(o)', '( @ )( @ )', '(.)(.)');
@@ -116,7 +117,7 @@ sub Oboobs {
} }
} }
} else { } else {
cluck sprintf 'Server return status %s with message: %s', $r->code, $r->message; $log->warn (sprintf '[WARN] Oboobs server return status %s with message: %s', $r->code, $r->message);
} }
return $ret; return $ret;
@@ -143,7 +144,7 @@ sub Obutts {
}; };
unless (defined $jobutts) { unless (defined $jobutts) {
cluck "[ERROR] Unable to decode JSON: $EVAL_ERROR"; $log->warn ("[ERROR] Unable to decode JSON from obutts: $EVAL_ERROR");
} else { } else {
if ($jobutts->[0]->{preview}) { if ($jobutts->[0]->{preview}) {
my @butts = ('(__(__)', '(_!_)', '(__.__)'); my @butts = ('(__(__)', '(_!_)', '(__.__)');
@@ -151,7 +152,7 @@ sub Obutts {
} }
} }
} else { } else {
cluck sprintf 'Server return status %s with message: %s', $r->code, $r->message; $log->warn (sprintf '[WARN] Obutts server return status %s with message: %s', $r->code, $r->message);
} }
return $ret; return $ret;

View File

@@ -10,6 +10,7 @@ use Carp qw (cluck);
use CHI; use CHI;
use CHI::Driver::BerkeleyDB; use CHI::Driver::BerkeleyDB;
use Digest::HMAC_SHA1 qw (hmac_sha1); use Digest::HMAC_SHA1 qw (hmac_sha1);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand); use Math::Random::Secure qw (irand);
use MIME::Base64; use MIME::Base64;
use Mojo::UserAgent; use Mojo::UserAgent;
@@ -188,7 +189,7 @@ sub flickrAuthorization {
if ($r->code == 302) { if ($r->code == 302) {
return $r->content->headers->location; return $r->content->headers->location;
} else { } else {
printf 'Something went wrong: %s %s', $r->code, $r->message; printf "Something went wrong: %s %s\n", $r->code, $r->message;
return; return;
} }
} }
@@ -335,19 +336,19 @@ sub flickrSearchByText {
if (defined $response) { if (defined $response) {
return $response; return $response;
} else { } else {
cluck "Flickr api returns incorrect json: $r->message"; $log->warn ("[WARN] Flickr api returns incorrect json: $r->message");
return undef; return undef;
} }
} else { } else {
cluck "Flickr api returns incorrect json: $r->message"; $log->warn ("[WARN] Flickr api returns incorrect json: $r->message");
return undef; return undef;
} }
} else { } else {
cluck "Flickr api returns incorrect json: $r->message"; $log->warn ("[WARN] Flickr api returns incorrect json: $r->message");
return undef; return undef;
} }
} else { } else {
cluck "HTTP status code not 200: $r->code, $r->message"; $log->warn ("[WARN] Flickr api HTTP status code not 200: $r->code, $r->message");
return undef; return undef;
} }
} }
@@ -442,19 +443,19 @@ sub flickrSearchByTags {
if (defined $response) { if (defined $response) {
return $response; return $response;
} else { } else {
cluck "Flickr api returns incorrect json: $r->message"; $log->warn ("[WARN] Flickr api returns incorrect json: $r->message");
return undef; return undef;
} }
} else { } else {
cluck "Flickr api returns incorrect json: $r->message"; $log->warn ("[WARN] Flickr api returns incorrect json: $r->message");
return undef; return undef;
} }
} else { } else {
cluck "Flickr api returns incorrect json: $r->message"; $log->warn ("[WARN] Flickr api returns incorrect json: $r->message");
return undef; return undef;
} }
} else { } else {
cluck "HTTP status code not 200: $r->code, $r->message"; $log->warn ("[WARN] Flickr api HTTP status code not 200: $r->code, $r->message");
return undef; return undef;
} }
} }
@@ -554,7 +555,7 @@ sub FlickrByTags {
$item = sprintf 'https://live.staticflickr.com/%s/%s_%s_z.jpg', $item->{server}, $item->{id}, $item->{secret}; $item = sprintf 'https://live.staticflickr.com/%s/%s_%s_z.jpg', $item->{server}, $item->{id}, $item->{secret};
return $item; return $item;
} else { } else {
cluck 'Flickr api returns empty search result list'; $log->notice ('[NOTICE] Flickr api returns empty search result list');
return; return;
} }
} else { } else {
@@ -587,7 +588,7 @@ sub FlickrByText {
$item = sprintf 'https://live.staticflickr.com/%s/%s_%s_z.jpg', $item->{server}, $item->{id}, $item->{secret}; $item = sprintf 'https://live.staticflickr.com/%s/%s_%s_z.jpg', $item->{server}, $item->{id}, $item->{secret};
return $item; return $item;
} else { } else {
cluck 'Flickr api returns empty search result list'; $log->notice ('[NOTICE] Flickr api returns empty search result list');
return; return;
} }
} else { } else {

View File

@@ -9,6 +9,7 @@ use English qw ( -no_match_vars );
use Carp qw (cluck); use Carp qw (cluck);
use CHI; use CHI;
use CHI::Driver::BerkeleyDB; use CHI::Driver::BerkeleyDB;
use Log::Any qw ($log);
use Math::Random::Secure qw (irand); use Math::Random::Secure qw (irand);
use Mojo::UserAgent; use Mojo::UserAgent;
@@ -28,19 +29,19 @@ sub Imgur {
my $search_url = 'https://api.imgur.com/3/gallery/search/'; my $search_url = 'https://api.imgur.com/3/gallery/search/';
my $imgur_client_id = $c->{image}->{imgur}->{client_id} // do { my $imgur_client_id = $c->{image}->{imgur}->{client_id} // do {
cluck 'No client_id specified in config for imgur module'; $log->error ('[ERROR] No client_id specified in config for imgur module');
return undef; return undef;
}; };
my $imgur_client_secret = $c->{image}->{imgur}->{client_secret} // do { my $imgur_client_secret = $c->{image}->{imgur}->{client_secret} // do {
cluck 'No client_secret specified in config for imgur module'; $log->error ('[ERROR] No client_secret specified in config for imgur module');
return undef; return undef;
}; };
my $imgur_access_token = $c->{image}->{imgur}->{access_token} // do { my $imgur_access_token = $c->{image}->{imgur}->{access_token} // do {
cluck 'No access_token specified in config for imgur module'; $log->error ('[ERROR] No access_token specified in config for imgur module');
return undef; return undef;
}; };
my $imgur_refresh_token = $c->{image}->{imgur}->{refresh_token} // do { my $imgur_refresh_token = $c->{image}->{imgur}->{refresh_token} // do {
cluck 'No refresh_token specified in config for imgur module'; $log->warn ('[WARN] No refresh_token specified in config for imgur module');
}; };
# N.B. Both imgur_access_token and imgur_refresh_token time to time can change, so we need # N.B. Both imgur_access_token and imgur_refresh_token time to time can change, so we need
@@ -81,7 +82,7 @@ sub Imgur {
); );
unless ($r->is_success) { unless ($r->is_success) {
cluck "Unable to refresh imgur token, please re-authorise app! or imgur experiencing api outage: $r->code $r->message"; $log->error ("[ERROR] Unable to refresh imgur token, please re-authorise app! or imgur experiencing api outage: $r->code $r->message");
return undef; return undef;
} }
@@ -93,7 +94,7 @@ sub Imgur {
$imgur_access_token = $refresh->{access_token}; $imgur_access_token = $refresh->{access_token};
$cache->set ('imgur_access_token', $refresh->{access_token}, 'never'); $cache->set ('imgur_access_token', $refresh->{access_token}, 'never');
} else { } else {
cluck "Unable to refresh imgur access_token, unable to decode json: $EVAL_ERROR"; $log->warn ("[ERROR] Unable to refresh imgur access_token, unable to decode json: $EVAL_ERROR");
return undef; return undef;
} }
@@ -130,19 +131,19 @@ sub Imgur {
if ($#urls > 0) { if ($#urls > 0) {
return $urls[irand ($#urls + 1)]; return $urls[irand ($#urls + 1)];
} else { } else {
cluck 'Imgur api returned empty result.'; $log->notice ('[NOTICE] Imgur api returned empty result.');
return undef; return undef;
} }
} else { } else {
cluck "Unable to get results from imgur api: $r->message"; $log->warn ("[WARN] Unable to get results from imgur api: $r->message");
return undef; return undef;
} }
} else { } else {
cluck "Unable to decode imgur api json: $EVAL_ERROR"; $log->warn ("[WARN] Unable to decode imgur api json: $EVAL_ERROR");
return undef; return undef;
} }
} else { } else {
cluck "Unable to get results from imgur api: $r->code, $r->message"; $log->warn ("[WARN] Unable to get results from imgur api: $r->code, $r->message");
return undef; return undef;
} }
} }

View File

@@ -9,6 +9,7 @@ use English qw ( -no_match_vars );
use Carp qw (cluck); use Carp qw (cluck);
use CHI; use CHI;
use CHI::Driver::BerkeleyDB; use CHI::Driver::BerkeleyDB;
use Log::Any qw ($log);
use BotLib::Conf qw (LoadConf); use BotLib::Conf qw (LoadConf);
use version; our $VERSION = qw (1.0); use version; our $VERSION = qw (1.0);
@@ -52,7 +53,7 @@ sub KarmaSet (@) {
} }
if ($cache->set ($phrase, $score, 'never') != $score) { if ($cache->set ($phrase, $score, 'never') != $score) {
cluck 'Cache error: unable to set karma.'; $log->error ('[ERROR] Cache error: unable to set karma.');
} }
if ($score < -1 && (($score % (0 - $max)) + 1) == 0) { if ($score < -1 && (($score % (0 - $max)) + 1) == 0) {

View File

@@ -11,6 +11,7 @@ use Carp qw (cluck);
use File::Basename qw (dirname); use File::Basename qw (dirname);
use File::Path qw (make_path); use File::Path qw (make_path);
use Hailo; use Hailo;
use Log::Any qw ($log);
use BotLib::Conf qw (LoadConf); use BotLib::Conf qw (LoadConf);
use version; our $VERSION = qw (1.0); use version; our $VERSION = qw (1.0);
@@ -49,12 +50,12 @@ sub Lat () {
my $braindir = dirname $brain; my $braindir = dirname $brain;
unless (-d $braindir) { unless (-d $braindir) {
cluck "[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 ''; return '';
} }
unless (-f $brain) { unless (-f $brain) {
cluck "[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 ''; return '';
} }

View File

@@ -8,6 +8,7 @@ use open qw (:std :utf8);
use English qw ( -no_match_vars ); use English qw ( -no_match_vars );
use Carp qw (cluck); use Carp qw (cluck);
use HTML::TokeParser; use HTML::TokeParser;
use Log::Any qw ($log);
use Math::Random::Secure qw (irand); use Math::Random::Secure qw (irand);
use Mojo::UserAgent; use Mojo::UserAgent;
@@ -51,7 +52,7 @@ sub Monkeyuser {
$ret = sprintf '[MonkeyUser](https://www.monkeyuser.com%s)', $link [irand (1 + $#link)]; $ret = sprintf '[MonkeyUser](https://www.monkeyuser.com%s)', $link [irand (1 + $#link)];
} }
} else { } 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; return $ret;

View File

@@ -9,6 +9,7 @@ use English qw ( -no_match_vars );
use Carp qw (carp cluck); use Carp qw (carp cluck);
use CHI; use CHI;
use CHI::Driver::BerkeleyDB; use CHI::Driver::BerkeleyDB;
use Log::Any qw ($log);
use Mojo::Log; use Mojo::Log;
use Mojo::UserAgent::Cached; use Mojo::UserAgent::Cached;
use BotLib::Conf qw (LoadConf); use BotLib::Conf qw (LoadConf);
@@ -109,11 +110,11 @@ sub __weather {
}; };
unless ($fc) { unless ($fc) {
cluck "[WARN] openweathermap returns corrupted json: $EVAL_ERROR"; $log->warn ("[WARN] openweathermap returns corrupted json: $EVAL_ERROR");
return undef; return undef;
}; };
} else { } else {
cluck sprintf 'Server return status %s with message: %s', $r->code, $r->message; $log->warn (sprintf '[WARN] Server return status %s with message: %s', $r->code, $r->message);
return undef; return undef;
} }

View File

@@ -11,6 +11,7 @@ use English qw ( -no_match_vars );
use Carp qw (cluck carp); use Carp qw (cluck carp);
use File::Path qw (make_path); use File::Path qw (make_path);
use Hailo; use Hailo;
use Log::Any qw($log);
use Math::Random::Secure qw (irand); use Math::Random::Secure qw (irand);
use Mojo::Base 'Teapot::Bot::Brain'; use Mojo::Base 'Teapot::Bot::Brain';
use BotLib::Admin qw (FortuneToggleList); use BotLib::Admin qw (FortuneToggleList);
@@ -72,7 +73,7 @@ sub __on_msg {
my $myObj = $self->getMe (); my $myObj = $self->getMe ();
unless ($myObj) { unless ($myObj) {
carp '[ERROR] Unable to get chatid, API Error?'; $log->error ('[ERROR] Unable to get chatid, API Error?');
return; return;
} }
@@ -105,7 +106,7 @@ sub __on_msg {
} }
} }
} else { } else {
carp '[INFO] Unable to get chatid'; $log->warn ('[INFO] Unable to get chatid');
return; return;
} }
@@ -158,9 +159,9 @@ sub __on_msg {
); );
if ($chatid < 0) { if ($chatid < 0) {
carp "[INFO] Initialized brain for public chat $chatname ($chatid): $brainname"; $log->info ("[INFO] Initialized brain for public chat $chatname ($chatid): $brainname");
} else { } else {
carp "[INFO] Initialized brain for private chat $chatname ($chatid): $brainname"; $log->info ("[INFO] Initialized brain for private chat $chatname ($chatid): $brainname");
} }
} }
@@ -171,7 +172,7 @@ sub __on_msg {
} }
my $text = $msg->text; my $text = $msg->text;
carp sprintf ('[DEBUG] Private chat %s say to bot: %s', $vis_a_vi, $text) if $c->{debug}; $log->debug (sprintf ('[DEBUG] Private chat %s say to bot: %s', $vis_a_vi, $text));
my $reply = 'Давайте ещё пообщаемся, а то я ещё не научилась от вас плохому.'; my $reply = 'Давайте ещё пообщаемся, а то я ещё не научилась от вас плохому.';
if (substr ($text, 0, 1) eq $csign) { if (substr ($text, 0, 1) eq $csign) {
@@ -213,7 +214,7 @@ sub __on_msg {
if (defined $reply) { if (defined $reply) {
$msg->typing (); $msg->typing ();
sleep 1; sleep 1;
carp sprintf ('[DEBUG] Private chat bot reply to %s: %s', $vis_a_vi, $reply) if $c->{debug}; $log->debug (sprintf ('[DEBUG] Private chat bot reply to %s: %s', $vis_a_vi, $reply));
$msg->reply ($reply); $msg->reply ($reply);
} }
# group chat # group chat
@@ -221,26 +222,26 @@ sub __on_msg {
my $reply; my $reply;
if (IsCensored $msg) { if (IsCensored $msg) {
carp sprintf '[INFO] In public chat %s (%s) message from %s was censored', $chatname, $chatid, $vis_a_vi; $log->info (sprintf '[INFO] In public chat %s (%s) message from %s was censored', $chatname, $chatid, $vis_a_vi);
$self->deleteMessage ({chat_id => $chatid, message_id => $msg->{message_id}}); $self->deleteMessage ({chat_id => $chatid, message_id => $msg->{message_id}});
} }
# detect and log messages without text, noop here # detect and log messages without text, noop here
unless (defined $msg->text) { unless (defined $msg->text) {
carp sprintf ('[DEBUG] No text in message from %s', $vis_a_vi) if $c->{debug}; $log->debug (sprintf ('[DEBUG] No text in message from %s', $vis_a_vi));
return; return;
} }
# we have text here! so potentially we can chit-chat # we have text here! so potentially we can chit-chat
my $text = $msg->text; my $text = $msg->text;
carp sprintf ('[DEBUG] In public chat %s (%s) %s say: %s', $chatname, $chatid, $vis_a_vi, $text) if $c->{debug}; $log->debug (sprintf ('[DEBUG] In public chat %s (%s) %s say: %s', $chatname, $chatid, $vis_a_vi, $text));
# are they quote something, maybe, us? # are they quote something, maybe, us?
if (defined ($msg->reply_to_message) && if (defined ($msg->reply_to_message) &&
defined ($msg->reply_to_message->from) && defined ($msg->reply_to_message->from) &&
defined ($msg->reply_to_message->from->username) && defined ($msg->reply_to_message->from->username) &&
($msg->reply_to_message->from->username eq $myusername)) { ($msg->reply_to_message->from->username eq $myusername)) {
carp sprintf ('[DEBUG] In public chat %s (%s) %s quote us!', $chatname, $chatid, $vis_a_vi) if $c->{debug}; $log->debug (sprintf ('[DEBUG] In public chat %s (%s) %s quote us!', $chatname, $chatid, $vis_a_vi));
# do not answer back if someone quote our new member greet # do not answer back if someone quote our new member greet
if ((substr ($msg->reply_to_message->text, 0, 9) eq 'Дратути, ') && if ((substr ($msg->reply_to_message->text, 0, 9) eq 'Дратути, ') &&
@@ -335,10 +336,10 @@ sub __on_msg {
$msg->typing (); $msg->typing ();
sleep (irand 2); sleep (irand 2);
carp sprintf ('[DEBUG] In public chat %s (%s) bot reply to %s: %s', $chatname, $chatid, $vis_a_vi, $reply) if $c->{debug}; $log->debug (sprintf ('[DEBUG] In public chat %s (%s) bot reply to %s: %s', $chatname, $chatid, $vis_a_vi, $reply));
$msg->reply ($reply); $msg->reply ($reply);
} else { } else {
carp sprintf ('[DEBUG] In public chat %s (%s) bot is not required to reply to %s', $chatname, $chatid, $vis_a_vi) if $c->{debug}; $log->debug (sprintf ('[DEBUG] In public chat %s (%s) bot is not required to reply to %s', $chatname, $chatid, $vis_a_vi));
} }
# should be channel, so we can't talk # should be channel, so we can't talk
@@ -356,7 +357,7 @@ sub init {
unless (-d $braindir) { unless (-d $braindir) {
make_path ($braindir, 0, 0755) or do { ## no critic (ValuesAndExpressions::ProhibitLeadingZeros) make_path ($braindir, 0, 0755) or do { ## no critic (ValuesAndExpressions::ProhibitLeadingZeros)
cluck "Unable to create $braindir: $OS_ERROR"; $log->fatal ("[FATAL] Unable to create $braindir: $OS_ERROR");
exit 1; exit 1;
}; };
} }