Skip to content

Commit

Permalink
Exclude profile for Bootstrap logging
Browse files Browse the repository at this point in the history
Logging what happens during tests can be very noisy. Introduces a
mechanism for excluding a profile from logging for this type of
scenario.

Fixes #198
  • Loading branch information
cspray committed Aug 11, 2022
1 parent 0be9ac0 commit 199f2c8
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -5,11 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v.1.4.0](https://github.com/cspray/annotated-container/tree/v1.4.0) - 2022-08-10
## [v.1.4.0](https://github.com/cspray/annotated-container/tree/v1.4.0) - 2022-08-11

### Added

- Added extensive logging to all compiler and container factory operations.
- Added ability to define a stdout or file logger when using the Bootstrap functionality.
- Added ability to define a set of profiles that should be excluded from logging when using the Bootstrap functionality.

### Fixed

Expand Down
7 changes: 7 additions & 0 deletions annotated-container.xsd
Expand Up @@ -67,6 +67,7 @@
<xs:all>
<xs:element name="file" type="xs:token" minOccurs="0" maxOccurs="1" />
<xs:element name="stdout" type="stdoutType" minOccurs="0" maxOccurs="1" />
<xs:element name="exclude" type="excludeType" minOccurs="0" maxOccurs="1" />
</xs:all>
</xs:complexType>

Expand All @@ -76,4 +77,10 @@
</xs:simpleContent>
</xs:complexType>

<xs:complexType name="excludeType">
<xs:sequence>
<xs:element name="profile" type="xs:token" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>

</xs:schema>
5 changes: 3 additions & 2 deletions src/Bootstrap.php
Expand Up @@ -35,8 +35,9 @@ public function bootstrapContainer(
$compileOptions = $compileOptions->withContainerDefinitionBuilderContextConsumer($containerDefinitionConsumer);
}

$profilesAllowLogging = count(array_intersect($profiles, $configuration->getLoggingExcludedProfiles())) === 0;
$logger = $configuration->getLogger();
if ($logger !== null) {
if ($logger !== null && $profilesAllowLogging) {
$compileOptions = $compileOptions->withLogger($logger);
}

Expand All @@ -54,7 +55,7 @@ public function bootstrapContainer(
}

$logger = $configuration->getLogger();
if ($logger !== null) {
if ($logger !== null && $profilesAllowLogging) {
$factoryOptions = $factoryOptions->withLogger($logger);
}

Expand Down
2 changes: 2 additions & 0 deletions src/BootstrappingConfiguration.php
Expand Up @@ -23,4 +23,6 @@ public function getContainerDefinitionConsumer() : ?ContainerDefinitionBuilderCo
public function getParameterStores() : array;

public function getLogger() : ?LoggerInterface;

public function getLoggingExcludedProfiles() : array;
}
15 changes: 15 additions & 0 deletions src/XmlBootstrappingConfiguration.php
Expand Up @@ -25,6 +25,7 @@ final class XmlBootstrappingConfiguration implements BootstrappingConfiguration
private readonly ?ContainerDefinitionBuilderContextConsumer $contextConsumer;
private readonly ?string $cacheDir;
private readonly ?LoggerInterface $logger;
private readonly array $excludedProfiles;

/**
* @var list<ParameterStore>
Expand Down Expand Up @@ -125,11 +126,21 @@ public function __construct(
$logger = new StdoutLogger($dateTimeProvider);
}

$excludedProfilesNodes = $xpath->query('/ac:annotatedContainer/ac:logging/ac:exclude/ac:profile/text()');
$excludedProfiles = [];

if ($excludedProfilesNodes instanceof DOMNodeList) {
foreach ($excludedProfilesNodes as $node) {
$excludedProfiles[] = $node->nodeValue;
}
}

$this->directories = $scanDirectories;
$this->contextConsumer = $contextConsumer;
$this->cacheDir = $cache;
$this->parameterStores = $parameterStores;
$this->logger = $logger;
$this->excludedProfiles = $excludedProfiles;
} finally {
libxml_clear_errors();
libxml_use_internal_errors(false);
Expand Down Expand Up @@ -159,4 +170,8 @@ public function getCacheDirectory() : ?string {
public function getLogger() : ?LoggerInterface {
return $this->logger;
}

public function getLoggingExcludedProfiles() : array {
return $this->excludedProfiles;
}
}
29 changes: 29 additions & 0 deletions test/BootstrapTest.php
Expand Up @@ -223,4 +223,33 @@ public function testBootstrapWithLogging() : void {
);
}

public function testBootstrapWithLoggingProfileExcluded() : void {
$directoryResolver = new FixtureBootstrappingDirectoryResolver();

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<annotatedContainer xmlns="https://annotated-container.cspray.io/schema/annotated-container.xsd">
<scanDirectories>
<source>
<dir>SingleConcreteService</dir>
</source>
</scanDirectories>
<logging>
<file>annotated-container.log</file>
<exclude><profile>test</profile></exclude>
</logging>
</annotatedContainer>
XML;

VirtualFilesystem::newFile('annotated-container.xml')
->withContent($xml)
->at($this->vfs);

(new Bootstrap(directoryResolver: $directoryResolver))->bootstrapContainer(['default', 'test']);

self::assertFileExists('vfs://root/annotated-container.log');
$logContents = file_get_contents('vfs://root/annotated-container.log');
self::assertSame('', $logContents);
}

}
36 changes: 36 additions & 0 deletions test/XmlBootstrappingConfigurationTest.php
Expand Up @@ -489,4 +489,40 @@ public function testLoggingFileAndStdoutConfigurationReturnsCorrectLogger() : vo
self::assertInstanceOf(FileLogger::class, $logger->getLoggers()[0]);
self::assertInstanceOf(StdoutLogger::class, $logger->getLoggers()[1]);
}

public function testLoggingFileAndStdoutConfigurationReturnsCorrectExcludedLoggingProfiles() : void {
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<annotatedContainer xmlns="https://annotated-container.cspray.io/schema/annotated-container.xsd">
<scanDirectories>
<source>
<dir>src</dir>
</source>
</scanDirectories>
<logging>
<file>logs/annotated-container.log</file>
<stdout />
<exclude>
<profile>foo</profile>
<profile>bar</profile>
<profile>baz</profile>
</exclude>
</logging>
</annotatedContainer>
XML;

VirtualFilesystem::newDirectory('logs')->at($this->vfs);
VirtualFilesystem::newFile('annotated-container.xml')
->withContent($xml)
->at($this->vfs);

self::assertFileDoesNotExist('vfs://root/logs/annotated-container.log');

$config = new XmlBootstrappingConfiguration(
'vfs://root/annotated-container.xml',
new FixtureBootstrappingDirectoryResolver()
);

self::assertSame(['foo', 'bar', 'baz'], $config->getLoggingExcludedProfiles());
}
}

0 comments on commit 199f2c8

Please sign in to comment.