Logger: Better application log formatting

This commit is contained in:
mrkubax10 2023-09-17 20:21:14 +02:00
parent 73419f5a9c
commit bc37fcb2af
2 changed files with 18 additions and 10 deletions

View File

@ -11,3 +11,7 @@ our $botHostname = "hostname";
our $botName = "Full name of bot"; our $botName = "Full name of bot";
our $httpServerPort = 8080; our $httpServerPort = 8080;
our $verboseLogging = 0;
1;

View File

@ -164,12 +164,12 @@ sub prepareLogFile {
my $outputFilePath = $outputFileFolder."/".localtime->dmy("-").".txt"; my $outputFilePath = $outputFileFolder."/".localtime->dmy("-").".txt";
open(my $file, ">>", $outputFilePath); open(my $file, ">>", $outputFilePath);
if($file) { if($file) {
printf(":: Logger -> Outputting channel '%s' at '%s' to '%s'\n", $aChannelName, $aServerName, $outputFilePath); printf("[info] Outputting channel '%s' at '%s' to '%s'\n", $aChannelName, $aServerName, $outputFilePath);
$aLogFiles->{$aChannelName}{"file"} = $file; $aLogFiles->{$aChannelName}{"file"} = $file;
$aLogFiles->{$aChannelName}{"names"} = []; $aLogFiles->{$aChannelName}{"names"} = [];
} }
else { else {
print(":: Logger -> Failed to open '$outputFilePath' for writing\n"); print("[error] Failed to open '$outputFilePath' for writing\n");
return 0; return 0;
} }
} }
@ -182,10 +182,12 @@ sub handlePing {
my $aCommandLength = scalar(@$aCommand); my $aCommandLength = scalar(@$aCommand);
if($aCommandLength!=2) { if($aCommandLength!=2) {
printf("Encountered invalid PING command (2 arguments expected, %d provided)\n", $aCommandLength); printf("[error] Encountered invalid PING command (2 arguments expected, %d provided)\n", $aCommandLength);
return; return;
} }
printf(":: Response -> PONG :%s\n", $aCommand->[1]); if($configuration::verboseLogging) {
printf("[verbose] Response: PONG :%s\n", $aCommand->[1]);
}
$aStream->send(sprintf("PONG :%s\r\n", $aCommand->[1])); $aStream->send(sprintf("PONG :%s\r\n", $aCommand->[1]));
} }
@ -198,7 +200,7 @@ sub handlePrivMsg {
my $aCommandLength = scalar(@$aCommand); my $aCommandLength = scalar(@$aCommand);
if($aCommandLength!=4) { if($aCommandLength!=4) {
printf("Encountered invalid PRIVMSG command (4 arguments expected, %d provided)\n", $aCommandLength); printf("[error] Encountered invalid PRIVMSG command (4 arguments expected, %d provided)\n", $aCommandLength);
return; return;
} }
if(!prepareLogFile($aLogFiles, $aServerName, $aCommand->[1])) { if(!prepareLogFile($aLogFiles, $aServerName, $aCommand->[1])) {
@ -215,7 +217,7 @@ sub handleJoin {
my $aCommandLength = scalar(@$aCommand); my $aCommandLength = scalar(@$aCommand);
if($aCommandLength!=3) { if($aCommandLength!=3) {
printf("Encountered invalid JOIN command (3 arguments expected, %d provided)\n", $aCommandLength); printf("[error] Encountered invalid JOIN command (3 arguments expected, %d provided)\n", $aCommandLength);
return; return;
} }
if(!prepareLogFile($aLogFiles, $aServerName, $aCommand->[1])) { if(!prepareLogFile($aLogFiles, $aServerName, $aCommand->[1])) {
@ -232,7 +234,7 @@ sub handleQuit {
my $aCommandLength = scalar(@$aCommand); my $aCommandLength = scalar(@$aCommand);
if($aCommandLength!=3 && $aCommandLength!=2) { if($aCommandLength!=3 && $aCommandLength!=2) {
print("Encountered invalid QUIT command (3 or 2 arguments expected, $aCommandLength provided)\n"); print("[error] Encountered invalid QUIT command (3 or 2 arguments expected, $aCommandLength provided)\n");
return; return;
} }
my $reason = ""; my $reason = "";
@ -263,7 +265,7 @@ sub handlePart {
my $aCommandLength = scalar(@$aCommand); my $aCommandLength = scalar(@$aCommand);
if($aCommandLength!=3) { if($aCommandLength!=3) {
print("Encountered invalid PART command (3 arguments expected, $aCommandLength provided)\n"); print("[error] Encountered invalid PART command (3 arguments expected, $aCommandLength provided)\n");
return; return;
} }
if(!prepareLogFile($aLogFiles, $aServerName, $aCommand->[1])) { if(!prepareLogFile($aLogFiles, $aServerName, $aCommand->[1])) {
@ -296,7 +298,7 @@ sub handleNames {
my $aCommandLength = scalar(@$aCommand); my $aCommandLength = scalar(@$aCommand);
if($aCommandLength!=6) { if($aCommandLength!=6) {
print("Encountered invalid NAMES command (6 arguments expected, $aCommandLength provided)\n"); print("[error] Encountered invalid NAMES command (6 arguments expected, $aCommandLength provided)\n");
return; return;
} }
my @names = split(" ", $aCommand->[4]); my @names = split(" ", $aCommand->[4]);
@ -344,7 +346,9 @@ sub connectionWorker {
$buffer = $remaining; $buffer = $remaining;
while(length($line)>0) { while(length($line)>0) {
my @command = parseIRCCommand($line); my @command = parseIRCCommand($line);
printf(":: Server -> %s\n", $line); if($configuration::verboseLogging) {
printf("[verbose] Server: %s\n", $line);
}
given($command[0]) { given($command[0]) {
when("PING") { handlePing($stream, \@command); } when("PING") { handlePing($stream, \@command); }
when("PRIVMSG") { handlePrivMsg($stream, \@command, $aServerName, $aChannels, \%logFiles); } when("PRIVMSG") { handlePrivMsg($stream, \@command, $aServerName, $aChannels, \%logFiles); }