Commit Diff


commit - c758c43671f604a7bc694b5f832fbdf544b6e276
commit + f976eb7d37ee1bfb2d1ad04ea7d6aad88882db11
blob - 6574369a2b1a80d1e05792537ffd8483e2ed934f
blob + 727c6b5dc6737c702841433ec7376f6591cd308e
--- lib/IRCNOW/Logger.pm
+++ lib/IRCNOW/Logger.pm
@@ -2,6 +2,7 @@ package IRCNOW::Logger;
 use strict;
 use warnings;
 use POSIX qw(strftime);
+use Exporter 'import';
 
 my $black   = "\033[0;30m";
 my $red     = "\033[0;31m";
@@ -13,15 +14,7 @@ my $blue    = "\033[94m";
 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,
@@ -37,10 +30,9 @@ use constant {
   JSON => 1,
 };
 
+
 my $instance = undef;
 
-my $verbose = 5;
-
 sub logMessage {
   my ($instance, $level, $package, $filename, $line, $msg) = @_;
 
@@ -60,38 +52,38 @@ sub logMessage {
     $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);
@@ -99,28 +91,30 @@ sub debug {
 
 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;
@@ -131,12 +125,10 @@ sub format {
   $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 {
@@ -157,6 +149,7 @@ sub applyColor {
       return "$blue$val$nocolor";
     }
   }
+
   return $val;
 }
 
@@ -167,11 +160,12 @@ sub applyLength {
     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)) {
@@ -184,15 +178,15 @@ sub applyLength {
 }
 
 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};
   }
@@ -205,14 +199,20 @@ sub new {
   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;
   }