Commit 40f05dc8 authored by Simon Gadient's avatar Simon Gadient

[FEATURE] Logging

refs KIME-4583
parent 4269a602
...@@ -13,7 +13,6 @@ namespace WE\SpreadsheetImport\Command; ...@@ -13,7 +13,6 @@ namespace WE\SpreadsheetImport\Command;
use TYPO3\Flow\Annotations as Flow; use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Cli\CommandController; use TYPO3\Flow\Cli\CommandController;
use TYPO3\Flow\Exception;
use WE\SpreadsheetImport\Domain\Model\SpreadsheetImport; use WE\SpreadsheetImport\Domain\Model\SpreadsheetImport;
/** /**
...@@ -33,6 +32,12 @@ class SpreadsheetImportCommandController extends CommandController { ...@@ -33,6 +32,12 @@ class SpreadsheetImportCommandController extends CommandController {
*/ */
protected $spreadsheetImportService; protected $spreadsheetImportService;
/**
* @Flow\Inject
* @var \WE\SpreadsheetImport\Log\SpreadsheetImportLoggerInterface
*/
protected $logger;
/** /**
* @Flow\Inject * @Flow\Inject
* @var \TYPO3\Flow\Persistence\PersistenceManagerInterface * @var \TYPO3\Flow\Persistence\PersistenceManagerInterface
...@@ -57,6 +62,8 @@ class SpreadsheetImportCommandController extends CommandController { ...@@ -57,6 +62,8 @@ class SpreadsheetImportCommandController extends CommandController {
/** @var SpreadsheetImport $spreadsheetImport */ /** @var SpreadsheetImport $spreadsheetImport */
$spreadsheetImport = $this->spreadsheetImportRepository->findNextInQueue(); $spreadsheetImport = $this->spreadsheetImportRepository->findNextInQueue();
if ($spreadsheetImport instanceof SpreadsheetImport) { if ($spreadsheetImport instanceof SpreadsheetImport) {
$message = 'Spreadsheet import started.';
$this->log($spreadsheetImport, $message, LOG_INFO);
// mark importing status as "Progressing" before continuing the importing // mark importing status as "Progressing" before continuing the importing
$spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_IN_PROGRESS); $spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_IN_PROGRESS);
$this->spreadsheetImportRepository->update($spreadsheetImport); $this->spreadsheetImportRepository->update($spreadsheetImport);
...@@ -67,16 +74,19 @@ class SpreadsheetImportCommandController extends CommandController { ...@@ -67,16 +74,19 @@ class SpreadsheetImportCommandController extends CommandController {
try { try {
$this->spreadsheetImportService->import(); $this->spreadsheetImportService->import();
$spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_COMPLETED); $spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_COMPLETED);
$this->outputLine('Spreadsheet has been imported. %d inserted, %d updated, %d deleted, %d skipped', $args = array($spreadsheetImport->getTotalInserted(), $spreadsheetImport->getTotalUpdated(), $spreadsheetImport->getTotalDeleted(), $spreadsheetImport->getTotalSkipped());
array($spreadsheetImport->getTotalInserted(), $spreadsheetImport->getTotalUpdated(), $spreadsheetImport->getTotalDeleted(), $spreadsheetImport->getTotalSkipped())); $message = vsprintf('Spreadsheet import complete: %d inserted, %d updated, %d deleted, %d skipped', $args);
$this->log($spreadsheetImport, $message, LOG_INFO);
} catch (\Exception $e) { } catch (\Exception $e) {
$spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_FAILED); $spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_FAILED);
$this->outputLine('Spreadsheet import failed.'); $message = 'Spreadsheet import failed.';
$this->log($spreadsheetImport, $message, LOG_ERR, $e->getMessage());
} }
try { try {
$this->spreadsheetImportRepository->update($spreadsheetImport); $this->spreadsheetImportRepository->update($spreadsheetImport);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->outputLine('Spreadsheet import status update error. It remains in progress until cleanup.'); $message = 'Spreadsheet import status update error. It remains in progress until cleanup.';
$this->log($spreadsheetImport, $message, LOG_ERR, $e->getMessage());
} }
} else { } else {
$this->outputFormatted('No spreadsheet import in queue.'); $this->outputFormatted('No spreadsheet import in queue.');
...@@ -108,8 +118,9 @@ class SpreadsheetImportCommandController extends CommandController { ...@@ -108,8 +118,9 @@ class SpreadsheetImportCommandController extends CommandController {
/** @var SpreadsheetImport $spreadsheetImport */ /** @var SpreadsheetImport $spreadsheetImport */
foreach ($spreadsheetImports as $spreadsheetImport) { foreach ($spreadsheetImports as $spreadsheetImport) {
$this->spreadsheetImportRepository->remove($spreadsheetImport); $this->spreadsheetImportRepository->remove($spreadsheetImport);
$message = 'Spreadsheet import removed.';
$this->log($spreadsheetImport, $message, LOG_INFO, NULL);
} }
$this->outputLine('%d spreadsheet imports removed.', array($spreadsheetImports->count()));
} }
/** /**
...@@ -125,7 +136,21 @@ class SpreadsheetImportCommandController extends CommandController { ...@@ -125,7 +136,21 @@ class SpreadsheetImportCommandController extends CommandController {
foreach ($spreadsheetImports as $spreadsheetImport) { foreach ($spreadsheetImports as $spreadsheetImport) {
$spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_FAILED); $spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_FAILED);
$this->spreadsheetImportRepository->update($spreadsheetImport); $this->spreadsheetImportRepository->update($spreadsheetImport);
$message = 'Spreadsheet import exceeded max execution threashold. Status set to failed.';
$this->log($spreadsheetImport, $message, LOG_NOTICE);
} }
$this->outputLine('%d spreadsheet imports set to failed.', array($spreadsheetImports->count())); }
/**
* @param SpreadsheetImport $spreadsheetImport
* @param string $message
* @param int $severity
* @param null $additionalData
*/
private function log(SpreadsheetImport $spreadsheetImport, $message, $severity = LOG_INFO, $additionalData = NULL) {
$name = ucfirst($spreadsheetImport->getContext());
$message = vsprintf('[%s] ' . $message, array($name));
$this->logger->log($message, $severity, $additionalData, 'SpreadsheetImport');
$this->outputLine($message);
} }
} }
<?php
namespace WE\SpreadsheetImport\Log;
/* *
* This script belongs to the Flow package "SpreadsheetImport". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
/**
* Logger interface for the SpreadsheetImport package
*/
interface SpreadsheetImportLoggerInterface extends \TYPO3\Flow\Log\LoggerInterface {
}
...@@ -45,6 +45,12 @@ class SpreadsheetImportService { ...@@ -45,6 +45,12 @@ class SpreadsheetImportService {
*/ */
protected $settings; protected $settings;
/**
* @Flow\Inject
* @var \WE\SpreadsheetImport\Log\SpreadsheetImportLoggerInterface
*/
protected $logger;
/** /**
* @Flow\Inject * @Flow\Inject
* @var \TYPO3\Flow\Reflection\ReflectionService * @var \TYPO3\Flow\Reflection\ReflectionService
...@@ -212,19 +218,24 @@ class SpreadsheetImportService { ...@@ -212,19 +218,24 @@ class SpreadsheetImportService {
$totalCount = 0; $totalCount = 0;
/** @var \PHPExcel_Worksheet_Row $row */ /** @var \PHPExcel_Worksheet_Row $row */
foreach ($sheet->getRowIterator(2) as $row) { foreach ($sheet->getRowIterator(2) as $row) {
$recordNumber = $row->getRowIndex() - 1;
$totalCount++; $totalCount++;
$object = $this->findExistingObjectByRow($row); $object = $this->findExistingObjectByRow($row);
if (is_object($object)) { if (is_object($object)) {
$processedObjectIds[] = $this->persistenceManager->getIdentifierByObject($object); $id = $this->persistenceManager->getIdentifierByObject($object);
$processedObjectIds[] = $id;
if ($this->spreadsheetImport->isUpdating()) { if ($this->spreadsheetImport->isUpdating()) {
$this->setObjectPropertiesByRow($object, $row); $this->setObjectPropertiesByRow($object, $row);
$validationResult = $objectValidator->validate($object); $validationResult = $objectValidator->validate($object);
if ($validationResult->hasErrors()) { if ($validationResult->hasErrors()) {
$this->log(vsprintf('Object %s for record %d skipped. Update object validation failed.', array($id, $recordNumber)), LOG_INFO);
continue; continue;
} }
$objectRepository->update($object); $objectRepository->update($object);
$this->log(vsprintf('Object %s for record %d updated.', array($id, $recordNumber)), LOG_INFO);
$totalUpdated++; $totalUpdated++;
} else { } else {
$this->log(vsprintf('Object %s for record %d skipped. Updating is disabled.', array($id, $recordNumber)), LOG_INFO);
continue; continue;
} }
} elseif ($this->spreadsheetImport->isInserting()) { } elseif ($this->spreadsheetImport->isInserting()) {
...@@ -232,24 +243,29 @@ class SpreadsheetImportService { ...@@ -232,24 +243,29 @@ class SpreadsheetImportService {
$this->setObjectPropertiesByRow($newObject, $row); $this->setObjectPropertiesByRow($newObject, $row);
$validationResult = $objectValidator->validate($newObject); $validationResult = $objectValidator->validate($newObject);
if ($validationResult->hasErrors()) { if ($validationResult->hasErrors()) {
$this->log(vsprintf('Record %d skipped. Insert object validation failed.', array($recordNumber)), LOG_INFO);
continue; continue;
} }
$objectRepository->add($newObject); $objectRepository->add($newObject);
$processedObjectIds[] = $this->persistenceManager->getIdentifierByObject($newObject); $id = $this->persistenceManager->getIdentifierByObject($object);
$processedObjectIds[] = $id;
$this->log(vsprintf('Object %s for record %d inserted.', array($id, $recordNumber)), LOG_INFO);
$totalInserted++; $totalInserted++;
} else { } else {
$this->log(vsprintf('Record %d skipped. Inserting is disabled.', array($recordNumber)), LOG_INFO);
continue; continue;
} }
if ($totalCount % $persistRecordsChunkSize === 0) { if ($totalCount % $persistRecordsChunkSize === 0) {
$this->persistenceManager->persistAll(); $this->persistenceManager->persistAll();
} }
} }
$deleteCount = 0;
if ($this->spreadsheetImport->isDeleting()) { if ($this->spreadsheetImport->isDeleting()) {
$notExistingObjects = $this->findObjectsByArgumentsAndExcludedIds($processedObjectIds); $notExistingObjects = $this->findObjectsByArgumentsAndExcludedIds($processedObjectIds);
foreach ($notExistingObjects as $object) { foreach ($notExistingObjects as $object) {
$id = $this->persistenceManager->getIdentifierByObject($object);
$this->log(vsprintf('Object %s deleted.', array($id)), LOG_INFO);
$objectRepository->remove($object); $objectRepository->remove($object);
if (++$deleteCount % $persistRecordsChunkSize === 0) { if (++$totalDeleted % $persistRecordsChunkSize === 0) {
$this->persistenceManager->persistAll(); $this->persistenceManager->persistAll();
} }
} }
...@@ -358,12 +374,12 @@ class SpreadsheetImportService { ...@@ -358,12 +374,12 @@ class SpreadsheetImportService {
* @param \PHPExcel_Worksheet_Row $row * @param \PHPExcel_Worksheet_Row $row
*/ */
private function setObjectMappingProperties($object, $row) { private function setObjectMappingProperties($object, $row) {
$inversedMappingProperties = $this->getInverseMappingProperties(); $inverseMappingProperties = $this->getInverseMappingProperties();
/** @var \PHPExcel_Cell $cell */ /** @var \PHPExcel_Cell $cell */
foreach ($row->getCellIterator() as $cell) { foreach ($row->getCellIterator() as $cell) {
$column = $cell->getColumn(); $column = $cell->getColumn();
if (array_key_exists($column, $inversedMappingProperties)) { if (array_key_exists($column, $inverseMappingProperties)) {
$properties = $inversedMappingProperties[$column]; $properties = $inverseMappingProperties[$column];
foreach ($properties as $propertyMapping) { foreach ($properties as $propertyMapping) {
$property = $propertyMapping['property']; $property = $propertyMapping['property'];
/** @var Mapping $mapping */ /** @var Mapping $mapping */
...@@ -457,4 +473,15 @@ class SpreadsheetImportService { ...@@ -457,4 +473,15 @@ class SpreadsheetImportService {
$repository = $this->objectManager->get($repositoryClassName); $repository = $this->objectManager->get($repositoryClassName);
return $repository; return $repository;
} }
/**
* @param string $message
* @param int $severity
* @param null $additionalData
*/
private function log($message, $severity = LOG_INFO, $additionalData = NULL) {
$name = ucfirst($this->spreadsheetImport->getContext());
$message = vsprintf('[%s] ' . $message, array($name));
$this->logger->log($message, $severity, $additionalData, 'SpreadsheetImport');
}
} }
WE\SpreadsheetImport\Log\SpreadsheetImportLoggerInterface:
scope: singleton
factoryObjectName: TYPO3\Flow\Log\LoggerFactory
arguments:
1:
value: 'SystemLogger'
2:
setting: TYPO3.Flow.log.systemLogger.logger
3:
setting: TYPO3.Flow.log.systemLogger.backend
4:
setting: TYPO3.Flow.log.systemLogger.backendOptions
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment