From 0858c10c8724c5e434fe949b2c9f96719a9de246 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaime=20Pe=CC=81rez?= <jaime.perez@uninett.no>
Date: Wed, 10 Aug 2016 13:52:11 +0200
Subject: [PATCH] Log backtraces with the same log level as the error messages,
 whatever that is.

It's not very useful to log backtraces always as debug, since that implies getting all the log messages, while backtraces would still help debug a particular error.
---
 lib/SimpleSAML/Error/Exception.php | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/lib/SimpleSAML/Error/Exception.php b/lib/SimpleSAML/Error/Exception.php
index bd54a9a81..75ee11f96 100644
--- a/lib/SimpleSAML/Error/Exception.php
+++ b/lib/SimpleSAML/Error/Exception.php
@@ -196,15 +196,25 @@ class SimpleSAML_Error_Exception extends Exception
     /**
      * Print the backtrace to the log if the 'debug' option is enabled in the configuration.
      */
-    protected function logBacktrace()
+    protected function logBacktrace($level = \SimpleSAML\Logger::DEBUG)
     {
         if (!SimpleSAML_Configuration::getInstance()->getBoolean('debug', false)) {
             return;
         }
 
         $backtrace = $this->formatBacktrace();
+
+        $callback = array('\SimpleSAML\Logger');
+        $functions = array(
+            \SimpleSAML\Logger::ERR     => 'error',
+            \SimpleSAML\Logger::WARNING => 'warning',
+            \SimpleSAML\Logger::INFO    => 'info',
+            \SimpleSAML\Logger::DEBUG   => 'debug',
+        );
+        $callback[] = $functions[$level];
+
         foreach ($backtrace as $line) {
-            SimpleSAML\Logger::debug($line);
+            call_user_func($callback, $line);
         }
     }
 
@@ -224,7 +234,7 @@ class SimpleSAML_Error_Exception extends Exception
             SimpleSAML\Logger::INFO    => 'logInfo',
             SimpleSAML\Logger::DEBUG   => 'logDebug',
         );
-        call_user_func(array($this, $fn[$default_level]));
+        call_user_func(array($this, $fn[$default_level]), $default_level);
     }
 
 
@@ -236,7 +246,7 @@ class SimpleSAML_Error_Exception extends Exception
     public function logError()
     {
         SimpleSAML\Logger::error($this->getClass().': '.$this->getMessage());
-        $this->logBacktrace();
+        $this->logBacktrace(\SimpleSAML\Logger::ERR);
     }
 
 
@@ -248,7 +258,7 @@ class SimpleSAML_Error_Exception extends Exception
     public function logWarning()
     {
         SimpleSAML\Logger::warning($this->getClass().': '.$this->getMessage());
-        $this->logBacktrace();
+        $this->logBacktrace(\SimpleSAML\Logger::WARNING);
     }
 
 
@@ -260,7 +270,7 @@ class SimpleSAML_Error_Exception extends Exception
     public function logInfo()
     {
         SimpleSAML\Logger::info($this->getClass().': '.$this->getMessage());
-        $this->logBacktrace();
+        $this->logBacktrace(\SimpleSAML\Logger::INFO);
     }
 
 
@@ -272,7 +282,7 @@ class SimpleSAML_Error_Exception extends Exception
     public function logDebug()
     {
         SimpleSAML\Logger::debug($this->getClass().': '.$this->getMessage());
-        $this->logBacktrace();
+        $this->logBacktrace(\SimpleSAML\Logger::DEBUG);
     }
 
 
-- 
GitLab