Skip to content

Commit 0f2e277

Browse files
authored
Merge pull request #421 from bearsunday/monolog_v3
2 parents fa11420 + c2ccfa2 commit 0f2e277

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"bear/resource": "^1.20",
1919
"bear/streamer": "^1.2.2",
2020
"bear/sunday": "^1.6.1",
21-
"monolog/monolog": "^1.25 || ^2.0",
21+
"monolog/monolog": "^1.25 || ^2.0 || ^3.0",
2222
"ray/aop": "^2.13.1",
2323
"ray/di": "^2.15.1",
2424
"ray/object-visual-grapher": "^1.0",

src/Provide/Error/ErrorLogger.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use BEAR\AppMeta\AbstractAppMeta;
88
use BEAR\Sunday\Extension\Router\RouterMatch;
9-
use Monolog\Logger;
109
use Psr\Log\LoggerInterface;
1110
use Throwable;
1211

@@ -22,12 +21,29 @@ public function __construct(
2221

2322
public function __invoke(Throwable $e, RouterMatch $request): string
2423
{
25-
$level = $e->getCode() >= 500 ? Logger::ERROR : Logger::DEBUG;
24+
$isError = $e->getCode() >= 500;
2625
$logRef = new LogRef($e);
27-
$message = sprintf('req:"%s" code:%s e:%s(%s) logref:%s', (string) $request, $e->getCode(), $e::class, $e->getMessage(), (string) $logRef);
28-
$this->logger->log($level, $message);
2926
$logRef->log($e, $request, $this->appMeta);
27+
$message = sprintf('req:"%s" code:%s e:%s(%s) logref:%s', (string) $request, $e->getCode(), $e::class, $e->getMessage(), (string) $logRef);
28+
$this->log($isError, $message);
3029

3130
return (string) $logRef;
3231
}
32+
33+
/**
34+
* Log with method
35+
*
36+
* monolog has different log level constants(200,400) than psr/logger,
37+
* and those constants change from version to version.
38+
*/
39+
private function log(bool $isError, string $message): void
40+
{
41+
if ($isError) {
42+
$this->logger->error($message);
43+
44+
return;
45+
}
46+
47+
$this->logger->debug($message);
48+
}
3349
}

0 commit comments

Comments
 (0)