Skip to content

Commit

Permalink
Implemented Psr Logger (#81)
Browse files Browse the repository at this point in the history
* Implemented Psr Logger
* composer require psr/log
* Invalid Items count on step (#80)
* log: show invalid items as warning
* cleanup

---------

Co-authored-by: Thijs De Paepe <[email protected]>
  • Loading branch information
J0rdyV and Thijzer authored Jun 4, 2024
1 parent d66de51 commit 6c3a650
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 15 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"beberlei/assert": "^3.3",
"adhocore/cli": "^0.9.0",
"firebase/php-jwt": "^v6.8",
"aspera/xlsx-reader": "^1.1.0"
"aspera/xlsx-reader": "^1.1.0",
"psr/log": "^3.0"
},
"require-dev": {
"roave/security-advisories": "dev-master",
Expand Down
2 changes: 2 additions & 0 deletions src/Command/TransformationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Assert\Assertion;
use Misery\Component\Common\FileManager\LocalFileManager;
use Misery\Component\Common\Functions\ArrayFunctions;
use Misery\Component\Logger\OutputLogger;
use Misery\Component\Process\ProcessManager;
use Symfony\Component\Yaml\Yaml;

Expand Down Expand Up @@ -70,6 +71,7 @@ public function execute(string $file, string $source, string $workpath, bool $de
$source ? new LocalFileManager($source): null,
$addSource ? new LocalFileManager($addSource): null,
$extensions ? new LocalFileManager($extensions): null,
new OutputLogger()
);

$transformationFile = ArrayFunctions::array_filter_recursive(Yaml::parseFile($file), function ($value) {
Expand Down
7 changes: 6 additions & 1 deletion src/Component/Common/Pipeline/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace Misery\Component\Common\Pipeline;

use Psr\Log\LoggerAwareTrait;
use Misery\Component\Common\Pipeline\Exception\InvalidItemException;
use Misery\Component\Common\Pipeline\Exception\SkipPipeLineException;
use Misery\Component\Debugger\ItemDebugger;
use Misery\Component\Debugger\NullItemDebugger;

class Pipeline
{
use LoggerAwareTrait;

/** @var PipeReaderInterface */
private $in;
/** @var PipeWriterInterface */
Expand Down Expand Up @@ -74,7 +77,7 @@ public function run(int $amount = -1, int $lineNumber = -1)
}
} catch (SkipPipeLineException $exception) {
if (!empty($exception->getMessage())) {
echo sprintf('Skipped: %s', $exception->getMessage()) . PHP_EOL;
$this->logger->info(sprintf('Skipped: %s', $exception->getMessage()));
}
continue;
} catch (InvalidItemException $exception) {
Expand All @@ -83,6 +86,7 @@ public function run(int $amount = -1, int $lineNumber = -1)
'msg' => $exception->getMessage(),
'item' => json_encode($exception->getInvalidItem()),
]);
$this->logger->error($exception->getMessage());
continue;
}
if ($i === $lineNumber) {
Expand All @@ -105,6 +109,7 @@ public function run(int $amount = -1, int $lineNumber = -1)
'msg' => $exception->getMessage(),
'item' => json_encode($exception->getInvalidItem()),
]);
$this->logger->error($exception->getMessage());
continue;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Component/Configurator/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Misery\Component\Configurator;

use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Misery\Component\Action\ItemActionProcessor;
use Misery\Component\BluePrint\BluePrint;
use Misery\Component\Common\Client\ApiClient;
Expand All @@ -22,6 +24,7 @@

class Configuration
{
use LoggerAwareTrait;
private $pipeline = null;
private $actions = null;
private $groupedActions = null;
Expand Down Expand Up @@ -251,6 +254,11 @@ public function getWriter(): ?ItemWriterInterface
return $this->writer;
}

public function getLogger(): LoggerInterface
{
return $this->logger;
}

public function setReader($reader): void
{
if ($reader instanceof ReaderInterface) {
Expand Down
6 changes: 4 additions & 2 deletions src/Component/Configurator/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Misery\Component\Configurator;

use Psr\Log\LoggerInterface;
use Misery\Component\Common\FileManager\LocalFileManager;
use Misery\Component\Common\Pipeline\ActionPipe;
use Misery\Component\Common\Registry\Registry;
Expand Down Expand Up @@ -31,8 +32,10 @@ public function init(
LocalFileManager $source = null,
LocalFileManager $additionalSources = null,
LocalFileManager $extensions = null,
LoggerInterface $logger
) {
$this->config = new Configuration();
$this->config->setLogger($logger);
$sources = ($source) ? $this->getFactory('source')->createFromFileManager($source) : null;
$this->manager = new ConfigurationManager(
$this->config,
Expand Down Expand Up @@ -98,8 +101,7 @@ public function parseDirectivesFromConfiguration(array $configuration): Configur
break;
case $key === 'transformation_steps';
$this->config->setAsMultiStep();
// @todo move this to a dedicated logger
echo sprintf("Multi Step [%s]", basename($this->config->getContext('transformation_file'))). PHP_EOL;
$this->config->getLogger()->info(sprintf("Multi Step [%s]", basename($this->config->getContext('transformation_file'))));
$this->manager->addTransformationSteps($configuration['transformation_steps'], $configuration);
break;
case $key === 'pipeline';
Expand Down
53 changes: 53 additions & 0 deletions src/Component/Logger/OutputLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Misery\Component\Logger;

use Psr\Log\LoggerInterface;

class OutputLogger implements LoggerInterface
{
public function emergency($message, array $context = []): void
{
$this->log('emergency', $message, $context);
}

public function alert($message, array $context = []): void
{
$this->log('alert', $message, $context);
}

public function critical($message, array $context = []): void
{
$this->log('critical', $message, $context);
}

public function error($message, array $context = []): void
{
$this->log('error', $message, $context);
}

public function warning($message, array $context = []): void
{
$this->log('warning', $message, $context);
}

public function notice($message, array $context = []): void
{
$this->log('notice', $message, $context);
}

public function info($message, array $context = []): void
{
$this->log('info', $message, $context);
}

public function debug($message, array $context = []): void
{
$this->log('debug', $message, $context);
}

public function log($level, $message, array $context = []): void
{
echo $message . PHP_EOL;
}
}
51 changes: 40 additions & 11 deletions src/Component/Process/ProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@

use Misery\Component\Common\Pipeline\LoggingPipe;
use Misery\Component\Configurator\Configuration;
use Psr\Log\LoggerInterface;

class ProcessManager
{
private Configuration $configuration;
private ?int $startTimeStamp = null;
private ?int $invalidItems = 0;
private LoggerInterface $logger;

public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;
}

private function log(string $message)
{
echo $message . PHP_EOL;
$this->logger = $this->configuration->getLogger();
}

public function startProcess(): void
Expand All @@ -34,7 +33,11 @@ public function startProcess(): void
$amount = -1;
}

$path = $this->configuration->getContext('workpath').'/invalid_items.csv';
$this->invalidItems = $this->getLines($path);

if ($pipeline = $this->configuration->getPipeline()) {
$pipeline->setLogger($this->logger);
if ($debug === true) {
if ($mappings === true) {
dump($this->configuration->getMappings());
Expand Down Expand Up @@ -67,11 +70,37 @@ public function stopProcess(): void
$executionTime = round($stopTimeStamp - $this->startTimeStamp, 1);
$executionTime = "Execution Time: {$executionTime}s";

$this->log(sprintf(
"Finished Step :: %s (%s, %s)",
basename($this->configuration->getContext('transformation_file')),
$usage,
$executionTime
));
$path = $this->configuration->getContext('workpath').'/invalid_items.csv';
$this->invalidItems = $this->getLines($path) - $this->invalidItems;
$invalidItems = "Invalid Items: $this->invalidItems";

if ($this->invalidItems > 0) {
$this->logger->warning(sprintf(
"Finished Step :: %s (%s, %s, %s)",
basename($this->configuration->getContext('transformation_file')),
$usage,
$executionTime,
$invalidItems
));
} else {
$this->logger->info(sprintf(
"Finished Step :: %s (%s, %s, %s)",
basename($this->configuration->getContext('transformation_file')),
$usage,
$executionTime,
$invalidItems
));
}
}

private function getLines($file): int
{
$f = fopen($file, 'rb');
$lines = 0;
while (!feof($f)) {
$lines += substr_count(fread($f, 8192), "\n");
}
fclose($f);
return $lines;
}
}

0 comments on commit 6c3a650

Please sign in to comment.