Skip to content

Commit 6f11c31

Browse files
author
Petr Knap
committed
Added PsrLoggerAdapter
1 parent b9cc642 commit 6f11c31

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
},
2323
"conflict-note": "We are using 'conflict' combined with 'require-dev' to '*' as soft require.",
2424
"conflict": {
25+
"psr/log": "<1.0 || >= 2",
2526
"tracy/tracy": "<2.2 || >=3",
2627
"nette/di": "<2.2 || >=3"
2728
},
2829
"require-dev": {
30+
"psr/log": "*",
2931
"tracy/tracy": "*",
3032
"nette/di": "*",
3133
"nette/bootstrap": "^2.3",
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

0 commit comments

Comments
 (0)