Commits

Edward Kirton committed 514eb4d

Logger class no longer opens db connection for msgs below threshold

Comments (0)

Files changed (1)

lib/iTagger/Logger.pm

 {
     my ($class, $loggerName, $loggerConfigFile) = @_;
     die("Logger: logger name required\n") unless $loggerName;
-    my $this = \$loggerName;
-    bless $this, $class;
     if ( $loggerConfigFile )
     {
         Log::Log4perl->init($loggerConfigFile);
     {
         die("Logger: either log config file parameter or environment variable ITAGGER_LOG_CONFIG is required\n");
     }
+    my $logger = Log::Log4perl->get_logger($loggerName);
+    my $this =
+    {
+        loggerName => $loggerName,
+        is => 
+        {
+            trace => $logger->is_trace(),
+            debug => $logger->is_debug(),
+            info => $logger->is_info(),
+            warn => $logger->is_warn(),
+            error => $logger->is_error(),
+            fatal => $logger->is_fatal()
+        }
+    };
+    bless $this, $class;
+    Log::Log4perl->remove_logger($logger);
     return $this;
 }
 
 sub _log
 {
     my ($this, $type, $msg) = @_;
+    return unless $this->{is}->{$type}; # avoid connecting to db if not going to insert entry
     if ( !defined($msg) ) { $msg = '' }
     elsif ( $msg =~ /^(.*)\n+$/ ) { $msg = $1 }
-    my $logger = Log::Log4perl->get_logger($$this);
+    my $logger = Log::Log4perl->get_logger($this->{loggerName});
     if ( $type eq 'die' ) { $logger->logdie($msg) }
     elsif ( $type eq 'warn' ) { $logger->logwarn($msg) }
     elsif ( $type eq 'error' ) { $logger->error($msg) }
 
 sub debug { shift->_log('debug', @_) }
 
+=item trace
+
+Log trace message.
+
+=cut
+
+sub trace { shift->_log('trace', @_) }
+
 1;
 
 =back