From facd913e5418fde9823ea24e476a0b0cd5ef154c Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Sat, 7 Mar 2020 13:31:33 +0100 Subject: Better logging --- lib/TgLib/Api.pm | 5 +---- tgserver | 7 ++++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/TgLib/Api.pm b/lib/TgLib/Api.pm index 7a14ef3..51d4c31 100644 --- a/lib/TgLib/Api.pm +++ b/lib/TgLib/Api.pm @@ -38,7 +38,7 @@ sub get_updates { my $updates = decode_json($resp->content)->{'result'}; # TODO why does `decode_json` not do this work? map { utf8::encode($_->{'message'}{'text'}) if exists $_->{'message'}{'text'} } @$updates; - $logger->info(sprintf "Received %d updates from chats %s\n", + $logger->debug(sprintf "Received %d updates from chats %s\n", scalar(@$updates), join(", ", map { $_->{'message'}{'chat'}{'id'} } @$updates)) if @$updates; @@ -51,7 +51,6 @@ sub send_message { my $logger = $self->{'logger'}; my $uri = "$self->{'uri'}/sendMessage"; - $logger->info("Sending to $chat_id: '$text'\n"); utf8::decode($text) unless utf8::is_utf8($text); my $content = encode_json {'chat_id' => $chat_id, 'text' => $text}; my $req = HTTP::Request->new("POST", $uri, @@ -74,7 +73,6 @@ sub send_document { 'document' => [undef, 'cosa.png', Content => $photo]}; my $req = POST $uri, 'Content-Type' => "multipart/form-data", 'Content' => $content; - $logger->info("Sending photo to $chat_id\n"); $logger->debug(sprintf "Request:\n%s\n", Dumper($req)); # DEBUG my $resp = $self->{'ua'}->request($req); @@ -101,7 +99,6 @@ sub get_file { die $resp->message; } else { my $file_path = decode_json($resp->content)->{'result'}{'file_path'}; - $logger->info("Getting file $file_id (file_path: $file_path)...\n"); my $uri = "$self->{'file_uri'}/$file_path"; my $req = HTTP::Request->new("GET", $uri); diff --git a/tgserver b/tgserver index 8dbb2ce..55855ce 100755 --- a/tgserver +++ b/tgserver @@ -84,7 +84,7 @@ sub handle_photo { my $photos = $msg->{'photo'}; my $chat_id = $msg->{'chat'}{'id'}; my $photo = (sort { $b->{'width'} <=> $a->{'width'} } @$photos)[0]; - $logger->info(sprintf "%s: [Received photo %s (size=%d)]\n", + $logger->info(sprintf "%s: [Photo %s (size=%d)] -> 🤖\n", $chat_id, $photo->{'file_id'}, $photo->{'file_size'}); my $file = $api->get_file($photo->{'file_id'}); @@ -98,7 +98,7 @@ sub handle_text { my $msg = shift; my $text = $msg->{'text'}; my $chat_id = $msg->{'chat'}{'id'}; - $logger->info("$chat_id: '$text'\n"); + $logger->info("$chat_id: '$text' -> 🤖\n"); $ENV{'TGUTILS_TYPE'} = 'TEXT'; my $response = pipe_send($text, @ARGV); @@ -111,8 +111,10 @@ sub reply { my $type = ref $response eq 'HASH' ? $response->{'type'} : 'TEXT'; if ($type eq 'DOCUMENT') { my $caption = $response->{'caption'}; + $logger->info("🤖: [Document '$caption'] -> $chat_id\n"); $api->send_document($chat_id, decode_base64 $response->{'content'}, $caption); } elsif ($type eq 'TEXT' and $response) { + $logger->info("🤖: '$response' -> $chat_id\n"); $api->send_message($chat_id, $response); } else { $logger->debug("Empty response, skipping\n"); @@ -144,7 +146,6 @@ sub pipe_send { waitpid $pid, 0; # collect the child process chomp $response; - # $logger->debug("'$content' -> @ARGV -> '$response'\n"); return try { decode_json $response } catch { $response }; } -- cgit v1.2.3