commit - c758c43671f604a7bc694b5f832fbdf544b6e276
commit + f976eb7d37ee1bfb2d1ad04ea7d6aad88882db11
blob - 6574369a2b1a80d1e05792537ffd8483e2ed934f
blob + 727c6b5dc6737c702841433ec7376f6591cd308e
--- lib/IRCNOW/Logger.pm
+++ lib/IRCNOW/Logger.pm
use strict;
use warnings;
use POSIX qw(strftime);
+use Exporter 'import';
my $black = "\033[0;30m";
my $red = "\033[0;31m";
my $magenta = "\033[35m";
my $cyan = "\033[36m";
-our @EXPORT_OK = qw(
- debug info warn error
-);
-# create debug tag so you can import the debug sub and messages
-# this enables loading lists of exports by tag like so:
-# use IRCNOW::IO qw(:DEBUG :FILEIO);
-our %EXPORT_TAGS = (
- FILEIO=>[qw(readarray readstr writefile appendfile)],
-);
+our @EXPORT_OK = qw(debug info warn error NONE ERRORS WARNINGS INFO DEBUG ALL JSON SUGAR);
use constant {
NONE => 0,
JSON => 1,
};
+
my $instance = undef;
-my $verbose = 5;
-
sub logMessage {
my ($instance, $level, $package, $filename, $line, $msg) = @_;
$levelString = "ALL";
}
- if ($verbose >= $level) { $instance->printLog($level, $levelString, $package, $filename, $line, $msg); }
+ if (int($instance->{'verbosity'}) >= $level) { $instance->printLog($level, $levelString, $package, $filename, $line, $msg); }
}
sub printLog {
- my ($instance, $level, $levelString, $package, $filename, $line, $msg) = @_;
-
+ my ($instance, $level, $levelString, $package, $filename, $line, $msg) = @_;
+
if ($instance->{'printType'} == JSON) {
- $instance->printJSON($level, $levelString, $package, $filename, $line, $msg);
+ printJSON($level, $levelString, $package, $filename, $line, $msg);
} elsif ($instance->{'printType'} == SUGAR) {
- $instance->printSugar($level, $levelString, $package, $filename, $line, $msg);
+ printSugar($instance->{'format'}, $level, $levelString, $package, $filename, $line, $msg);
}
}
sub printJSON {
- my ($instance, $level, $levelString, $package, $filename, $line, $msg) = @_;
-
- my $content = sprintf "{ \"level\": \"%s\", \"msg\": \"%s\", \"package\": \"%s\", \"file\": \"%s:%s\" }\n", $levelString, $msg, $package, $filename, $line;
+ my ($level, $levelString, $package, $filename, $line, $msg) = @_;
+ my $content = sprintf "{ \"level\": \"%s\", \"msg\": \"%s\", \"package\": \"%s\", \"file\": \"%s:%s\" }\n", $levelString, $msg, $package, $filename, $line;
+
print $content;
}
sub printSugar {
- my ($instance, $level, $levelString, $package, $filename, $line, $msg) = @_;
-
- my $formattedStr = $instance->format($level, $levelString, $package, $filename, $line, $msg);
+ my ($format, $level, $levelString, $package, $filename, $line, $msg) = @_;
+ my $formattedStr = formatLog($format, $level, $levelString, $package, $filename, $line, $msg);
+
print $formattedStr;
-
}
sub debug {
my ($instance, $msg) = @_;
+
my ($package, $filename, $line) = caller;
$instance->logMessage(DEBUG, $package, $filename, $line, $msg);
sub warn {
my ($instance, $msg) = @_;
+
my ($package, $filename, $line) = caller;
-
+
$instance->logMessage(WARNINGS, $package, $filename, $line, $msg);
}
sub info {
my ($instance, $msg) = @_;
+
my ($package, $filename, $line) = caller;
-
+
$instance->logMessage(INFO, $package, $filename, $line, $msg);
}
sub error {
my ($instance, $msg) = @_;
+
my ($package, $filename, $line) = caller;
-
$instance->logMessage(ERRORS, $package, $filename, $line, $msg);
}
-sub format {
- my ($instance, $level, $levelString, $package, $filename, $line, $msg) = @_;
+sub formatLog {
+ my ($format, $level, $levelString, $package, $filename, $line, $msg) = @_;
my $data = {};
$data->{'package'} = $package;
$data->{'loglevel'} = $levelString;
$data->{"timestamp"} = strftime("%Y-%m-%d %H:%M:%S", localtime);
- my $formattedStr = $instance->{'format'};
- $formattedStr =~ s{\:(.+?(%\d+|)(?=([^a-zA-Z]|$)))}{ $instance->getDataValue($level, $data, $1) }ge;
-
- return "$formattedStr\n";
+ my $formattedStr = $format;
+ $formattedStr =~ s{\:(.+?(%\d+|)(?=([^a-zA-Z]|$)))}{ getDataValue($level, $data, $1) }ge;
-
+ return "$formattedStr\n";
}
sub applyColor {
return "$blue$val$nocolor";
}
}
+
return $val;
}
return $value;
}
- my $str = "";
my $len = int($length);
if ($len < length($value)) {
$len = length($value);
}
+
+ my $str = "";
for my $i (0..$len-1) {
my $val = " ";
if ($i < length($value)) {
}
sub getDataValue {
- my ($instance, $level, $data, $fieldStr) = @_;
+ my ($level, $data, $fieldStr) = @_;
my @fieldMatch = $fieldStr =~ /(.+?)(%|$)/g;
- #print "\n$fieldMatch[2]\n";
my @fieldLength = $fieldStr =~ /.+?(%|$)(.+|$)/g;
+
my $length = $fieldLength[1];
my $field = $fieldMatch[0];
- my $value = "";
+ my $value = "";
if (defined $data->{$field}) {
$value = $data->{$field};
}
my $class = shift;
$instance ||= bless {
+ 'verbosity' => DEBUG,
'printType' => JSON,
- # timestamp [loglevel]
'format' => ":timestamp [:loglevel%5] :package%8 :fileline%10 :msg",
}, $class;
+
+
+ my $passedVerbosity = shift;
my $passedPrintType = shift;
my $passedFormat = shift;
+ if (defined $passedVerbosity) {
+ $instance->{'verbosity'} = $passedVerbosity;
+ }
if (defined $passedPrintType) {
$instance->{'printType'} = $passedPrintType;
}