From 38362eef57500385e8817c122532b9a593e87dd5 Mon Sep 17 00:00:00 2001
From: Jaime Perez Crespo <jaime.perez@uninett.no>
Date: Thu, 31 Mar 2016 15:25:57 +0200
Subject: [PATCH] If we are invoked from the command line, log the messages to
 standard error, instead of ignoring them.

---
 lib/SimpleSAML/Logger.php | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/lib/SimpleSAML/Logger.php b/lib/SimpleSAML/Logger.php
index 8801a4af4..ef3127afc 100644
--- a/lib/SimpleSAML/Logger.php
+++ b/lib/SimpleSAML/Logger.php
@@ -365,6 +365,8 @@ class Logger
             $sh = new \SimpleSAML_Logger_LoggingHandlerFile();
         } elseif ($handler === 'errorlog') {
             $sh = new \SimpleSAML_Logger_LoggingHandlerErrorLog();
+        } elseif ($handler === 'stderr') {
+            $sh = new \SimpleSAML\Logger\StandardError();
         } else {
             throw new \Exception(
                 'Invalid value for the [logging.handler] configuration option. Unknown handler: '.$handler
@@ -381,12 +383,17 @@ class Logger
 
     private static function log($level, $string, $statsLog = false)
     {
-        if (php_sapi_name() === 'cli' || defined('STDIN')) {
-            // we are being executed from the CLI, nowhere to log
+        if (self::$loggingHandler === false) {
+            // some error occurred while initializing logging
+            self::defer($level, $string, $statsLog);
             return;
-        }
-
-        if (self::$loggingHandler === null) {
+        } elseif (php_sapi_name() === 'cli' || defined('STDIN')) {
+            // we are being executed from the CLI, nowhere to log
+            if (is_null(self::$loggingHandler)) {
+                self::createLoggingHandler('stderr');
+            }
+            $_SERVER['REMOTE_ADDR'] = "CLI";
+        } elseif (self::$loggingHandler === null) {
             // Initialize logging
             self::createLoggingHandler();
 
@@ -396,9 +403,6 @@ class Logger
                     self::log($msg['level'], $msg['string'], $msg['statsLog']);
                 }
             }
-        } elseif (self::$loggingHandler === false) {
-            self::defer($level, $string, $statsLog);
-            return;
         }
 
         if (self::$captureLog) {
-- 
GitLab