Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Hypernode\DeployConfiguration;

use Hypernode\DeployConfiguration\Logging\SimpleLogger;
use Hypernode\DeployConfiguration\Logging\LoggingFactory;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

Expand Down Expand Up @@ -157,7 +157,7 @@ class Configuration

public function __construct()
{
$this->logger = new SimpleLogger(LogLevel::INFO);
$this->logger = LoggingFactory::create(LogLevel::INFO);
$this->setDefaultComposerOptions();
}

Expand Down
52 changes: 52 additions & 0 deletions src/Logging/LegacyLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Hypernode\DeployConfiguration\Logging;

use Psr\Log\AbstractLogger;
use Psr\Log\LogLevel;

/**
* Simple logger implementation using printf
* This logger was written for psr/log < 3.0.0.
*/
class LegacyLogger extends AbstractLogger
{
private const LEVEL_MAPPING = [
LogLevel::DEBUG => 0,
LogLevel::INFO => 1,
LogLevel::NOTICE => 2,
LogLevel::WARNING => 3,
LogLevel::ERROR => 4,
LogLevel::CRITICAL => 5,
LogLevel::ALERT => 6,
LogLevel::EMERGENCY => 7
];

/**
* @var int
*/
private $mappedLevel;

public function __construct(string $level)
{
$this->mappedLevel = self::LEVEL_MAPPING[$level] ?? 1;
}

/**
* @param mixed $level
* @param string|\Stringable $message
* @param mixed[] $context
* @return void
*/
public function log($level, $message, array $context = array())
{
if ($this->mapLevelToNumber($level) ?? 1 >= $this->mappedLevel) {
printf("%s (%s)\n", $message, json_encode($context));
}
}

private static function mapLevelToNumber(string $level): int
{
return self::LEVEL_MAPPING[$level] ?? 1;
}
}
19 changes: 19 additions & 0 deletions src/Logging/LoggingFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Hypernode\DeployConfiguration\Logging;

use Composer\InstalledVersions;
use Psr\Log\LoggerInterface;

class LoggingFactory
{
public static function create(string $level): LoggerInterface
{
if (version_compare(InstalledVersions::getVersion('psr/log'), '3.0.0', '<')) {
return new LegacyLogger($level);
}
return new SimpleLogger($level);
}
}
17 changes: 11 additions & 6 deletions src/Logging/SimpleLogger.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Hypernode\DeployConfiguration\Logging;

use Psr\Log\AbstractLogger;
Expand Down Expand Up @@ -32,14 +34,17 @@ public function __construct(string $level)
}

/**
* @param mixed $level
* @param string|\Stringable $message
* @param mixed[] $context
* @return void
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string|\Stringable $message
* @param mixed[] $context
*
* @return void
*/
public function log($level, $message, array $context = array())
public function log($level, $message, array $context = []): void
{
if ($this->mapLevelToNumber($level) ?? 1 >= $this->mappedLevel) {
if ($this->mapLevelToNumber($level) >= $this->mappedLevel) {
printf("%s (%s)\n", $message, json_encode($context));
}
}
Expand Down