diff --git a/lib/SimpleSAML/Error/Error.php b/lib/SimpleSAML/Error/Error.php index 69662ab94c..6597d67a93 100644 --- a/lib/SimpleSAML/Error/Error.php +++ b/lib/SimpleSAML/Error/Error.php @@ -211,7 +211,7 @@ protected function saveError() $etrace = implode("\n", $data); $reportId = bin2hex(openssl_random_pseudo_bytes(4)); - SimpleSAML_Logger::error('Error report with id '.$reportId.' generated.'); + SimpleSAML_Logger::notice('Error report with id '.$reportId.' generated.'); $config = SimpleSAML_Configuration::getInstance(); $session = SimpleSAML_Session::getSessionFromRequest(); diff --git a/lib/SimpleSAML/Error/Exception.php b/lib/SimpleSAML/Error/Exception.php index 2872b335a9..07ca22508c 100644 --- a/lib/SimpleSAML/Error/Exception.php +++ b/lib/SimpleSAML/Error/Exception.php @@ -182,10 +182,7 @@ public function format() public function logError() { - $lines = $this->format(); - foreach ($lines as $line) { - SimpleSAML_Logger::error($line); - } + $this->log('error'); } @@ -197,10 +194,7 @@ public function logError() public function logWarning() { - $lines = $this->format(); - foreach ($lines as $line) { - SimpleSAML_Logger::warning($line); - } + $this->log('warning'); } @@ -212,10 +206,18 @@ public function logWarning() public function logInfo() { - $lines = $this->format(); - foreach ($lines as $line) { - SimpleSAML_Logger::debug($line); - } + $this->log('debug'); + } + + + /** + * Abstracted logging method. + * + * @param string $level The log level (method to be called.) + */ + private function log($level) { + + SimpleSAML_Logger::$level($this->format()); } @@ -227,10 +229,7 @@ public function logInfo() public function logDebug() { - $lines = $this->format(); - foreach ($lines as $line) { - SimpleSAML_Logger::debug($line); - } + $this->log('debug'); } diff --git a/lib/SimpleSAML/Logger.php b/lib/SimpleSAML/Logger.php index 11d275dd69..47cb4ee5a6 100644 --- a/lib/SimpleSAML/Logger.php +++ b/lib/SimpleSAML/Logger.php @@ -350,9 +350,14 @@ private static function log($level, $string, $statsLog = false) } if (self::$logLevel >= $level || $statsLog) { + $raw_input = array($string); if (is_array($string)) { + $raw_input = $string; $string = implode(",", $string); } + if ((self::$format == 'json') && method_exists(self::$loggingHandler, 'setArray')) { + self::$loggingHandler->setArray($raw_input); + } $formats = array('%trackid', '%msg', '%srcip', '%stat'); $replacements = array(self::$trackid, $string, $_SERVER['REMOTE_ADDR']); diff --git a/lib/SimpleSAML/Logger/LoggingHandlerSyslog.php b/lib/SimpleSAML/Logger/LoggingHandlerSyslog.php index c0a94ea22c..ca576ecd3a 100644 --- a/lib/SimpleSAML/Logger/LoggingHandlerSyslog.php +++ b/lib/SimpleSAML/Logger/LoggingHandlerSyslog.php @@ -13,6 +13,7 @@ class SimpleSAML_Logger_LoggingHandlerSyslog implements SimpleSAML_Logger_Loggin { private $isWindows = FALSE; private $format; + private $arrayData; /** @@ -36,6 +37,16 @@ public function __construct() } + /** + * Set the Array data for use when logging JSON. + * + * @param array $array Array of data to log with JSON. + */ + public function setArray($array) { + $this->arrayData = $array; + } + + /** * Set the format desired for the logs. * @@ -64,12 +75,21 @@ public function log($level, $string) } } - $formats = array('%process', '%level'); - $replacements = array('', $level); - $string = str_replace($formats, $replacements, $string); - $string = preg_replace('/%\w+(\{[^\}]+\})?/', '', $string); - $string = trim($string); + if ($this->format == 'json') { + $data = $this->arrayData; + // Send a single line as text, not an array. + if (count($this->arrayData) == 1) { + $data = reset($this->arrayData); + } + $message = json_encode(array('message' => $data)); + } else { + $formats = array('%process', '%level'); + $replacements = array('', $level); + $string = str_replace($formats, $replacements, $string); + $string = preg_replace('/%\w+(\{[^\}]+\})?/', '', $string); + $message = trim($string); + } - syslog($level, $string); + syslog($level, $message); } }