1

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

@@ -8,6 +8,7 @@ use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (cluck);
use HTML::TokeParser;
use Log::Any qw ($log);
use Mojo::UserAgent;
use version; our $VERSION = qw (1.0);
@@ -48,7 +49,7 @@ sub Buni {
} while ($#{$a[0]} > 1);
}
} 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;

View File

@@ -12,6 +12,7 @@ use English qw ( -no_match_vars );
use Encode;
use Carp qw (cluck);
use HTML::TokeParser;
use Log::Any qw ($log);
use Mojo::Log;
use Mojo::UserAgent::Cached;
use POSIX qw (strftime);
@@ -103,7 +104,7 @@ sub Drink {
$ret = join "\n", @holyday;
}
} 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;

View File

@@ -9,6 +9,7 @@ use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (cluck 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);
@@ -69,7 +70,7 @@ sub Fortune () {
my $backingfile = sprintf '%s/fortune.sqlite', $dir;
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 '';
};

View File

@@ -9,6 +9,7 @@ use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (cluck 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 {
cluck "[ERROR] Unable to tie to $backingfile: $OS_ERROR\n";
$log->error ("[ERROR] Unable to tie to $backingfile: $OS_ERROR\n");
return '';
};

View File

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

View File

@@ -10,6 +10,7 @@ use Carp qw (cluck);
use CHI;
use CHI::Driver::BerkeleyDB;
use Digest::HMAC_SHA1 qw (hmac_sha1);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
use MIME::Base64;
use Mojo::UserAgent;
@@ -188,7 +189,7 @@ sub flickrAuthorization {
if ($r->code == 302) {
return $r->content->headers->location;
} else {
printf 'Something went wrong: %s %s', $r->code, $r->message;
printf "Something went wrong: %s %s\n", $r->code, $r->message;
return;
}
}
@@ -335,19 +336,19 @@ sub flickrSearchByText {
if (defined $response) {
return $response;
} else {
cluck "Flickr api returns incorrect json: $r->message";
$log->warn ("[WARN] Flickr api returns incorrect json: $r->message");
return undef;
}
} else {
cluck "Flickr api returns incorrect json: $r->message";
$log->warn ("[WARN] Flickr api returns incorrect json: $r->message");
return undef;
}
} else {
cluck "Flickr api returns incorrect json: $r->message";
$log->warn ("[WARN] Flickr api returns incorrect json: $r->message");
return undef;
}
} 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;
}
}
@@ -442,19 +443,19 @@ sub flickrSearchByTags {
if (defined $response) {
return $response;
} else {
cluck "Flickr api returns incorrect json: $r->message";
$log->warn ("[WARN] Flickr api returns incorrect json: $r->message");
return undef;
}
} else {
cluck "Flickr api returns incorrect json: $r->message";
$log->warn ("[WARN] Flickr api returns incorrect json: $r->message");
return undef;
}
} else {
cluck "Flickr api returns incorrect json: $r->message";
$log->warn ("[WARN] Flickr api returns incorrect json: $r->message");
return undef;
}
} 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;
}
}
@@ -554,7 +555,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 {
@@ -587,7 +588,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 {

View File

@@ -9,6 +9,7 @@ use English qw ( -no_match_vars );
use Carp qw (cluck);
use CHI;
use CHI::Driver::BerkeleyDB;
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
use Mojo::UserAgent;
@@ -28,19 +29,19 @@ sub Imgur {
my $search_url = 'https://api.imgur.com/3/gallery/search/';
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;
};
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;
};
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;
};
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
@@ -81,7 +82,7 @@ sub Imgur {
);
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;
}
@@ -93,7 +94,7 @@ sub Imgur {
$imgur_access_token = $refresh->{access_token};
$cache->set ('imgur_access_token', $refresh->{access_token}, 'never');
} 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;
}
@@ -130,19 +131,19 @@ sub Imgur {
if ($#urls > 0) {
return $urls[irand ($#urls + 1)];
} else {
cluck 'Imgur api returned empty result.';
$log->notice ('[NOTICE] Imgur api returned empty result.');
return undef;
}
} 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;
}
} else {
cluck "Unable to decode imgur api json: $EVAL_ERROR";
$log->warn ("[WARN] Unable to decode imgur api json: $EVAL_ERROR");
return undef;
}
} 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;
}
}

View File

@@ -9,6 +9,7 @@ use English qw ( -no_match_vars );
use Carp qw (cluck);
use CHI;
use CHI::Driver::BerkeleyDB;
use Log::Any qw ($log);
use BotLib::Conf qw (LoadConf);
use version; our $VERSION = qw (1.0);
@@ -52,7 +53,7 @@ sub KarmaSet (@) {
}
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) {

View File

@@ -11,6 +11,7 @@ use Carp qw (cluck);
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) {
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 '';
}
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 '';
}

View File

@@ -8,6 +8,7 @@ use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Carp qw (cluck);
use HTML::TokeParser;
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
use Mojo::UserAgent;
@@ -51,7 +52,7 @@ sub Monkeyuser {
$ret = sprintf '[MonkeyUser](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;

View File

@@ -9,6 +9,7 @@ use English qw ( -no_match_vars );
use Carp qw (carp cluck);
use CHI;
use CHI::Driver::BerkeleyDB;
use Log::Any qw ($log);
use Mojo::Log;
use Mojo::UserAgent::Cached;
use BotLib::Conf qw (LoadConf);
@@ -109,11 +110,11 @@ sub __weather {
};
unless ($fc) {
cluck "[WARN] openweathermap returns corrupted json: $EVAL_ERROR";
$log->warn ("[WARN] openweathermap returns corrupted json: $EVAL_ERROR");
return undef;
};
} 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;
}