File tree 3 files changed +103
-0
lines changed
3 files changed +103
-0
lines changed Original file line number Diff line number Diff line change 22
22
},
23
23
"conflict-note" : " We are using 'conflict' combined with 'require-dev' to '*' as soft require." ,
24
24
"conflict" : {
25
+ "psr/log" : " <1.0 || >= 2" ,
25
26
"tracy/tracy" : " <2.2 || >=3" ,
26
27
"nette/di" : " <2.2 || >=3"
27
28
},
28
29
"require-dev" : {
30
+ "psr/log" : " *" ,
29
31
"tracy/tracy" : " *" ,
30
32
"nette/di" : " *" ,
31
33
"nette/bootstrap" : " ^2.3" ,
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Netpromotion \Profiler \Adapter ;
4
+
5
+ use Netpromotion \Profiler \Profiler ;
6
+ use /** @noinspection PhpInternalEntityUsedInspection */ Netpromotion \Profiler \Service \ProfilerService ;
7
+ use PetrKnap \Php \Profiler \Profile ;
8
+ use Psr \Log \LoggerInterface ;
9
+
10
+ class PsrLoggerAdapter
11
+ {
12
+ /**
13
+ * @var ProfilerService
14
+ */
15
+ private $ service ;
16
+
17
+ /**
18
+ * @var LoggerInterface
19
+ */
20
+ private $ logger ;
21
+
22
+ /**
23
+ * @param LoggerInterface $logger
24
+ */
25
+ public function __construct (LoggerInterface $ logger )
26
+ {
27
+ /** @noinspection PhpInternalEntityUsedInspection */
28
+ $ this ->service = ProfilerService::getInstance ();
29
+ $ this ->logger = $ logger ;
30
+ }
31
+
32
+ /**
33
+ * Logs known profiles
34
+ *
35
+ * @return void
36
+ */
37
+ public function log ()
38
+ {
39
+ if (Profiler::isEnabled ()) {
40
+ $ this ->service ->iterateProfiles (function (Profile $ profile ) {
41
+ $ this ->logger ->debug (sprintf (
42
+ "%s -> %s: %d ms, %d kB " ,
43
+ $ profile ->meta [Profiler::START_LABEL ],
44
+ $ profile ->meta [Profiler::FINISH_LABEL ],
45
+ $ profile ->duration * 1000 ,
46
+ $ profile ->memoryUsageChange / 1024
47
+ ), $ profile ->jsonSerialize ());
48
+ });
49
+ }
50
+ }
51
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Netpromotion \Profiler \Test \Adapter ;
4
+
5
+ use Netpromotion \Profiler \Adapter \PsrLoggerAdapter ;
6
+ use Netpromotion \Profiler \Profiler ;
7
+
8
+ class PsrLoggerAdapterTest extends \PHPUnit_Framework_TestCase
9
+ {
10
+ /**
11
+ * @dataProvider dataAdapterWorks
12
+ * @param bool $enabled
13
+ * @runInSeparateProcess
14
+ */
15
+ public function testAdapterWorks ($ enabled )
16
+ {
17
+ $ logger = $ this ->getMock ("Psr \\Log \\LoggerInterface " );
18
+ $ logger ->expects ($ this ->exactly ($ enabled ? 6 : 0 ))
19
+ ->method ("debug " )->willReturn (null );
20
+ $ adapter = new PsrLoggerAdapter ($ logger );
21
+
22
+ if ($ enabled ) {
23
+ Profiler::enable ();
24
+ }
25
+
26
+ Profiler::start ("1 " );
27
+ {
28
+ Profiler::start ("1.1 " );
29
+ {
30
+ Profiler::start ("1.1.1 " );
31
+ Profiler::finish ("1.1.1 " );
32
+ Profiler::start ("1.1.2 " );
33
+ Profiler::finish ("1.1.2 " );
34
+ }
35
+ Profiler::finish ("1.1 " );
36
+ Profiler::start ("1.2 " );
37
+ Profiler::finish ("1.2 " );
38
+ }
39
+ Profiler::finish ("1 " );
40
+ Profiler::start ("2 " );
41
+ Profiler::finish ("2 " );
42
+
43
+ $ adapter ->log ();
44
+ }
45
+
46
+ public function dataAdapterWorks ()
47
+ {
48
+ return [[false ], [true ]];
49
+ }
50
+ }
You can’t perform that action at this time.
0 commit comments