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

@@ -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;
}
}