File tree Expand file tree Collapse file tree 4 files changed +84
-8
lines changed
Expand file tree Collapse file tree 4 files changed +84
-8
lines changed Original file line number Diff line number Diff line change 22
33namespace Hypernode \DeployConfiguration ;
44
5- use Hypernode \DeployConfiguration \Logging \SimpleLogger ;
5+ use Hypernode \DeployConfiguration \Logging \LoggingFactory ;
66use Psr \Log \LoggerInterface ;
77use Psr \Log \LogLevel ;
88
@@ -157,7 +157,7 @@ class Configuration
157157
158158 public function __construct ()
159159 {
160- $ this ->logger = new SimpleLogger (LogLevel::INFO );
160+ $ this ->logger = LoggingFactory:: create (LogLevel::INFO );
161161 $ this ->setDefaultComposerOptions ();
162162 }
163163
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 11<?php
22
3+ declare (strict_types=1 );
4+
35namespace Hypernode \DeployConfiguration \Logging ;
46
57use Psr \Log \AbstractLogger ;
@@ -32,14 +34,17 @@ public function __construct(string $level)
3234 }
3335
3436 /**
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
3944 */
40- public function log ($ level , $ message , array $ context = array ())
45+ public function log ($ level , $ message , array $ context = []): void
4146 {
42- if ($ this ->mapLevelToNumber ($ level ) ?? 1 >= $ this ->mappedLevel ) {
47+ if ($ this ->mapLevelToNumber ($ level ) >= $ this ->mappedLevel ) {
4348 printf ("%s (%s) \n" , $ message , json_encode ($ context ));
4449 }
4550 }
You can’t perform that action at this time.
0 commit comments