Mantis #0009248 Expresso #8349
authorFlávio Gomes da Silva Lisboa <flavio.lisboa@serpro.gov.br>
Wed, 13 Nov 2013 11:52:12 +0000 (09:52 -0200)
committerLars Kneschke <l.kneschke@metaways.de>
Wed, 13 Nov 2013 21:30:12 +0000 (22:30 +0100)
 - Configurable backtracing for discovering origin of queries

Change-Id: Ic2a99b5b8b07a0cd03b732555a9840322dfb89ec
Reviewed-on: https://gerrit.tine20.org/tine20/2532
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: jenkins user
tine20/Tinebase/Backend/Sql/Abstract.php

index 64d20a2..86f004c 100644 (file)
@@ -288,6 +288,8 @@ abstract class Tinebase_Backend_Sql_Abstract extends Tinebase_Backend_Abstract i
         $select->where($this->_db->quoteIdentifier($this->_tableName . '.' . $this->_identifier) . ' IN (?)', (array) $ids);
         Tinebase_Backend_Sql_Abstract::traitGroup($select);
         
+        $this->_checkTracing($select);
+        
         $stmt = $this->_db->query($select);
         $queryResult = $stmt->fetchAll();
         $stmt->closeCursor();
@@ -354,6 +356,9 @@ abstract class Tinebase_Backend_Sql_Abstract extends Tinebase_Backend_Abstract i
         } else {
                 $select = $this->_getSelect('*', $_getDeleted)->where('1=0');
         }
+        
+        $this->_checkTracing($select);
+        
         $stmt = $this->_db->query($select);
         
         $resultSet = $this->_rawDataToRecordSet($stmt->fetchAll());
@@ -421,7 +426,7 @@ abstract class Tinebase_Backend_Sql_Abstract extends Tinebase_Backend_Abstract i
         
         Tinebase_Backend_Sql_Abstract::traitGroup($select);
         
-        if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . $select->__toString());
+        $this->_checkTracing($select);
         
         $stmt = $this->_db->query($select);
         $queryResult = $stmt->fetchAll();
@@ -665,6 +670,32 @@ abstract class Tinebase_Backend_Sql_Abstract extends Tinebase_Backend_Abstract i
     }
     
     /**
+     * Checks if backtrace and query should be logged
+     * 
+     * For enabling this feature, you must add a key in config.inc.php:
+     * 
+     *     'logger' => 
+     *         array(
+     *             // logger stuff
+     *             'traceQueryOrigins' => true,
+     *             'priority' => 8
+     *         ),
+     *
+     * @param Zend_Db_Select $select
+     */
+    protected function _checkTracing(Zend_Db_Select $select)
+    {
+        if (Tinebase_Core::isLogLevel(Zend_Log::TRACE) && $config = Tinebase_Core::getConfig()->logger) {
+            if ($config->traceQueryOrigins) {
+                $e = new Exception();
+                Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . "\n" . 
+                    "BACKTRACE: \n" . $e->getTraceAsString() . "\n" . 
+                    "SQL QUERY: \n" . $select);
+            }
+        }
+    }
+    
+    /**
      * fetch rows from db
      * 
      * @param Zend_Db_Select $_select
@@ -677,6 +708,8 @@ abstract class Tinebase_Backend_Sql_Abstract extends Tinebase_Backend_Abstract i
         
         Tinebase_Backend_Sql_Abstract::traitGroup($_select);
         
+        $this->_checkTracing($_select);
+        
         $stmt = $this->_db->query($_select);
         
         if ($_mode === self::FETCH_ALL) {