Skip to content

Commit 2f1f715

Browse files
authored
Merge pull request #36 from ByteInternet/psr_log_3x
2 parents e4cfd1a + 2a5076f commit 2f1f715

File tree

4 files changed

+84
-8
lines changed

4 files changed

+84
-8
lines changed

src/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Hypernode\DeployConfiguration;
44

5-
use Hypernode\DeployConfiguration\Logging\SimpleLogger;
5+
use Hypernode\DeployConfiguration\Logging\LoggingFactory;
66
use Psr\Log\LoggerInterface;
77
use 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

src/Logging/LegacyLogger.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

src/Logging/LoggingFactory.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

src/Logging/SimpleLogger.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Hypernode\DeployConfiguration\Logging;
46

57
use 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
}

0 commit comments

Comments
 (0)