Development

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

You must first sign up to be able to contribute.

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

Revision 23810, 3.4 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
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$
20  *
21  * @deprecated In favor of {@link sfDoctrineConnectionProfiler}. Will be removed in symfony 1.4.
22  */
23 class sfDoctrineLogger extends Doctrine_EventListener
24 {
25   protected $connection = null,
26             $encoding = 'UTF8',
27             $timer = null;
28
29   public function __construct()
30   {
31     $this->timer = sfTimerManager::getTimer('Database (Doctrine)');
32   }
33
34   /**
35    * Log a query before it is executed
36    *
37    * @param Doctrine_Event $event
38    * @return void
39    */
40   public function preExecute(Doctrine_Event $event)
41   {
42     $this->timer->startTimer();
43   }
44
45   /**
46    * Add the time after a query is executed
47    *
48    * @param Doctrine_Event $event
49    * @return void
50    */
51   public function postExecute(Doctrine_Event $event)
52   {
53     $this->sfLogQuery("executeQuery : ", $event, $this->timer->addTime());
54   }
55
56   /**
57    * Add the time before a query is prepared
58    *
59    * @param Doctrine_Event $event
60    * @return void
61    */
62   public function prePrepare(Doctrine_Event $event)
63   {
64     $this->timer->startTimer();
65   }
66
67   /**
68    * Add the time after a query is prepared
69    *
70    * @param Doctrine_Event $event
71    * @return void
72    */
73   public function postPrepare(Doctrine_Event $event)
74   {
75     $this->timer->addTime();
76   }
77
78   /**
79    * Before a query statement is executed log it
80    *
81    * @param Doctrine_Event $event
82    * @return void
83    */
84   public function preStmtExecute(Doctrine_Event $event)
85   {
86     $this->timer->startTimer();
87   }
88
89   /**
90    * postStmtExecute
91    *
92    * @param Doctrine_Event $event
93    * @return void
94    */
95   public function postStmtExecute(Doctrine_Event $event)
96   {
97     $this->sfLogQuery('executeQuery : ', $event, $this->timer->addTime());
98   }
99
100   /**
101    * Log a query before it is executed
102    *
103    * @param Doctrine_Event $event
104    * @return void
105    */
106   public function preQuery(Doctrine_Event $event)
107   {
108     $this->timer->startTimer();
109   }
110
111   /**
112    * Post query add the time
113    *
114    * @param string $Doctrine_Event
115    * @return void
116    */
117   public function postQuery(Doctrine_Event $event)
118   {
119     $this->sfLogQuery('executeQuery : ', $event, $this->timer->addTime());
120   }
121
122   /**
123    * Log a Doctrine_Query
124    *
125    * @param string $message
126    * @param string $event
127    * @return void
128    */
129   protected function sfLogQuery($message, $event, $time)
130   {
131     $message .= $event->getQuery();
132
133     if ($params = $event->getParams())
134     {
135       foreach ($params as $key => $param)
136       {
137         if (strlen($param) >= 255)
138         {
139           $len = strlen($param);
140           $kb = '[' . number_format($len / 1024, 2) . 'Kb]';
141           $params[$key] = $kb;
142         }
143       }
144       $message .= ' - ('.implode(', ', $params) . ' )';
145     }
146     
147     $message = sprintf('{sfDoctrineLogger} [%.2f ms] %s', $time * 1000, $message);
148     if (sfContext::hasInstance())
149     {
150       sfContext::getInstance()->getLogger()->log($message);
151     }
152
153     $sqlTimer = sfTimerManager::getTimer('Database (Doctrine)');
154   }
155 }
Note: See TracBrowser for help on using the browser.