Skip to content

Commit 744e709

Browse files
committed
Log only above warning by default
1 parent 2a29bf5 commit 744e709

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ The logger does not require any configuration:
2727
$logger = new \Bref\Logger\StderrLogger();
2828
```
2929

30+
By default messages **above the `warning` level** will be logged, the rest will be discarded.
31+
3032
It is possible to log using any [PSR-3 log level](https://www.php-fig.org/psr/psr-3/#5-psrlogloglevel), the most common ones being:
3133

3234
```php
@@ -37,12 +39,12 @@ $logger->error('This is an error');
3739
```
3840

3941
```
40-
[DEBUG] This is a debug message
41-
[INFO] This is an info
4242
[WARNING] This is a warning
4343
[ERROR] This is an error
4444
```
4545

46+
Messages under `warning` are not logged.
47+
4648
### Message placeholders
4749

4850
[PSR-3 placeholders](https://www.php-fig.org/psr/psr-3/#12-message) can be used to insert information from the `$context` array into the message without having to concatenate strings manually:
@@ -79,3 +81,13 @@ Stack trace:
7981
#0 /var/task/index.php(86): main()
8082
...
8183
```
84+
85+
### Log level
86+
87+
It is possible to change the level above which messages are logged.
88+
89+
For example to log all messages:
90+
91+
```php
92+
$logger = new \Bref\Logger\StderrLogger(\Psr\Log\LogLevel::DEBUG);
93+
```

src/StderrLogger.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,41 @@
33
namespace Bref\Logger;
44

55
use Psr\Log\AbstractLogger;
6+
use Psr\Log\LogLevel;
67

78
/**
89
* PSR-3 logger that logs into stderr.
910
*/
1011
class StderrLogger extends AbstractLogger
1112
{
13+
private const LOG_LEVEL_MAP = [
14+
LogLevel::EMERGENCY => 8,
15+
LogLevel::ALERT => 7,
16+
LogLevel::CRITICAL => 6,
17+
LogLevel::ERROR => 5,
18+
LogLevel::WARNING => 4,
19+
LogLevel::NOTICE => 3,
20+
LogLevel::INFO => 2,
21+
LogLevel::DEBUG => 1,
22+
];
23+
24+
/** @var string */
25+
private $logLevel;
26+
1227
/** @var string|null */
1328
private $url;
1429

1530
/** @var resource|null */
1631
private $stream;
1732

1833
/**
34+
* @param string $logLevel The log level above which messages will be logged. Messages under this log level will be ignored.
1935
* @param resource|string $stream If unsure leave the default value.
2036
*/
21-
public function __construct($stream = 'php://stderr')
37+
public function __construct(string $logLevel = LogLevel::WARNING, $stream = 'php://stderr')
2238
{
39+
$this->logLevel = $logLevel;
40+
2341
if (is_resource($stream)) {
2442
$this->stream = $stream;
2543
} elseif (is_string($stream)) {
@@ -34,6 +52,10 @@ public function __construct($stream = 'php://stderr')
3452
*/
3553
public function log($level, $message, array $context = []): void
3654
{
55+
if (self::LOG_LEVEL_MAP[$level] < self::LOG_LEVEL_MAP[$this->logLevel]) {
56+
return;
57+
}
58+
3759
$this->openStderr();
3860

3961
$message = $this->interpolate($message, $context);

tests/StderrLoggerTest.php

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

55
use Bref\Logger\StderrLogger;
66
use PHPUnit\Framework\TestCase;
7+
use Psr\Log\LogLevel;
78

89
class StderrLoggerTest extends TestCase
910
{
@@ -17,7 +18,7 @@ public function setUp(): void
1718
parent::setUp();
1819

1920
$this->stream = fopen('php://memory', 'a+');
20-
$this->logger = new StderrLogger($this->stream);
21+
$this->logger = new StderrLogger(LogLevel::DEBUG, $this->stream);
2122
}
2223

2324
public function test log messages to stderr()
@@ -45,6 +46,29 @@ public function test log messages to stderr()
4546
);
4647
}
4748

49+
public function test logs above the configured log level()
50+
{
51+
$this->logger = new StderrLogger(LogLevel::WARNING, $this->stream);
52+
$this->logger->debug('Debug');
53+
$this->logger->info('Info');
54+
$this->logger->notice('Notice');
55+
$this->logger->warning('Alert');
56+
$this->logger->error('Error');
57+
$this->logger->critical('Critical');
58+
$this->logger->alert('Alert');
59+
$this->logger->emergency('Emergency');
60+
61+
$this->assertLogs(<<<'LOGS'
62+
[WARNING] Alert
63+
[ERROR] Error
64+
[CRITICAL] Critical
65+
[ALERT] Alert
66+
[EMERGENCY] Emergency
67+
68+
LOGS
69+
);
70+
}
71+
4872
/**
4973
* @param mixed $contextValue
5074
*
@@ -89,11 +113,12 @@ public function test exceptions are logged()
89113
]);
90114

91115
$currentFile = __FILE__;
92-
$this->assertLogsStartWith(<<<LOGS
116+
$this->assertLogsMatch(<<<LOGS
93117
[INFO] Exception
94-
InvalidArgumentException: This is an exception message in $currentFile:115
118+
InvalidArgumentException: This is an exception message in $currentFile:%d
95119
Stack trace:
96-
#0 $currentFile(88): Bref\Logger\Test\StderrLoggerTest->createTestException()
120+
#0 $currentFile(%d): Bref\Logger\Test\StderrLoggerTest->createTestException()
121+
#1 %a
97122
LOGS
98123
);
99124
}
@@ -104,10 +129,10 @@ private function assertLogs(string $expectedLog): void
104129
self::assertSame($expectedLog, fread($this->stream, fstat($this->stream)['size']));
105130
}
106131

107-
private function assertLogsStartWith(string $expectedLog): void
132+
private function assertLogsMatch(string $expectedLog): void
108133
{
109134
rewind($this->stream);
110-
self::assertStringStartsWith($expectedLog, fread($this->stream, fstat($this->stream)['size']));
135+
self::assertStringMatchesFormat($expectedLog, fread($this->stream, fstat($this->stream)['size']));
111136
}
112137

113138
private function createTestException(): \InvalidArgumentException

0 commit comments

Comments
 (0)