Skip to content

Commit 3ce517d

Browse files
JanTvrdikdg
authored andcommitted
add PSR-3 adapters (#314)
1 parent f7ecd3c commit 3ce517d

File tree

5 files changed

+217
-1
lines changed

5 files changed

+217
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"require-dev": {
2323
"nette/utils": "^2.4 || ^3.0",
2424
"nette/di": "^2.4 || ~3.0.0",
25-
"nette/tester": "^2.0"
25+
"nette/tester": "^2.0",
26+
"psr/log": "^1.0"
2627
},
2728
"suggest": {
2829
"https://nette.org/donate": "Please support Tracy via a donation"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Tracy (https://tracy.nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Tracy\Bridges\Psr;
11+
12+
use Psr;
13+
use Tracy;
14+
15+
16+
/**
17+
* Psr\Log\LoggerInterface to Tracy\ILogger adapter.
18+
*/
19+
class PsrToTracyLoggerAdapter implements Tracy\ILogger
20+
{
21+
/** Tracy logger priority to PSR-3 log level mapping */
22+
private const PRIORITY_MAP = [
23+
Tracy\ILogger::DEBUG => Psr\Log\LogLevel::DEBUG,
24+
Tracy\ILogger::INFO => Psr\Log\LogLevel::INFO,
25+
Tracy\ILogger::WARNING => Psr\Log\LogLevel::WARNING,
26+
Tracy\ILogger::ERROR => Psr\Log\LogLevel::ERROR,
27+
Tracy\ILogger::EXCEPTION => Psr\Log\LogLevel::ERROR,
28+
Tracy\ILogger::CRITICAL => Psr\Log\LogLevel::CRITICAL,
29+
];
30+
31+
/** @var Psr\Log\LoggerInterface */
32+
private $psrLogger;
33+
34+
35+
public function __construct(Psr\Log\LoggerInterface $psrLogger)
36+
{
37+
$this->psrLogger = $psrLogger;
38+
}
39+
40+
41+
public function log($value, string $priority = self::INFO)
42+
{
43+
if ($value instanceof \Throwable) {
44+
$message = $value->getMessage();
45+
$context = ['exception' => $value];
46+
47+
} elseif (!is_string($value)) {
48+
$message = trim(Tracy\Dumper::toText($value));
49+
$context = [];
50+
51+
} else {
52+
$message = $value;
53+
$context = [];
54+
}
55+
56+
$this->psrLogger->log(
57+
self::PRIORITY_MAP[$priority] ?? Psr\Log\LogLevel::ERROR,
58+
$message,
59+
$context
60+
);
61+
}
62+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Tracy (https://tracy.nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Tracy\Bridges\Psr;
11+
12+
use Psr;
13+
use Tracy;
14+
15+
16+
/**
17+
* Tracy\ILogger to Psr\Log\LoggerInterface adapter.
18+
*/
19+
class TracyToPsrLoggerAdapter extends Psr\Log\AbstractLogger
20+
{
21+
/** PSR-3 log level to Tracy logger priority mapping */
22+
private const PRIORITY_MAP = [
23+
Psr\Log\LogLevel::EMERGENCY => Tracy\ILogger::CRITICAL,
24+
Psr\Log\LogLevel::ALERT => Tracy\ILogger::CRITICAL,
25+
Psr\Log\LogLevel::CRITICAL => Tracy\ILogger::CRITICAL,
26+
Psr\Log\LogLevel::ERROR => Tracy\ILogger::ERROR,
27+
Psr\Log\LogLevel::WARNING => Tracy\ILogger::WARNING,
28+
Psr\Log\LogLevel::NOTICE => Tracy\ILogger::WARNING,
29+
Psr\Log\LogLevel::INFO => Tracy\ILogger::INFO,
30+
Psr\Log\LogLevel::DEBUG => Tracy\ILogger::DEBUG,
31+
];
32+
33+
/** @var Tracy\ILogger */
34+
private $tracyLogger;
35+
36+
37+
public function __construct(Tracy\ILogger $tracyLogger)
38+
{
39+
$this->tracyLogger = $tracyLogger;
40+
}
41+
42+
43+
public function log($level, $message, array $context = [])
44+
{
45+
$priority = self::PRIORITY_MAP[$level] ?? Tracy\ILogger::ERROR;
46+
47+
if (isset($context['exception']) && $context['exception'] instanceof \Throwable) {
48+
$this->tracyLogger->log($context['exception'], $priority);
49+
unset($context['exception']);
50+
}
51+
52+
if ($context) {
53+
$message = [
54+
'message' => $message,
55+
'context' => $context,
56+
];
57+
}
58+
59+
$this->tracyLogger->log($message, $priority);
60+
}
61+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* Test: PsrToTracyLoggerAdapter.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Tester\Assert;
10+
use Tracy\Bridges\Psr\PsrToTracyLoggerAdapter;
11+
use Tracy\ILogger;
12+
13+
14+
require __DIR__ . '/../bootstrap.php';
15+
16+
class DummyPsrLogger extends Psr\Log\AbstractLogger
17+
{
18+
/** @var array */
19+
public $entries = [];
20+
21+
22+
public function log($level, $message, array $context = [])
23+
{
24+
$this->entries[] = [$level, $message, $context];
25+
}
26+
}
27+
28+
29+
$psrLogger = new DummyPsrLogger;
30+
$tracyLogger = new PsrToTracyLoggerAdapter($psrLogger);
31+
$exception = new \Exception('Something went wrong');
32+
33+
$tracyLogger->log('info');
34+
$tracyLogger->log('warning', ILogger::WARNING);
35+
$tracyLogger->log(123);
36+
$tracyLogger->log(['x' => 'y']);
37+
$tracyLogger->log($exception);
38+
39+
Assert::same([
40+
[Psr\Log\LogLevel::INFO, 'info', []],
41+
[Psr\Log\LogLevel::WARNING, 'warning', []],
42+
[Psr\Log\LogLevel::INFO, '123', []],
43+
[Psr\Log\LogLevel::INFO, "array (1)\n x => \"y\"", []],
44+
[Psr\Log\LogLevel::INFO, 'Something went wrong', ['exception' => $exception]],
45+
], $psrLogger->entries);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* Test: TracyToPsrLoggerAdapter.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Tester\Assert;
10+
use Tracy\Bridges\Psr\TracyToPsrLoggerAdapter;
11+
use Tracy\ILogger;
12+
13+
14+
require __DIR__ . '/../bootstrap.php';
15+
16+
class DummyTracyLogger implements ILogger
17+
{
18+
/** @var array */
19+
public $entries = [];
20+
21+
22+
public function log($value, string $priority = self::INFO)
23+
{
24+
$this->entries[] = [$priority, $value];
25+
}
26+
}
27+
28+
29+
$tracyLogger = new DummyTracyLogger;
30+
$psrLogger = new TracyToPsrLoggerAdapter($tracyLogger);
31+
$exception = new \Exception('Something went wrong');
32+
33+
$psrLogger->info('info');
34+
$psrLogger->warning('warning');
35+
$psrLogger->error('order failed with exception', ['exception' => $exception]);
36+
$psrLogger->error('order failed with context', ['orderId' => 123]);
37+
$psrLogger->error('order failed with context and exception', ['orderId' => 123, 'exception' => $exception]);
38+
39+
Assert::same([
40+
[ILogger::INFO, 'info'],
41+
[ILogger::WARNING, 'warning'],
42+
[ILogger::ERROR, $exception],
43+
[ILogger::ERROR, 'order failed with exception'],
44+
[ILogger::ERROR, ['message' => 'order failed with context', 'context' => ['orderId' => 123]]],
45+
[ILogger::ERROR, $exception],
46+
[ILogger::ERROR, ['message' => 'order failed with context and exception', 'context' => ['orderId' => 123]]],
47+
], $tracyLogger->entries);

0 commit comments

Comments
 (0)