11
11
use Psr \Log \LogLevel ;
12
12
13
13
use function func_get_args ;
14
+ use function MongoDB \Driver \Monitoring \mongoc_log ;
14
15
use function sprintf ;
15
16
16
17
class PsrLogAdapterTest extends BaseTestCase
@@ -20,47 +21,76 @@ class PsrLogAdapterTest extends BaseTestCase
20
21
public function setUp (): void
21
22
{
22
23
$ this ->logger = $ this ->createTestPsrLogger ();
24
+
25
+ PsrLogAdapter::addLogger ($ this ->logger );
23
26
}
24
27
25
28
public function tearDown (): void
26
29
{
27
30
PsrLogAdapter::removeLogger ($ this ->logger );
28
31
}
29
32
33
+ public function testLog (): void
34
+ {
35
+ /* This uses PHPC's internal mongoc_log() function to write messages
36
+ * directly to libmongoc. Those messages are then relayed to
37
+ * PsrLogAdapter and forwarded to each registered PSR logger.
38
+ *
39
+ * Note: it's not possible to test PsrLogAdapter::log() with an invalid
40
+ * level since mongoc_log() already validates its level parameter. */
41
+ mongoc_log (LogSubscriber::LEVEL_ERROR , 'domain1 ' , 'error ' );
42
+ mongoc_log (LogSubscriber::LEVEL_CRITICAL , 'domain2 ' , 'critical ' );
43
+ mongoc_log (LogSubscriber::LEVEL_WARNING , 'domain3 ' , 'warning ' );
44
+ mongoc_log (LogSubscriber::LEVEL_MESSAGE , 'domain4 ' , 'message ' );
45
+ mongoc_log (LogSubscriber::LEVEL_INFO , 'domain5 ' , 'info ' );
46
+ mongoc_log (LogSubscriber::LEVEL_DEBUG , 'domain6 ' , 'debug ' );
47
+
48
+ $ expectedLogs = [
49
+ [LogLevel::ERROR , 'error ' , ['domain ' => 'domain1 ' ]],
50
+ [LogLevel::ERROR , 'critical ' , ['domain ' => 'domain2 ' ]],
51
+ [LogLevel::WARNING , 'warning ' , ['domain ' => 'domain3 ' ]],
52
+ [LogLevel::NOTICE , 'message ' , ['domain ' => 'domain4 ' ]],
53
+ [LogLevel::INFO , 'info ' , ['domain ' => 'domain5 ' ]],
54
+ [LogLevel::DEBUG , 'debug ' , ['domain ' => 'domain6 ' ]],
55
+ ];
56
+
57
+ $ this ->assertSame ($ this ->logger ->logs , $ expectedLogs );
58
+ }
59
+
30
60
/**
31
61
* @testWith [-1]
32
- * [6 ]
62
+ * [9 ]
33
63
*/
34
64
public function testWriteLogWithInvalidLevel (int $ level ): void
35
65
{
36
66
$ this ->expectException (UnexpectedValueException::class);
37
- $ this ->expectExceptionMessage (sprintf ('Expected level to be >= 0 and <= 5 , %d given for domain "domain" and message: message ' , $ level ));
67
+ $ this ->expectExceptionMessage (sprintf ('Expected level to be >= 0 and <= 8 , %d given for domain "domain" and message: message ' , $ level ));
38
68
39
69
PsrLogAdapter::writeLog ($ level , 'domain ' , 'message ' );
40
70
}
41
71
42
- public function testWriteLogMapsDriverLogLevelsToPsrLogLevels (): void
72
+ public function testWriteLog (): void
43
73
{
44
- PsrLogAdapter::addLogger ($ this ->logger );
45
-
46
- $ domain = 'domain ' ;
47
-
48
- PsrLogAdapter::writeLog (LogSubscriber::LEVEL_ERROR , $ domain , 'error ' );
49
- PsrLogAdapter::writeLog (LogSubscriber::LEVEL_CRITICAL , $ domain , 'critical ' );
50
- PsrLogAdapter::writeLog (LogSubscriber::LEVEL_WARNING , $ domain , 'warning ' );
51
- PsrLogAdapter::writeLog (LogSubscriber::LEVEL_MESSAGE , $ domain , 'message ' );
52
- PsrLogAdapter::writeLog (LogSubscriber::LEVEL_INFO , $ domain , 'info ' );
53
- PsrLogAdapter::writeLog (LogSubscriber::LEVEL_DEBUG , $ domain , 'debug ' );
54
-
55
- $ context = ['domain ' => $ domain ];
74
+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_EMERGENCY , 'domain1 ' , 'emergency ' );
75
+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_ALERT , 'domain2 ' , 'alert ' );
76
+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_CRITICAL , 'domain3 ' , 'critical ' );
77
+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_ERROR , 'domain4 ' , 'error ' );
78
+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_WARN , 'domain5 ' , 'warn ' );
79
+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_NOTICE , 'domain6 ' , 'notice ' );
80
+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_INFO , 'domain7 ' , 'info ' );
81
+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_DEBUG , 'domain8 ' , 'debug ' );
82
+ PsrLogAdapter::writeLog (PsrLogAdapter::LEVEL_TRACE , 'domain9 ' , 'trace ' );
56
83
57
84
$ expectedLogs = [
58
- [LogLevel::ERROR , 'error ' , $ context ],
59
- [LogLevel::ERROR , 'critical ' , $ context ],
60
- [LogLevel::WARNING , 'warning ' , $ context ],
61
- [LogLevel::NOTICE , 'message ' , $ context ],
62
- [LogLevel::INFO , 'info ' , $ context ],
63
- [LogLevel::DEBUG , 'debug ' , $ context ],
85
+ [LogLevel::EMERGENCY , 'emergency ' , ['domain ' => 'domain1 ' ]],
86
+ [LogLevel::ALERT , 'alert ' , ['domain ' => 'domain2 ' ]],
87
+ [LogLevel::CRITICAL , 'critical ' , ['domain ' => 'domain3 ' ]],
88
+ [LogLevel::ERROR , 'error ' , ['domain ' => 'domain4 ' ]],
89
+ [LogLevel::WARNING , 'warn ' , ['domain ' => 'domain5 ' ]],
90
+ [LogLevel::NOTICE , 'notice ' , ['domain ' => 'domain6 ' ]],
91
+ [LogLevel::INFO , 'info ' , ['domain ' => 'domain7 ' ]],
92
+ [LogLevel::DEBUG , 'debug ' , ['domain ' => 'domain8 ' ]],
93
+ [LogLevel::DEBUG , 'trace ' , ['domain ' => 'domain9 ' ]],
64
94
];
65
95
66
96
$ this ->assertSame ($ this ->logger ->logs , $ expectedLogs );
0 commit comments