Skip to content

Commit ca042cc

Browse files
Allow psr/log v2 and v3 dep (#392)
Co-authored-by: Michael Voříšek <mvorisek@mvorisek.cz>
1 parent 2403296 commit ca042cc

File tree

4 files changed

+65
-94
lines changed

4 files changed

+65
-94
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"require": {
3333
"php": ">=7.4 <8.4",
3434
"benmorel/weakmap-polyfill": "^0.4.0",
35-
"psr/log": "^1.0",
35+
"psr/log": "^1.0 || ^2.0 || ^3.0",
3636
"symfony/polyfill-php80": "^1.28",
3737
"symfony/polyfill-php81": "^1.28",
3838
"symfony/polyfill-php82": "^1.28",

docs/debug.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ described below:
2828

2929
The design goal of Debug is to be able to display contextual debug information
3030
only when it's manually enabled. For instance, if you are having problem with
31-
user authentication, you should enable `$auth->debug()`. On other hand - if
32-
you wish to see persistence-related debug info, then `$db->debug()` will
31+
user authentication, you should enable `$auth->debug(true)`. On other hand - if
32+
you wish to see persistence-related debug info, then `$db->debug(true)` will
3333
enable that.
3434

3535
Information logged through debug like this on any object that implements
@@ -48,7 +48,7 @@ most cases this will simply be ignored right away unless you manually enable
4848
debugging for the object:
4949

5050
```
51-
$obj1->debug(); // enable debugging
51+
$obj1->debug(true); // enable debugging
5252
$obj1->debug(false); // disable debugging
5353
$obj1->debug(true); // also enables debugging
5454

src/DebugTrait.php

+50-72
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,34 @@ protected function _echoStderr(string $message): void
2323
}
2424

2525
/**
26-
* Send some info to debug stream.
26+
* Logs with an arbitrary level.
2727
*
28-
* @param bool|string $message
29-
* @param array<mixed> $context
28+
* @param mixed $level
29+
* @param string|\Stringable $message
30+
* @param array<mixed> $context
31+
*/
32+
public function log($level, $message, array $context = []): void
33+
{
34+
if (TraitUtil::hasAppScopeTrait($this) && $this->issetApp() && $this->getApp()->logger instanceof \Psr\Log\LoggerInterface) {
35+
$this->getApp()->logger->log($level, $message, $context);
36+
} else {
37+
$this->_echoStderr($message . "\n");
38+
}
39+
}
40+
41+
/**
42+
* Detailed debug information.
3043
*
31-
* @return $this
44+
* @param bool|string|\Stringable $message
45+
* @param array<mixed> $context
3246
*/
33-
public function debug($message = true, array $context = [])
47+
public function debug($message, array $context = []): void
3448
{
3549
// using this to switch on/off the debug for this object
3650
if (is_bool($message)) {
3751
$this->debug = $message;
3852

39-
return $this;
53+
return;
4054
}
4155

4256
// if debug is enabled, then log it
@@ -46,28 +60,6 @@ public function debug($message = true, array $context = [])
4660
}
4761
$this->log(LogLevel::DEBUG, $message, $context);
4862
}
49-
50-
return $this;
51-
}
52-
53-
/**
54-
* Output log message.
55-
*
56-
* @param mixed $level
57-
* @param string $message
58-
* @param array<mixed> $context
59-
*
60-
* @return $this
61-
*/
62-
public function log($level, $message, array $context = [])
63-
{
64-
if (TraitUtil::hasAppScopeTrait($this) && $this->issetApp() && $this->getApp()->logger instanceof \Psr\Log\LoggerInterface) {
65-
$this->getApp()->logger->log($level, $message, $context);
66-
} else {
67-
$this->_echoStderr($message . "\n");
68-
}
69-
70-
return $this;
7163
}
7264

7365
/**
@@ -79,7 +71,7 @@ public function log($level, $message, array $context = [])
7971
* Place debugTraceChange inside your hook and give unique $trace identifier. If the method
8072
* is invoked through different call paths, this debug info will be logged.
8173
*
82-
* Do not leave this method in production code !!!
74+
* Do not use this method in production code !!!
8375
*/
8476
public function debugTraceChange(string $trace = 'default'): void
8577
{
@@ -94,7 +86,7 @@ public function debugTraceChange(string $trace = 'default'): void
9486
$d1 = array_diff($this->_previousTrace[$trace], $bt);
9587
$d2 = array_diff($bt, $this->_previousTrace[$trace]);
9688

97-
$this->log('debug', 'Call path for ' . $trace . ' has diverged (was ' . implode(', ', $d1) . ', now ' . implode(', ', $d2) . ")\n");
89+
$this->log(LogLevel::DEBUG, 'Call path for ' . $trace . ' has diverged (was ' . implode(', ', $d1) . ', now ' . implode(', ', $d2) . ")\n");
9890
}
9991

10092
$this->_previousTrace[$trace] = $bt;
@@ -103,14 +95,12 @@ public function debugTraceChange(string $trace = 'default'): void
10395
/**
10496
* System is unusable.
10597
*
106-
* @param string $message
107-
* @param array<mixed> $context
108-
*
109-
* @return $this
98+
* @param string|\Stringable $message
99+
* @param array<mixed> $context
110100
*/
111-
public function emergency($message, array $context = [])
101+
public function emergency($message, array $context = []): void
112102
{
113-
return $this->log(LogLevel::EMERGENCY, $message, $context);
103+
$this->log(LogLevel::EMERGENCY, $message, $context);
114104
}
115105

116106
/**
@@ -119,43 +109,37 @@ public function emergency($message, array $context = [])
119109
* Example: Entire website down, database unavailable, etc. This should
120110
* trigger the SMS alerts and wake you up.
121111
*
122-
* @param string $message
123-
* @param array<mixed> $context
124-
*
125-
* @return $this
112+
* @param string|\Stringable $message
113+
* @param array<mixed> $context
126114
*/
127-
public function alert($message, array $context = [])
115+
public function alert($message, array $context = []): void
128116
{
129-
return $this->log(LogLevel::ALERT, $message, $context);
117+
$this->log(LogLevel::ALERT, $message, $context);
130118
}
131119

132120
/**
133121
* Critical conditions.
134122
*
135123
* Example: Application component unavailable, unexpected exception.
136124
*
137-
* @param string $message
138-
* @param array<mixed> $context
139-
*
140-
* @return $this
125+
* @param string|\Stringable $message
126+
* @param array<mixed> $context
141127
*/
142-
public function critical($message, array $context = [])
128+
public function critical($message, array $context = []): void
143129
{
144-
return $this->log(LogLevel::CRITICAL, $message, $context);
130+
$this->log(LogLevel::CRITICAL, $message, $context);
145131
}
146132

147133
/**
148134
* Runtime errors that do not require immediate action but should typically
149135
* be logged and monitored.
150136
*
151-
* @param string $message
152-
* @param array<mixed> $context
153-
*
154-
* @return $this
137+
* @param string|\Stringable $message
138+
* @param array<mixed> $context
155139
*/
156-
public function error($message, array $context = [])
140+
public function error($message, array $context = []): void
157141
{
158-
return $this->log(LogLevel::ERROR, $message, $context);
142+
$this->log(LogLevel::ERROR, $message, $context);
159143
}
160144

161145
/**
@@ -164,41 +148,35 @@ public function error($message, array $context = [])
164148
* Example: Use of deprecated APIs, poor use of an API, undesirable things
165149
* that are not necessarily wrong.
166150
*
167-
* @param string $message
168-
* @param array<mixed> $context
169-
*
170-
* @return $this
151+
* @param string|\Stringable $message
152+
* @param array<mixed> $context
171153
*/
172-
public function warning($message, array $context = [])
154+
public function warning($message, array $context = []): void
173155
{
174-
return $this->log(LogLevel::WARNING, $message, $context);
156+
$this->log(LogLevel::WARNING, $message, $context);
175157
}
176158

177159
/**
178160
* Normal but significant events.
179161
*
180-
* @param string $message
181-
* @param array<mixed> $context
182-
*
183-
* @return $this
162+
* @param string|\Stringable $message
163+
* @param array<mixed> $context
184164
*/
185-
public function notice($message, array $context = [])
165+
public function notice($message, array $context = []): void
186166
{
187-
return $this->log(LogLevel::NOTICE, $message, $context);
167+
$this->log(LogLevel::NOTICE, $message, $context);
188168
}
189169

190170
/**
191171
* Interesting events.
192172
*
193173
* Example: User logs in, SQL logs.
194174
*
195-
* @param string $message
196-
* @param array<mixed> $context
197-
*
198-
* @return $this
175+
* @param string|\Stringable $message
176+
* @param array<mixed> $context
199177
*/
200-
public function info($message, array $context = [])
178+
public function info($message, array $context = []): void
201179
{
202-
return $this->log(LogLevel::INFO, $message, $context);
180+
$this->log(LogLevel::INFO, $message, $context);
203181
}
204182
}

tests/DebugTraitTest.php

+11-18
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function testDebug(): void
1616

1717
self::assertFalse($m->debug);
1818

19-
$m->debug();
19+
$m->debug(true);
2020
self::assertTrue($m->debug);
2121

2222
$m->debug(false);
@@ -28,56 +28,53 @@ public function testDebug(): void
2828

2929
public function testDebugOutput(): void
3030
{
31-
$this->expectOutputString("[Atk4\\Core\\Tests\\DebugMock]: debug test1\n");
32-
3331
$m = new DebugMock();
34-
$m->debug();
32+
$m->debug(true);
3533

34+
$this->expectOutputString("[Atk4\\Core\\Tests\\DebugMock]: debug test1\n");
3635
$m->debug('debug test1');
3736
}
3837

3938
public function testDebugNoOutput(): void
4039
{
41-
$this->expectOutputString('');
42-
4340
$m = new DebugMock();
4441

42+
$this->expectOutputString('');
4543
$m->debug('debug test2');
4644
}
4745

4846
public function testDebugApp(): void
4947
{
50-
$this->expectOutputString('');
51-
5248
$app = new DebugAppMock();
5349
$app->logger = $app;
5450

5551
$m = new DebugMock();
5652
$m->setApp($app);
57-
$m->debug();
53+
$m->debug(true);
5854

55+
$this->expectOutputString('');
5956
$m->debug('debug test2');
6057

6158
self::assertSame(['debug', 'debug test2', []], $app->log);
6259
}
6360

6461
public function testLog1(): void
6562
{
66-
$this->expectOutputString("debug test3\n");
67-
6863
$m = new DebugMock();
64+
65+
$this->expectOutputString("debug test3\n");
6966
$m->log('warning', 'debug test3');
7067
}
7168

7269
public function testLog2(): void
7370
{
74-
$this->expectOutputString('');
75-
7671
$app = new DebugAppMock();
7772
$app->logger = $app;
7873

7974
$m = new DebugMock();
8075
$m->setApp($app);
76+
77+
$this->expectOutputString('');
8178
$m->log('warning', 'debug test3');
8279

8380
self::assertSame(['warning', 'debug test3', []], $app->log);
@@ -166,11 +163,7 @@ class DebugAppMock implements \Psr\Log\LoggerInterface
166163
/** @var self */
167164
public $logger;
168165

169-
/**
170-
* @param mixed $level
171-
* @param string $message
172-
*/
173-
public function log($level, $message, array $context = [])
166+
public function log($level, $message, array $context = []): void
174167
{
175168
$this->log = [$level, $message, $context];
176169
}

0 commit comments

Comments
 (0)