Skip to content

Commit 8d2926a

Browse files
committed
Add simple logger as fallback logger implementation
1 parent 60fa970 commit 8d2926a

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Hypernode\DeployConfiguration\Command\Command;
66
use Hypernode\DeployConfiguration\Command\DeployCommand;
7+
use Hypernode\DeployConfiguration\Logging\SimpleLogger;
78
use Psr\Log\LoggerInterface;
9+
use Psr\Log\LogLevel;
810

911
class Configuration
1012
{
@@ -147,6 +149,7 @@ class Configuration
147149
public function __construct(string $gitRepository)
148150
{
149151
$this->gitRepository = $gitRepository;
152+
$this->logger = new SimpleLogger(LogLevel::INFO);
150153
}
151154

152155
public function getGitRepository(): string

src/Logging/SimpleLogger.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
*/
11+
class SimpleLogger extends AbstractLogger
12+
{
13+
private const LEVEL_MAPPING = [
14+
LogLevel::DEBUG => 0,
15+
LogLevel::INFO => 1,
16+
LogLevel::NOTICE => 2,
17+
LogLevel::WARNING => 3,
18+
LogLevel::ERROR => 4,
19+
LogLevel::CRITICAL => 5,
20+
LogLevel::ALERT => 6,
21+
LogLevel::EMERGENCY => 7
22+
];
23+
24+
/**
25+
* @var int
26+
*/
27+
private $mappedLevel;
28+
29+
public function __construct(string $level)
30+
{
31+
$this->mappedLevel = self::LEVEL_MAPPING[$level] ?? 1;
32+
}
33+
34+
/**
35+
* @param mixed $level
36+
* @param string|\Stringable $message
37+
* @param mixed[] $context
38+
* @return void
39+
*/
40+
public function log($level, $message, array $context = array())
41+
{
42+
if ($this->mapLevelToNumber($level) ?? 1 >= $this->mappedLevel) {
43+
printf("%s (%s)\n", $message, json_encode($context));
44+
}
45+
}
46+
47+
private static function mapLevelToNumber(string $level): int
48+
{
49+
return self::LEVEL_MAPPING[$level] ?? 1;
50+
}
51+
}

0 commit comments

Comments
 (0)