2
2
/**
3
3
* @package PerformancePrinter
4
4
* @author TsaiKoga
5
- * @version 1.0.0
5
+ * @version 1.1.1
6
6
*
7
7
*/
8
8
namespace TsaiKoga \PerformancePrinter \Printer ;
9
9
10
10
use Symfony \Component \Console \Helper \Table ;
11
11
use Symfony \Component \Console \Style \SymfonyStyle ;
12
12
use Symfony \Component \Console \Output \ConsoleOutput ;
13
+ use TsaiKoga \PerformancePrinter \Printer \PrinterLogger ;
13
14
14
15
class Printer extends ConsoleOutput
15
16
{
@@ -56,6 +57,21 @@ class Printer extends ConsoleOutput
56
57
*/
57
58
protected $ lang ;
58
59
60
+
61
+ /**
62
+ * is logging enabled
63
+ *
64
+ * @var bool
65
+ */
66
+ protected $ is_logging_enabled ;
67
+
68
+ /**
69
+ * logger
70
+ *
71
+ * @var \Monolog\Logger
72
+ */
73
+ protected $ logger ;
74
+
59
75
/**
60
76
* Create a new Printer instance.
61
77
*
@@ -112,6 +128,22 @@ public function setLang($lang = 'en')
112
128
return $ this ;
113
129
}
114
130
131
+ /**
132
+ * Enable logging
133
+ *
134
+ * @param string $filepath
135
+ * @return TsaiKoga\PerformancePrinter\Printer\Printer $this
136
+ */
137
+ public function enableLogging ($ filepath )
138
+ {
139
+ $ this ->is_logging_enabled = true ;
140
+ $ logger = new PrinterLogger ();
141
+ $ logger ->pushHandler ($ logger ->setFormatter ($ filepath ));
142
+ $ this ->logger = $ logger ;
143
+ return $ this ;
144
+
145
+ }
146
+
115
147
/**
116
148
* Get table
117
149
*
@@ -147,6 +179,10 @@ protected function outputTable($table, $header, $content)
147
179
$ table ->setRows ($ content );
148
180
$ table ->render ();
149
181
}
182
+
183
+ if ($ this ->is_logging_enabled ) {
184
+ $ this ->logger ->renderTable ($ header , $ content );
185
+ }
150
186
}
151
187
152
188
/**
@@ -157,10 +193,10 @@ protected function outputTable($table, $header, $content)
157
193
public function outputRequest ()
158
194
{
159
195
$ request = $ this ->request ->server ();
160
- $ this ->writeln ("<question>[ {$ request ['REQUEST_METHOD ' ]} ]</question> <info> {$ request ['REQUEST_URI ' ]}</info> " );
196
+ $ this ->outputMsg ("<question>[ {$ request ['REQUEST_METHOD ' ]} ]</question> <info> {$ request ['REQUEST_URI ' ]}</info> " );
161
197
if (in_array ($ request ['REQUEST_METHOD ' ], ['POST ' , 'PUT ' , 'PATCH ' ])) {
162
- if (isset ($ request ['HTTP_CONTENT_TYPE ' ])) $ this ->writeln ("<question>[ Content-Type ]</question> : {$ request ['HTTP_CONTENT_TYPE ' ]} " );
163
- $ this ->writeln ($ this ->request ->getContent ());
198
+ if (isset ($ request ['HTTP_CONTENT_TYPE ' ])) $ this ->outputMsg ("<question>[ Content-Type ]</question> : {$ request ['HTTP_CONTENT_TYPE ' ]} " );
199
+ $ this ->outputMsg ($ this ->request ->getContent ());
164
200
}
165
201
}
166
202
@@ -173,9 +209,9 @@ public function outputFilesCount()
173
209
{
174
210
$ included_files_count = count (get_included_files ());
175
211
if ($ this ->lang === 'en ' ) {
176
- $ this ->writeln ("<question>[ Included Files Count ] </question> $ included_files_count " );
212
+ $ this ->outputMsg ("<question>[ Included Files Count ] </question> $ included_files_count " );
177
213
} else {
178
- $ this ->writeln ("<question>[ 加载文件数量 ] </question> $ included_files_count " );
214
+ $ this ->outputMsg ("<question>[ 加载文件数量 ] </question> $ included_files_count " );
179
215
}
180
216
}
181
217
@@ -189,9 +225,9 @@ public function outputQueryCount()
189
225
$ total = $ this ->querylog ->getQueriesTotal ();
190
226
$ times = $ this ->querylog ->getQueriesRunningTime ();
191
227
if ($ this ->lang === 'en ' ) {
192
- $ this ->writeln ("<question>[ Total ]</question> $ total queries and ran for $ times ms. " );
228
+ $ this ->outputMsg ("<question>[ Total ]</question> $ total queries and ran for $ times ms. " );
193
229
} else {
194
- $ this ->writeln ("<question>[ 总计 ]</question> $ total 条语句运行了 $ times 毫秒。 " );
230
+ $ this ->outputMsg ("<question>[ 总计 ]</question> $ total 条语句运行了 $ times 毫秒。 " );
195
231
}
196
232
}
197
233
@@ -217,17 +253,17 @@ protected function outputRawSql($log)
217
253
{
218
254
if ($ this ->lang === 'en ' ) {
219
255
if ($ log ['count ' ] > 1 ) {
220
- $ this ->writeln ("<info>>>>></info> There are a total of {$ log ['count ' ]} same queries with different bindings: " );
221
- $ this ->writeln ("<question>[ These queries ran for {$ log ['time ' ]} ms totally ]</question> RAW SQL: <info> {$ log ['sql ' ]}</info> " );
256
+ $ this ->outputMsg ("<info>>>>></info> There are a total of {$ log ['count ' ]} same queries with different bindings: " );
257
+ $ this ->outputMsg ("<question>[ These queries ran for {$ log ['time ' ]} ms totally ]</question> RAW SQL: <info> {$ log ['sql ' ]}</info> " );
222
258
} else {
223
- $ this ->writeln ("<question>[ SQL ran for {$ log ['time ' ]} ms ]</question> RAW SQL: <info> {$ log ['sql ' ]}</info> " );
259
+ $ this ->outputMsg ("<question>[ SQL ran for {$ log ['time ' ]} ms ]</question> RAW SQL: <info> {$ log ['sql ' ]}</info> " );
224
260
}
225
261
} else {
226
262
if ($ log ['count ' ] > 1 ) {
227
- $ this ->writeln ("<info>>>>></info> 总共有 {$ log ['count ' ]} 条相同的查询只是查询参数不同: " );
228
- $ this ->writeln ("<question>[ 这些查询总共运行 {$ log ['time ' ]} 毫秒 ]</question> 原生 SQL: <info> {$ log ['sql ' ]}</info> " );
263
+ $ this ->outputMsg ("<info>>>>></info> 总共有 {$ log ['count ' ]} 条相同的查询只是查询参数不同: " );
264
+ $ this ->outputMsg ("<question>[ 这些查询总共运行 {$ log ['time ' ]} 毫秒 ]</question> 原生 SQL: <info> {$ log ['sql ' ]}</info> " );
229
265
} else {
230
- $ this ->writeln ("<question>[ SQL 运行了 {$ log ['time ' ]} 毫秒 ]</question> 原生 SQL: <info> {$ log ['sql ' ]}</info> " );
266
+ $ this ->outputMsg ("<question>[ SQL 运行了 {$ log ['time ' ]} 毫秒 ]</question> 原生 SQL: <info> {$ log ['sql ' ]}</info> " );
231
267
}
232
268
}
233
269
@@ -254,9 +290,9 @@ public function outputResponse()
254
290
{
255
291
$ response = $ this ->response ->content ();
256
292
if ($ this ->lang === 'en ' ) {
257
- $ this ->writeln ("<question>[ Response Load $ this ->expense ms]</question> $ response " );
293
+ $ this ->outputMsg ("<question>[ Response Load $ this ->expense ms]</question> $ response " );
258
294
} else {
259
- $ this ->writeln ("<question>[ 响应时间 $ this ->expense 毫秒 ]</question> $ response " );
295
+ $ this ->outputMsg ("<question>[ 响应时间 $ this ->expense 毫秒 ]</question> $ response " );
260
296
}
261
297
}
262
298
@@ -270,6 +306,23 @@ public function outputBlankLine($num)
270
306
{
271
307
foreach (range (1 , $ num ) as $ i ) {
272
308
$ this ->writeln ('' );
309
+ if ($ this ->is_logging_enabled ) {
310
+ $ this ->logger ->info ('' );
311
+ }
312
+ }
313
+ }
314
+
315
+ /**
316
+ * output message
317
+ *
318
+ * @param string $msg
319
+ * @return void
320
+ */
321
+ public function outputMsg ($ msg )
322
+ {
323
+ $ this ->writeln ($ msg );
324
+ if ($ this ->is_logging_enabled ) {
325
+ $ this ->logger ->info (strip_tags ($ msg ));
273
326
}
274
327
}
275
328
@@ -285,5 +338,4 @@ public function outputBlankLine($num)
285
338
protected function outputIndexSuggestion ($ sql , $ query , $ bindings , $ explains )
286
339
{
287
340
}
288
-
289
341
}
0 commit comments