Logging consolidation and syslog support
authorBill Erickson <berickxx@gmail.com>
Tue, 12 Apr 2016 14:43:17 +0000 (10:43 -0400)
committerBill Erickson <berickxx@gmail.com>
Tue, 12 Apr 2016 14:43:21 +0000 (10:43 -0400)
Log all messages to syslog.  See $syslog_* variables to modify behavior.
Add support for --log-stdout option, which causes all log messages to be
cloned to STDOUT.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
pingest.pl

index 47f630a..ce07e47 100755 (executable)
@@ -19,6 +19,8 @@ use strict;
 use warnings;
 use DBI;
 use Getopt::Long;
+use DateTime;
+use Sys::Syslog qw(syslog openlog);
 
 # Globals for the command line options: --
 
@@ -38,8 +40,13 @@ my $max_id;       # stop processing when this bib ID is reached.
 my $max_duration; # max processing duration in seconds
 my $newest_first; # Process records in descending order of edit_date
 my $sort_id_desc; # Sort by record ID descending
+my $log_stdout;   # Clone log messages to stdout
 my $help;         # show help text
 
+my $syslog_facility = 'LOCAL6'; # matches Evergreen gateway
+my $syslog_ops      = 'pid';
+my $syslog_ident    = 'pingest';
+
 GetOptions(
     'batch-size=i'   => \$batch_size,
     'max-child=i'    => \$max_child,
@@ -52,6 +59,7 @@ GetOptions(
     'newest-first'   => \$newest_first,
     'sort-id-desc'   => \$sort_id_desc,
     'max-duration=i' => \$max_duration,
+    'log-stdout'     => \$log_stdout,
     'help'           => \$help
 );
 
@@ -100,6 +108,13 @@ sub help {
     --max-id
         Only process records whose ID is equal to or less than max-id.
 
+    --log-stdout
+        Clone debug and error messages to STDOUT.  Beware that the 
+        contents of 'print' calls from daemonized child process may not 
+        reach STDOUT of the controlling terminal.
+
+        All messages are logged to syslog.
+
     --help
         Show this help text.
 
@@ -109,6 +124,32 @@ HELP
 
 help() if $help;
 
+openlog($syslog_ident, $syslog_ops, $syslog_facility);
+
+# options for level match syslog options: DEBUG,INFO,WARNING,ERR
+sub announce {
+    my ($level, $msg, $die) = @_;
+    syslog("LOG_$level", "$level $msg");
+
+    my $date_str = DateTime->now(time_zone => 'local')->strftime('%F %T');
+    my $msg_str = "$date_str [$$] $level $msg\n";
+
+    if ($die) {
+        die $msg_str;
+
+    } else {
+        if ($level eq 'ERR' or $level eq 'WARNING') {
+            # always clone error messages to stdout
+            warn $msg_str; # avoid dupe messages
+        } elsif ($log_stdout) {
+            print $msg_str;
+        }
+    }
+}
+
+announce('DEBUG', 
+    "pingest starting with batch-size $batch_size and max_child $max_child");
+
 my $where = "WHERE deleted = 'f'";
 if ($min_id && $max_id) {
     $where .= " AND id BETWEEN $min_id AND $max_id";
@@ -204,12 +245,12 @@ while ($count < $lists) {
         if (grep {$_ == $pid} @running) {
             @running = grep {$_ != $pid} @running;
             $count++;
-            print "$count of $lists processed\n";
+            announce('INFO', "$count of $lists processed");
         }
     }
 
     if ($duration_expired && scalar(@running) == 0) {
-        warn "Exiting on max_duration ($max_duration)\n";
+        announce('INFO', "Exiting on max_duration ($max_duration)");
         exit(0);
     }
 }
@@ -235,11 +276,11 @@ sub browse_ingest {
             if ($sth->execute($_)) {
                 my $crap = $sth->fetchall_arrayref();
             } else {
-                warn ("Browse ingest failed for record $_");
+                announce('WARNING', "Browse ingest failed for record $_");
             }
             if (duration_expired()) {
-                warn "browse_ingest() stopping on record $_ ".
-                    "after max duration reached\n";
+                announce('INFO', 
+                    "browse_ingest() stopping on record $_ on max duration");
                 last;
             }
         }
@@ -280,7 +321,8 @@ sub reingest_field_entries {
         if ($sth->execute()) {
             my $crap = $sth->fetchall_arrayref();
         } else {
-            warn ("metabib.reingest_metabib_field_entries failed for record $_");
+            announce('WARNING', 
+                "metabib.reingest_metabib_field_entries failed for record $_");
         }
     }
 }
@@ -300,7 +342,8 @@ END_OF_INGEST
         if ($sth->execute()) {
             my $crap = $sth->fetchall_arrayref();
         } else {
-            warn ("metabib.reingest_record_attributes failed for record $_");
+            announce('WARNING', 
+                "metabib.reingest_record_attributes failed for record $_");
         }
     }
 }