@@ -29,7 +29,39 @@ class DatabaseLogger
2929
3030 private function __construct ()
3131 {
32+ // private constructor - singleton can be created only by itself
33+ }
34+
35+ /**
36+ * @return DatabaseLogger
37+ */
38+ public static function getInstance ()
39+ {
40+ if (!isset (self ::$ uniqueInstance )) {
41+ self ::$ uniqueInstance = new self ;
42+ }
43+
44+ return self ::$ uniqueInstance ;
45+ }
46+
47+ public static function getQueryLog ()
48+ {
49+ return self ::getInstance ()->getLog ();
50+ }
51+
52+ public static function getErrorLog ()
53+ {
54+ return self ::getInstance ()->errorLog ;
55+ }
56+
57+ public static function getTotalQueriesNumber ()
58+ {
59+ return self ::getInstance ()->queries ;
60+ }
3261
62+ public static function getTotalTime ()
63+ {
64+ return self ::getInstance ()->time ;
3365 }
3466
3567 public static function logStart ()
@@ -42,6 +74,17 @@ public static function logEnd($query, $params = null)
4274 self ::getInstance ()->logQuery ($ query , microtime (true ) - self ::$ lastStartTime , $ params );
4375 }
4476
77+ public static function logError ($ query , $ error , $ errorCode , $ params = null )
78+ {
79+ self ::getInstance ()->_logError ($ query , microtime (true ) - self ::$ lastStartTime , $ error , $ errorCode , $ params );
80+ }
81+
82+ public static function setQueryLogging ($ flag )
83+ {
84+ return self ::getInstance ()->_setQueryLogging ($ flag );
85+ }
86+
87+
4588 public function logQuery ($ query , $ time , $ params )
4689 {
4790 $ this ->time += $ time ;
@@ -52,6 +95,60 @@ public function logQuery($query, $time, $params)
5295 }
5396 }
5497
98+ public function getLog ()
99+ {
100+ if ($ this ->addExplain ) {
101+ $ this ->addExplain ();
102+ }
103+
104+ return $ this ->log ;
105+ }
106+
107+ public function _logError ($ query , $ time , $ error , $ errorCode , $ params = null )
108+ {
109+ if ($ this ->queryLogging or $ this ->debug ) {
110+ $ logItem = $ this ->prepareLogEntry ($ query , $ time , $ params , true );
111+ $ logItem ['error ' ] = [
112+ 'code ' => $ errorCode ,
113+ 'message ' => $ error
114+ ];
115+
116+ $ this ->errorLog [] = $ logItem ;
117+ }
118+ }
119+
120+ public function setDebug ($ flag )
121+ {
122+ $ this ->debug = (bool )$ flag ;
123+ if ($ this ->debug ) {
124+ $ this ->_setQueryLogging (true );
125+ }
126+
127+ return $ this ;
128+ }
129+
130+ public function addIncludeDebugDir ($ namespace )
131+ {
132+ self ::getInstance ()->getBacktracer ()->addIncludeDir ($ namespace );
133+
134+ return $ this ;
135+ }
136+
137+ public function enableExplain (\PDO $ pdo )
138+ {
139+ $ this ->addExplain = true ;
140+ $ this ->pdo = $ pdo ;
141+
142+ return $ this ;
143+ }
144+
145+ public function setPrettyPrint ($ flag )
146+ {
147+ $ this ->prettyPrint = $ flag ;
148+
149+ return $ this ;
150+ }
151+
55152 private function prepareLogEntry ($ query , $ time , $ params , $ trace = false )
56153 {
57154 $ stats = array (
@@ -78,7 +175,11 @@ private function formatQuery($query)
78175 $ query = preg_replace ('/ (order by|limit|having) /i ' , "\n $1 " , $ query );
79176 $ query = preg_replace ('/ (union( (all|distinct))?) /i ' , "\n$1 \n" , $ query );
80177
81- $ query = preg_replace ('/(?<=^|\s|\()(select|from|explain|update|insert|replace|left|right|outer|inner|join|where|order by|limit|as|and|or|having|union|all|distinct|on|is|not|null|true|false|desc|asc|between|in)(?=$|\s|\)|,)/i ' , "<b>$1</b> " , $ query );
178+ $ query = preg_replace (
179+ '/(?<=^|\s|\()(select|set|from|explain|update|insert|replace|left|right|outer|inner|join|where|order by| '
180+ . 'limit|as|and|or|having|union|all|distinct|on|is|not|null|true|false|desc|asc|between|in) '
181+ . '(?=$|\s|\)|,)/ix ' , "<b>$1</b> " , $ query
182+ );
82183
83184 return $ query ;
84185 }
@@ -128,78 +229,13 @@ private function getBacktracer()
128229 return $ this ->backtracer ;
129230 }
130231
131- /**
132- * @return DatabaseLogger
133- */
134- public static function getInstance ()
135- {
136- if (!isset (self ::$ uniqueInstance )) {
137- self ::$ uniqueInstance = new self ;
138- }
139-
140- return self ::$ uniqueInstance ;
141- }
142-
143- /**
144- * @param $query
145- * @param $error
146- * @param $errorCode
147- * @param null $params
148- */
149- public static function logError ($ query , $ error , $ errorCode , $ params = null )
150- {
151- self ::getInstance ()->_logError ($ query , microtime (true ) - self ::$ lastStartTime , $ error , $ errorCode , $ params );
152- }
153-
154- public function _logError ($ query , $ time , $ error , $ errorCode , $ params = null )
155- {
156- if ($ this ->queryLogging or $ this ->debug ) {
157- $ logItem = $ this ->prepareLogEntry ($ query , $ time , $ params , true );
158- $ logItem ['error ' ] = [
159- 'code ' => $ errorCode ,
160- 'message ' => $ error
161- ];
162-
163- $ this ->errorLog [] = $ logItem ;
164- }
165- }
166-
167- public static function getTotalQueriesNumber ()
168- {
169- return self ::getInstance ()->queries ;
170- }
171-
172- public static function getTotalTime ()
173- {
174- return self ::getInstance ()->time ;
175- }
176-
177- public static function getErrorLog ()
178- {
179- return self ::getInstance ()->errorLog ;
180- }
181-
182- public static function setQueryLogging ($ flag )
183- {
184- self ::getInstance ()->_setQueryLogging ($ flag );
185- }
186-
187232 private function _setQueryLogging ($ flag )
188233 {
189234 $ this ->queryLogging = (bool )$ flag ;
190235
191236 return $ this ;
192237 }
193238
194- public function getLog ()
195- {
196- if ($ this ->addExplain ) {
197- $ this ->addExplain ();
198- }
199-
200- return $ this ->log ;
201- }
202-
203239 private function addExplain ()
204240 {
205241 $ this ->disableLogging ();
@@ -246,32 +282,4 @@ private function enableLogging()
246282 {
247283 $ this ->pdo ->setAttribute (\PDO ::ATTR_STATEMENT_CLASS , $ this ->statementClass );
248284 }
249-
250- public function enableExplain (\PDO $ pdo )
251- {
252- $ this ->addExplain = true ;
253- $ this ->pdo = $ pdo ;
254- }
255-
256- public function addIncludeDebugDir ($ namespace )
257- {
258- self ::getInstance ()->getBacktracer ()->addIncludeDir ($ namespace );
259- }
260-
261- public function setDebug ($ flag )
262- {
263- $ this ->debug = (bool )$ flag ;
264- if ($ this ->debug ) {
265- $ this ->_setQueryLogging (true );
266- }
267-
268- return $ this ;
269- }
270-
271- public function setPrettyPrint ($ flag )
272- {
273- $ this ->prettyPrint = $ flag ;
274-
275- return $ this ;
276- }
277285}
0 commit comments