Skip to content

Commit 665e7ea

Browse files
Merge pull request #312 from daniellienert/task/adapt-to-flow-6.0
!!! TASK: Refactor and simplify logging for compatibility with Flow 6.0
2 parents a5b91d5 + 171325b commit 665e7ea

16 files changed

Lines changed: 71 additions & 115 deletions

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ matrix:
1313
env: ES=5
1414

1515
before_install:
16-
- export NEOS_TARGET_VERSION=4.0
16+
- export NEOS_TARGET_VERSION=4.3
1717
- cd ..
1818
- if [ "$ES" = 5 ]; then wget --no-check-certificate https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.15.zip && unzip elasticsearch-5.6.15.zip && mv elasticsearch-5.6.15 elasticsearch; fi
1919
- cd elasticsearch

Classes/Command/NodeIndexCommandController.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\Error\ErrorInterface;
1919
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\NodeTypeMappingBuilderInterface;
2020
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
21-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\LoggerInterface;
2221
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service\ErrorHandlingService;
2322
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service\IndexWorkspaceTrait;
2423
use Flowpack\ElasticSearch\Domain\Model\Mapping;
@@ -33,9 +32,11 @@
3332
use Neos\Flow\Cli\CommandController;
3433
use Neos\Flow\Configuration\ConfigurationManager;
3534
use Neos\Flow\Configuration\Exception\InvalidConfigurationTypeException;
35+
use Neos\Flow\Log\Utility\LogEnvironment;
3636
use Neos\Flow\Mvc\Exception\StopActionException;
3737
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
3838
use Neos\Neos\Controller\CreateContentContextTrait;
39+
use Psr\Log\LoggerInterface;
3940
use Symfony\Component\Yaml\Yaml;
4041

4142
/**
@@ -246,18 +247,18 @@ public function indexNodeCommand(string $identifier, string $workspace = null):
246247
public function buildCommand(int $limit = null, bool $update = false, string $workspace = null, string $postfix = ''): void
247248
{
248249
if ($workspace !== null && $this->workspaceRepository->findByIdentifier($workspace) === null) {
249-
$this->logger->log('The given workspace (' . $workspace . ') does not exist.', LOG_ERR);
250+
$this->logger->error('The given workspace (' . $workspace . ') does not exist.', LogEnvironment::fromMethodName(__METHOD__));
250251
$this->quit(1);
251252
}
252253

253254
if ($update === true) {
254-
$this->logger->log('!!! Update Mode (Development) active!');
255+
$this->logger->warning('!!! Update Mode (Development) active!', LogEnvironment::fromMethodName(__METHOD__));
255256
} else {
256257
$this->createNewIndex($postfix);
257258
}
258259
$this->applyMapping();
259260

260-
$this->logger->log(sprintf('Indexing %snodes ... ', ($limit !== null ? 'the first ' . $limit . ' ' : '')));
261+
$this->logger->info(sprintf('Indexing %snodes ... ', ($limit !== null ? 'the first ' . $limit . ' ' : '')), LogEnvironment::fromMethodName(__METHOD__));
261262

262263
$count = 0;
263264

@@ -291,7 +292,7 @@ public function buildCommand(int $limit = null, bool $update = false, string $wo
291292
$this->outputLine();
292293
$this->outputLine('<error>Check your logs for more information</error>');
293294
} else {
294-
$this->logger->log('Done. (indexed ' . $count . ' nodes)', LOG_INFO);
295+
$this->logger->info('Done. (indexed ' . $count . ' nodes)', LogEnvironment::fromMethodName(__METHOD__));
295296
}
296297
$this->nodeIndexer->getIndex()->refresh();
297298

@@ -313,17 +314,17 @@ public function cleanupCommand(): void
313314
$indicesToBeRemoved = $this->nodeIndexer->removeOldIndices();
314315
if (count($indicesToBeRemoved) > 0) {
315316
foreach ($indicesToBeRemoved as $indexToBeRemoved) {
316-
$this->logger->log('Removing old index ' . $indexToBeRemoved);
317+
$this->logger->info('Removing old index ' . $indexToBeRemoved, LogEnvironment::fromMethodName(__METHOD__));
317318
}
318319
} else {
319-
$this->logger->log('Nothing to remove.');
320+
$this->logger->info('Nothing to remove.', LogEnvironment::fromMethodName(__METHOD__));
320321
}
321322
} catch (ApiException $exception) {
322323
$response = json_decode($exception->getResponse());
323324
if ($response->error instanceof \stdClass) {
324-
$this->logger->log(sprintf('Nothing removed. ElasticSearch responded with status %s, saying "%s: %s"', $response->status, $response->error->type, $response->error->reason));
325+
$this->logger->info(sprintf('Nothing removed. ElasticSearch responded with status %s, saying "%s: %s"', $response->status, $response->error->type, $response->error->reason), LogEnvironment::fromMethodName(__METHOD__));
325326
} else {
326-
$this->logger->log(sprintf('Nothing removed. ElasticSearch responded with status %s, saying "%s"', $response->status, $response->error));
327+
$this->logger->info(sprintf('Nothing removed. ElasticSearch responded with status %s, saying "%s"', $response->status, $response->error), LogEnvironment::fromMethodName(__METHOD__));
327328
}
328329
}
329330
}
@@ -339,11 +340,11 @@ protected function createNewIndex(string $postfix): void
339340
{
340341
$this->nodeIndexer->setIndexNamePostfix($postfix ?: (string)time());
341342
if ($this->nodeIndexer->getIndex()->exists() === true) {
342-
$this->logger->log(sprintf('Deleted index with the same postfix (%s)!', $postfix), LOG_WARNING);
343+
$this->logger->warning(sprintf('Deleted index with the same postfix (%s)!', $postfix), LogEnvironment::fromMethodName(__METHOD__));
343344
$this->nodeIndexer->getIndex()->delete();
344345
}
345346
$this->nodeIndexer->getIndex()->create();
346-
$this->logger->log('Created index ' . $this->nodeIndexer->getIndexName());
347+
$this->logger->info('Created index ' . $this->nodeIndexer->getIndexName(), LogEnvironment::fromMethodName(__METHOD__));
347348
}
348349

349350
/**
@@ -359,6 +360,6 @@ protected function applyMapping(): void
359360
/** @var Mapping $mapping */
360361
$mapping->apply();
361362
}
362-
$this->logger->log('Updated Mapping.');
363+
$this->logger->info('Updated Mapping.', LogEnvironment::fromMethodName(__METHOD__));
363364
}
364365
}

Classes/Driver/AbstractDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient;
18-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\LoggerInterface;
18+
use Psr\Log\LoggerInterface;
1919
use Neos\Flow\Annotations as Flow;
2020

2121
/**

Classes/Driver/AbstractIndexerDriver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use Neos\ContentRepository\Domain\Model\NodeInterface;
1818
use Neos\Flow\Annotations as Flow;
19+
use Neos\Flow\Log\Utility\LogEnvironment;
1920

2021
/**
2122
* Abstract Fulltext Indexer Driver
@@ -58,7 +59,7 @@ protected function findClosestFulltextRoot(NodeInterface $node)
5859
if ($closestFulltextNode === null) {
5960
// root of hierarchy, no fulltext root found anymore, abort silently...
6061
if ($node->getPath() !== '/' && $node->getPath() !== '/sites') {
61-
$this->logger->log(sprintf('NodeIndexer: No fulltext root found for node %s (%s)', $node->getIdentifier(), $node->getContextPath()), LOG_WARNING, null, 'ElasticSearch (CR)');
62+
$this->logger->warning(sprintf('NodeIndexer: No fulltext root found for node %s (%s)', $node->getIdentifier(), $node->getContextPath()), LogEnvironment::fromMethodName(__METHOD__));
6263
}
6364

6465
return null;

Classes/Driver/Version5/DocumentDriver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Neos\ContentRepository\Domain\Model\NodeInterface;
2222
use Neos\ContentRepository\Domain\Model\NodeType;
2323
use Neos\Flow\Annotations as Flow;
24+
use Neos\Flow\Log\Utility\LogEnvironment;
2425

2526
/**
2627
* Document driver for Elasticsearch version 5.x
@@ -90,7 +91,7 @@ public function deleteDuplicateDocumentNotMatchingType(Index $index, string $doc
9091
$result = $index->request('GET', '/_search/scroll?scroll=1m', [], $scrollId, false);
9192
$treatedContent = $result->getTreatedContent();
9293
}
93-
$this->logger->log(sprintf('NodeIndexer: Check duplicate nodes for %s (%s), found %d document(s)', $documentIdentifier, $nodeType->getName(), count($bulkRequest)), LOG_DEBUG, null, 'ElasticSearch (CR)');
94+
$this->logger->debug(sprintf('NodeIndexer: Check duplicate nodes for %s (%s), found %d document(s)', $documentIdentifier, $nodeType->getName(), count($bulkRequest)), LogEnvironment::fromMethodName(__METHOD__));
9495
if ($bulkRequest !== []) {
9596
$index->request('POST', '/_bulk', [], implode("\n", $bulkRequest) . "\n");
9697
}

Classes/Driver/Version5/IndexerDriver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Flowpack\ElasticSearch\Domain\Model\Document as ElasticSearchDocument;
2020
use Neos\ContentRepository\Domain\Model\NodeInterface;
2121
use Neos\Flow\Annotations as Flow;
22+
use Neos\Flow\Log\Utility\LogEnvironment;
2223

2324
/**
2425
* Indexer driver for Elasticsearch version 5.x
@@ -96,12 +97,12 @@ public function fulltext(NodeInterface $node, array $fulltextIndexOfNode, string
9697

9798
if ($closestFulltextNode->isRemoved()) {
9899
// fulltext root is removed, abort silently...
99-
$this->logger->log(sprintf('NodeIndexer (%s): Fulltext root found for %s (%s) not updated, it is removed', $closestFulltextNodeDocumentIdentifier, $node->getPath(), $node->getIdentifier()), LOG_DEBUG, null, 'ElasticSearch (CR)');
100+
$this->logger->debug(sprintf('NodeIndexer (%s): Fulltext root found for %s (%s) not updated, it is removed', $closestFulltextNodeDocumentIdentifier, $node->getPath(), $node->getIdentifier()), LogEnvironment::fromMethodName(__METHOD__));
100101

101102
return [];
102103
}
103104

104-
$this->logger->log(sprintf('NodeIndexer (%s): Updated fulltext index for %s (%s)', $closestFulltextNodeDocumentIdentifier, $closestFulltextNodeContextPath, $closestFulltextNode->getIdentifier()), LOG_DEBUG, null, 'ElasticSearch (CR)');
105+
$this->logger->debug(sprintf('NodeIndexer (%s): Updated fulltext index for %s (%s)', $closestFulltextNodeDocumentIdentifier, $closestFulltextNodeContextPath, $closestFulltextNode->getIdentifier()), LogEnvironment::fromMethodName(__METHOD__));
105106

106107
$upsertFulltextParts = [];
107108
if (!empty($fulltextIndexOfNode)) {

Classes/Driver/Version5/RequestDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\Version5;
@@ -29,6 +28,7 @@ class RequestDriver extends AbstractDriver implements RequestDriverInterface
2928
/**
3029
* {@inheritdoc}
3130
* @throws \Flowpack\ElasticSearch\Exception
31+
* @throws \Neos\Flow\Http\Exception
3232
*/
3333
public function bulk(Index $index, $request): array
3434
{
@@ -39,7 +39,7 @@ public function bulk(Index $index, $request): array
3939
// Bulk request MUST end with line return
4040
$request = trim($request) . "\n";
4141

42-
$response = $index->request('POST', '/_bulk', [], $request)->getOriginalResponse()->getContent();
42+
$response = $index->request('POST', '/_bulk', [], $request)->getOriginalResponse()->getBody()->getContents();
4343

4444
return array_map('json_decode', explode("\n", $response));
4545
}

Classes/Eel/ElasticSearchQueryBuilder.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel;
@@ -19,7 +18,9 @@
1918
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient;
2019
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
2120
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\QueryBuildingException;
22-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\LoggerInterface;
21+
use Neos\Flow\Log\ThrowableStorageInterface;
22+
use Neos\Flow\Log\Utility\LogEnvironment;
23+
use Psr\Log\LoggerInterface;
2324
use Flowpack\ElasticSearch\Transfer\Exception\ApiException;
2425
use Neos\ContentRepository\Domain\Model\NodeInterface;
2526
use Neos\ContentRepository\Search\Search\QueryBuilderInterface;
@@ -59,6 +60,12 @@ class ElasticSearchQueryBuilder implements QueryBuilderInterface, ProtectedConte
5960
*/
6061
protected $logger;
6162

63+
/**
64+
* @Flow\Inject
65+
* @var ThrowableStorageInterface
66+
*/
67+
protected $throwableStorage;
68+
6269
/**
6370
* @var boolean
6471
*/
@@ -189,6 +196,7 @@ public function sort($configuration): ElasticSearchQueryBuilder
189196
*
190197
* @param integer $limit
191198
* @return ElasticSearchQueryBuilder
199+
* @throws \Neos\Flow\Persistence\Exception\IllegalObjectTypeException
192200
* @api
193201
*/
194202
public function limit($limit)
@@ -570,7 +578,9 @@ public function getFullElasticSearchHitForNode(NodeInterface $node): array
570578
* This method is rather internal; just to be called from the ElasticSearchQueryResult. For the public API, please use execute()
571579
*
572580
* @return array<\Neos\ContentRepository\Domain\Model\NodeInterface>
581+
* @throws Exception
573582
* @throws \Flowpack\ElasticSearch\Exception
583+
* @throws \Neos\Flow\Http\Exception
574584
*/
575585
public function fetch(): array
576586
{
@@ -586,13 +596,13 @@ public function fetch(): array
586596

587597
$this->result['nodes'] = [];
588598

589-
$this->logThisQuery && $this->logger->log(sprintf('Query Log (%s): %s -- execution time: %s ms -- Limit: %s -- Number of results returned: %s -- Total Results: %s', $this->logMessage, $request, (($timeAfterwards - $timeBefore) * 1000), $this->limit, count($searchResult->getHits()), $searchResult->getTotal()), LOG_DEBUG);
599+
$this->logThisQuery && $this->logger->debug(sprintf('Query Log (%s): %s -- execution time: %s ms -- Limit: %s -- Number of results returned: %s -- Total Results: %s', $this->logMessage, $request, (($timeAfterwards - $timeBefore) * 1000), $this->limit, count($searchResult->getHits()), $searchResult->getTotal()), LogEnvironment::fromMethodName(__METHOD__));
590600

591601
if (count($searchResult->getHits()) > 0) {
592602
$this->result['nodes'] = $this->convertHitsToNodes($searchResult->getHits());
593603
}
594604
} catch (ApiException $exception) {
595-
$this->logger->logException($exception);
605+
$this->throwableStorage->logThrowable($exception);
596606
$this->result['nodes'] = [];
597607
}
598608

@@ -639,7 +649,9 @@ public function executeUncached(): ElasticSearchQueryResult
639649
* Return the total number of hits for the query.
640650
*
641651
* @return integer
652+
* @throws Exception
642653
* @throws \Flowpack\ElasticSearch\Exception
654+
* @throws \Neos\Flow\Http\Exception
643655
* @api
644656
*/
645657
public function count()
@@ -653,7 +665,7 @@ public function count()
653665
$treatedContent = $response->getTreatedContent();
654666
$count = $treatedContent['count'];
655667

656-
$this->logThisQuery && $this->logger->log('Count Query Log (' . $this->logMessage . '): ' . $request . ' -- execution time: ' . (($timeAfterwards - $timeBefore) * 1000) . ' ms -- Total Results: ' . $count, LOG_DEBUG);
668+
$this->logThisQuery && $this->logger->debug('Count Query Log (' . $this->logMessage . '): ' . $request . ' -- execution time: ' . (($timeAfterwards - $timeBefore) * 1000) . ' ms -- Total Results: ' . $count, LogEnvironment::fromMethodName(__METHOD__));
657669

658670
return $count;
659671
}
@@ -712,7 +724,7 @@ public function moreLikeThis(array $like, array $fields = [], array $options = [
712724
$respondedDocuments = Arrays::getValueByPath($response, 'hits.hits');
713725

714726
if (count($respondedDocuments) === 0) {
715-
$this->logger->log(sprintf('The node with identifier %s was not found in the elasticsearch index.', $node->getIdentifier()), LOG_INFO);
727+
$this->logger->info(sprintf('The node with identifier %s was not found in the elasticsearch index.', $node->getIdentifier()), LogEnvironment::fromMethodName(__METHOD__));
716728
return [];
717729
}
718730

@@ -852,7 +864,7 @@ protected function convertHitsToNodes(array $hits): array
852864
}
853865
}
854866

855-
$this->logThisQuery && $this->logger->log('Returned nodes (' . $this->logMessage . '): ' . count($nodes), LOG_DEBUG);
867+
$this->logThisQuery && $this->logger->debug('Returned nodes (' . $this->logMessage . '): ' . count($nodes));
856868

857869
$this->elasticSearchHitsIndexedByNodeFromLastRequest = $elasticSearchHitPerNode;
858870

@@ -865,8 +877,10 @@ protected function convertHitsToNodes(array $hits): array
865877
* hiddenBeforeDateTime or hiddenAfterDateTime properties of all nodes in the result.
866878
*
867879
* @return int
880+
* @throws Exception
868881
* @throws QueryBuildingException
869882
* @throws \Flowpack\ElasticSearch\Exception
883+
* @throws \Neos\Flow\Http\Exception
870884
*/
871885
public function cacheLifetime(): int
872886
{
@@ -889,8 +903,10 @@ public function cacheLifetime(): int
889903
/**
890904
* @param string $dateField
891905
* @return int
906+
* @throws Exception
892907
* @throws QueryBuildingException
893908
* @throws \Flowpack\ElasticSearch\Exception
909+
* @throws \Neos\Flow\Http\Exception
894910
*/
895911
protected function getNearestFutureDate(string $dateField): int
896912
{

Classes/Factory/AbstractDriverSpecificObjectFactory.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\DriverConfigurationException;
18-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\LoggerInterface;
18+
use Psr\Log\LoggerInterface;
1919
use Neos\Flow\Annotations as Flow;
2020

2121
/**
@@ -57,8 +57,6 @@ protected function resolve(string $type)
5757

5858
$className = trim($this->mapping[$version][$type]['className']);
5959

60-
$this->logger->log(sprintf('Load %s implementation for Elastic %s (%s)', $type, $version, $className), LOG_DEBUG);
61-
6260
if (!isset($this->mapping[$version][$type]['arguments'])) {
6361
return new $className();
6462
}

Classes/Indexer/Error/BulkIndexingError.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* source code.
1515
*/
1616

17-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\LoggerInterface;
17+
use Neos\Flow\Log\Utility\LogEnvironment;
18+
use Psr\Log\LoggerInterface;
1819
use Neos\Flow\Annotations as Flow;
1920

2021
/**
@@ -76,9 +77,9 @@ public function log(): void
7677
{
7778
if (file_exists(FLOW_PATH_DATA . 'Logs/Elasticsearch') && is_dir(FLOW_PATH_DATA . 'Logs/Elasticsearch') && is_writable(FLOW_PATH_DATA . 'Logs/Elasticsearch')) {
7879
file_put_contents($this->filename, $this->renderErrors());
79-
$this->logger->log($this->message, LOG_ERR, [], 'Flowpack.ElasticSearch.ContentRepositoryAdaptor', __CLASS__, __FUNCTION__);
80+
$this->logger->error($this->message, LogEnvironment::fromMethodName(__METHOD__));
8081
} else {
81-
$this->logger->log(sprintf('Could not write indexing errors backtrace into %s because the directory could not be created or is not writable.', FLOW_PATH_DATA . 'Logs/Elasticsearch/'), LOG_WARNING, [], 'Flowpack.ElasticSearch.ContentRepositoryAdaptor', __CLASS__, __FUNCTION__);
82+
$this->logger->warning(sprintf('Could not write indexing errors backtrace into %s because the directory could not be created or is not writable.', FLOW_PATH_DATA . 'Logs/Elasticsearch/'), LogEnvironment::fromMethodName(__METHOD__));
8283
}
8384
}
8485

0 commit comments

Comments
 (0)