LP1904233 Improve logger controls sans configs user/berick/lp1904233-log-standalone
authorBill Erickson <berickxx@gmail.com>
Fri, 13 Nov 2020 18:31:32 +0000 (13:31 -0500)
committerBill Erickson <berickxx@gmail.com>
Fri, 13 Nov 2020 18:39:07 +0000 (13:39 -0500)
Allow Perl client to specify log level and STDOUT logging when the
$logger is instantiated without any configuration information, i.e. the
client is not connecting to the OpenSRF network.

$logger->set_log_stdout(1);
$logger->set_log_level($logger->INFO);

Signed-off-by: Bill Erickson <berickxx@gmail.com>
src/perl/lib/OpenSRF/Utils/Logger.pm

index 6a662ac..36e0e91 100644 (file)
@@ -42,6 +42,9 @@ my $logfile_enabled = 1;    # are we logging to a file?
 my $act_logfile_enabled = 1;# are we logging to a file?
 my $max_log_msg_len = 1536; # SYSLOG default maximum is 2048
 
+# force stdout logging; useful when not connecting to opensrf
+my $log_stdout = 0;
+
 our $logger = "OpenSRF::Utils::Logger";
 
 # log levels
@@ -56,16 +59,25 @@ sub ALL      { return 100; }
 
 my $isclient;  # true if we control the osrf_xid
 
+sub set_log_level {
+    my ($self, $level) = @_;
+    $loglevel = $level;
+}
+
+sub set_log_stdout {
+    my ($eslf, $value) = @_;
+    $log_stdout = $value;
+}
+
 # load up our config options
 sub set_config {
     my $force = shift;
 
-    return if defined $config and !$force;
+    return if (defined $config || $log_stdout) and !$force;
 
     $config = OpenSRF::Utils::Config->current;
     if( !defined($config) ) {
         $loglevel = INFO();
-        warn "*** Logger found no config.  Using STDERR ***\n";
         return;
     }
 
@@ -290,6 +302,8 @@ sub _write_file {
     if ($file) {
         sysopen( SINK, $file, O_NONBLOCK|O_WRONLY|O_APPEND|O_CREAT ) 
             or die "Cannot sysopen $file: $!";
+    } elsif ($log_stdout) {
+        open (SINK, ">&1");  # print to STDOUT
     } else {
         open (SINK, ">&2");  # print to STDERR as warned
     }