Skip to content

Commit

Permalink
fix(logger): fix logger
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 13, 2023
1 parent ffb2732 commit 3ef0b51
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 71 deletions.
4 changes: 2 additions & 2 deletions config/pdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
use MyParcelNL\PrestaShop\Pdk\DeliveryOptions\Service\PsDeliveryOptionsService;
use MyParcelNL\PrestaShop\Pdk\Frontend\Service\PsFrontendRenderService;
use MyParcelNL\PrestaShop\Pdk\Language\Service\LanguageService;
use MyParcelNL\PrestaShop\Pdk\Logger\PdkLogger;
use MyParcelNL\PrestaShop\Pdk\Logger\PsLogger;
use MyParcelNL\PrestaShop\Pdk\Order\Repository\PsPdkOrderRepository;
use MyParcelNL\PrestaShop\Pdk\Plugin\Api\PsBackendEndpointService;
use MyParcelNL\PrestaShop\Pdk\Plugin\Repository\PdkAccountRepository;
Expand Down Expand Up @@ -108,7 +108,7 @@
*/
ClientAdapterInterface::class => get(Guzzle7ClientAdapter::class),
InstallerServiceInterface::class => get(PsInstallerService::class),
LoggerInterface::class => get(PdkLogger::class),
LoggerInterface::class => get(PsLogger::class),
MigrationServiceInterface::class => get(PsMigrationService::class),
ScriptServiceInterface::class => get(PsScriptService::class),

Expand Down
2 changes: 2 additions & 0 deletions src/Pdk/Base/PsPdkBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ protected function getAdditionalConfig(
'prestaShopVersionMin' => value('1.7.6'),
'prestaShopVersionMax' => value('8.2.0'),

'logDirectory' => value(sprintf('%s/var/logs/%s', _PS_ROOT_DIR_, $name)),

'MyParcelCarrierName' => value('MyParcel Carrier'),

/**
Expand Down
25 changes: 0 additions & 25 deletions src/Pdk/Logger/OrderLogger.php

This file was deleted.

96 changes: 52 additions & 44 deletions src/Pdk/Logger/PdkLogger.php → src/Pdk/Logger/PsLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

use FileLogger;
use InvalidArgumentException;
use MyParcelNL\Pdk\Base\FileSystemInterface;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Pdk\Logger\AbstractLogger;
use MyParcelNL\Sdk\src\Support\Arr;
use MyParcelNL\Sdk\src\Support\Str;
use Psr\Log\LogLevel;
use RuntimeException;
use Throwable;

class PdkLogger extends AbstractLogger
final class PsLogger extends AbstractLogger
{
/**
* Log levels, in order of severity.
Expand Down Expand Up @@ -48,25 +48,43 @@ class PdkLogger extends AbstractLogger
*/
private static $loggers = [];

public function __construct()
/**
* @var \MyParcelNL\Pdk\Base\FileSystemInterface
*/
private $fileSystem;

public function __construct(FileSystemInterface $fileSystem)
{
$directory = $this->getLogDirectory();
$this->fileSystem = $fileSystem;

$this->createLogDirectory($directory);
$this->createLogDirectory();
}

/**
* @param string $directory
* @param string $level
* @param \Throwable|array|string $message
* @param array $context
*
* @return void
*/
public function createLogDirectory(string $directory): void
public function log($level, $message, array $context = []): void
{
if (! is_dir($directory) && ! mkdir($directory) && ! is_dir($directory)) {
throw new RuntimeException("Directory \"$directory\" was not created");
if (! is_string($level) || ! in_array($level, self::LOG_LEVELS, true)) {
throw new InvalidArgumentException(sprintf('Invalid log level "%s"', $level));
}

if (! _PS_MODE_DEV_) {
$logger = $this->getLogger($level);
$string = $this->createMessage($message, $context, $level);

$logger->log($string, $this->mapLevel($level));
}

/**
* @return void
*/
private function createLogDirectory(): void
{
if (Pdk::isProduction()) {
return;
}

Expand All @@ -77,22 +95,21 @@ public function createLogDirectory(string $directory): void
}

/**
* @param string $level
* @param \Throwable|array|string $message
* @param array $context
* @param string $level
*
* @return void
*/
public function log($level, $message, array $context = []): void
private function createLogFile(string $level): void
{
if (! is_string($level) || ! in_array($level, self::LOG_LEVELS, true)) {
throw new InvalidArgumentException(sprintf('Invalid log level "%s"', $level));
}
$file = $this->getLogFilename($level);

$logger = $this->getLogger($level);
$string = $this->createMessage($message, $context, $level);
if (! $this->fileSystem->isDir(Pdk::get('logDirectory'))) {
$this->fileSystem->mkdir(Pdk::get('logDirectory'), true);
}

$logger->log($string, self::LEVEL_MAP[$level]);
if (! $this->fileSystem->fileExists($file)) {
$this->fileSystem->put($file, '');
}
}

/**
Expand All @@ -102,7 +119,7 @@ public function log($level, $message, array $context = []): void
*
* @return void
*/
protected function createMessage($message, array $context, string $level): string
private function createMessage($message, array $context, string $level): string
{
$output = $this->getOutput($message);
$logContext = Arr::except($context, 'exception');
Expand All @@ -118,20 +135,6 @@ protected function createMessage($message, array $context, string $level): strin
return $output;
}

/**
* @param string $level
*
* @return void
*/
private function createLogFile(string $level): void
{
$file = $this->getLogFilename($level);

if (! file_exists($file)) {
touch($file);
}
}

/**
* Get the first caller that's not a *Logger.php file.
*
Expand All @@ -158,19 +161,14 @@ static function ($item) {
return $caller;
}

private function getLogDirectory(): string
{
return sprintf('%s/var/logs/%s', _PS_ROOT_DIR_, Pdk::get('appInfo')['name']);
}

/**
* @param $level
* @param string $level
*
* @return string
*/
private function getLogFilename($level): string
private function getLogFilename(string $level): string
{
return sprintf("%s/%s.log", $this->getLogDirectory(), $level);
return sprintf('%s/%s.log', Pdk::get('logDirectory'), $level);
}

/**
Expand Down Expand Up @@ -243,9 +241,19 @@ private function initializeLogger(string $level): FileLogger
{
$this->createLogFile($level);

$logger = new FileLogger($level);
$logger = new FileLogger($this->mapLevel($level));
$logger->setFilename($this->getLogFilename($level));

return $logger;
}

/**
* @param string $level
*
* @return int
*/
private function mapLevel(string $level): int
{
return self::LEVEL_MAP[$level];
}
}

0 comments on commit 3ef0b51

Please sign in to comment.