Development

/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/log/sfDoctrineLogger.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/log/sfDoctrineLogger.class.php

Revision 13393, 3.0 kB (checked in by Jonathan.Wage, 6 years ago)

[1.2] sfDoctrinePlugin: fixes issue with query params with binary data causing problems in the logs (closes #5052)

Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) Jonathan H. Wage <jonwage@gmail.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * Logs queries to file and web debug toolbar
14  *
15  * @package    symfony
16  * @package    doctrine
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     Jonathan H. Wage <jonwage@gmail.com>
19  * @version    SVN: $Id: sfDoctrineLogger.class.php 8938 2008-05-14 01:52:35Z Jonathan.Wage $
20  */
21 class sfDoctrineLogger extends Doctrine_EventListener
22 {
23   protected $connection = null,
24             $encoding = 'UTF8';
25
26   /**
27    * Log a query before it is executed
28    *
29    * @param Doctrine_Event $event
30    * @return void
31    */
32   public function preExecute(Doctrine_Event $event)
33   {
34     $this->sfLogQuery('executeQuery : ', $event);
35   }
36
37   /**
38    * Add the time after a query is executed
39    *
40    * @param Doctrine_Event $event
41    * @return void
42    */
43   public function postExecute(Doctrine_Event $event)
44   {
45     $this->sfAddTime();
46   }
47
48   /**
49    * Add the time after a query is prepared
50    *
51    * @param Doctrine_Event $event
52    * @return void
53    */
54   public function postPrepare(Doctrine_Event $event)
55   {
56     $this->sfAddTime();
57   }
58
59   /**
60    * Before a query statement is executed log it
61    *
62    * @param Doctrine_Event $event
63    * @return void
64    */
65   public function preStmtExecute(Doctrine_Event $event)
66   {
67     $this->sfLogQuery('executeQuery : ', $event);
68   }
69
70   /**
71    * postStmtExecute
72    *
73    * @param Doctrine_Event $event
74    * @return void
75    */
76   public function postStmtExecute(Doctrine_Event $event)
77   {
78     $this->sfAddTime();
79   }
80
81   /**
82    * Log a query before it is executed
83    *
84    * @param Doctrine_Event $event
85    * @return void
86    */
87   public function preQuery(Doctrine_Event $event)
88   {
89     $this->sfLogQuery('executeQuery : ', $event);
90   }
91
92   /**
93    * Post query add the time
94    *
95    * @param string $Doctrine_Event
96    * @return void
97    */
98   public function postQuery(Doctrine_Event $event)
99   {
100     $this->sfAddTime();
101   }
102
103   /**
104    * Log a Doctrine_Query
105    *
106    * @param string $message
107    * @param string $event
108    * @return void
109    */
110   protected function sfLogQuery($message, $event)
111   {
112     $message .= $event->getQuery();
113
114     if ($params = $event->getParams())
115     {
116       foreach ($params as $key => $param)
117       {
118         if (strlen($param) >= 255)
119         {
120           $len = strlen($param);
121           $kb = '[' . number_format($len / 1024, 2) . 'Kb]';
122           $params[$key] = $kb;
123         }
124       }
125       $message .= ' - ('.implode(', ', $params) . ' )';
126     }
127
128     $message = '{sfDoctrineLogger} ' . $message;
129     if (sfContext::hasInstance())
130     {
131       sfContext::getInstance()->getLogger()->log($message);
132     }
133
134     $sqlTimer = sfTimerManager::getTimer('Database (Doctrine)');
135   }
136
137   /**
138    * Add the time to the log
139    *
140    * @return void
141    */
142   protected function sfAddTime()
143   {
144     sfTimerManager::getTimer('Database (Doctrine)')->addTime();
145   }
146 }
Note: See TracBrowser for help on using the browser.