File tree 4 files changed +84
-8
lines changed 4 files changed +84
-8
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace Hypernode \DeployConfiguration ;
4
4
5
- use Hypernode \DeployConfiguration \Logging \SimpleLogger ;
5
+ use Hypernode \DeployConfiguration \Logging \LoggingFactory ;
6
6
use Psr \Log \LoggerInterface ;
7
7
use Psr \Log \LogLevel ;
8
8
@@ -157,7 +157,7 @@ class Configuration
157
157
158
158
public function __construct ()
159
159
{
160
- $ this ->logger = new SimpleLogger (LogLevel::INFO );
160
+ $ this ->logger = LoggingFactory:: create (LogLevel::INFO );
161
161
$ this ->setDefaultComposerOptions ();
162
162
}
163
163
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Hypernode \DeployConfiguration \Logging ;
4
+
5
+ use Psr \Log \AbstractLogger ;
6
+ use Psr \Log \LogLevel ;
7
+
8
+ /**
9
+ * Simple logger implementation using printf
10
+ * This logger was written for psr/log < 3.0.0.
11
+ */
12
+ class LegacyLogger extends AbstractLogger
13
+ {
14
+ private const LEVEL_MAPPING = [
15
+ LogLevel::DEBUG => 0 ,
16
+ LogLevel::INFO => 1 ,
17
+ LogLevel::NOTICE => 2 ,
18
+ LogLevel::WARNING => 3 ,
19
+ LogLevel::ERROR => 4 ,
20
+ LogLevel::CRITICAL => 5 ,
21
+ LogLevel::ALERT => 6 ,
22
+ LogLevel::EMERGENCY => 7
23
+ ];
24
+
25
+ /**
26
+ * @var int
27
+ */
28
+ private $ mappedLevel ;
29
+
30
+ public function __construct (string $ level )
31
+ {
32
+ $ this ->mappedLevel = self ::LEVEL_MAPPING [$ level ] ?? 1 ;
33
+ }
34
+
35
+ /**
36
+ * @param mixed $level
37
+ * @param string|\Stringable $message
38
+ * @param mixed[] $context
39
+ * @return void
40
+ */
41
+ public function log ($ level , $ message , array $ context = array ())
42
+ {
43
+ if ($ this ->mapLevelToNumber ($ level ) ?? 1 >= $ this ->mappedLevel ) {
44
+ printf ("%s (%s) \n" , $ message , json_encode ($ context ));
45
+ }
46
+ }
47
+
48
+ private static function mapLevelToNumber (string $ level ): int
49
+ {
50
+ return self ::LEVEL_MAPPING [$ level ] ?? 1 ;
51
+ }
52
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Hypernode \DeployConfiguration \Logging ;
6
+
7
+ use Composer \InstalledVersions ;
8
+ use Psr \Log \LoggerInterface ;
9
+
10
+ class LoggingFactory
11
+ {
12
+ public static function create (string $ level ): LoggerInterface
13
+ {
14
+ if (version_compare (InstalledVersions::getVersion ('psr/log ' ), '3.0.0 ' , '< ' )) {
15
+ return new LegacyLogger ($ level );
16
+ }
17
+ return new SimpleLogger ($ level );
18
+ }
19
+ }
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
+ declare (strict_types=1 );
4
+
3
5
namespace Hypernode \DeployConfiguration \Logging ;
4
6
5
7
use Psr \Log \AbstractLogger ;
@@ -32,14 +34,17 @@ public function __construct(string $level)
32
34
}
33
35
34
36
/**
35
- * @param mixed $level
36
- * @param string|\Stringable $message
37
- * @param mixed[] $context
38
- * @return void
37
+ * Logs with an arbitrary level.
38
+ *
39
+ * @param mixed $level
40
+ * @param string|\Stringable $message
41
+ * @param mixed[] $context
42
+ *
43
+ * @return void
39
44
*/
40
- public function log ($ level , $ message , array $ context = array ())
45
+ public function log ($ level , $ message , array $ context = []): void
41
46
{
42
- if ($ this ->mapLevelToNumber ($ level ) ?? 1 >= $ this ->mappedLevel ) {
47
+ if ($ this ->mapLevelToNumber ($ level ) >= $ this ->mappedLevel ) {
43
48
printf ("%s (%s) \n" , $ message , json_encode ($ context ));
44
49
}
45
50
}
You can’t perform that action at this time.
0 commit comments