Skip to content

Commit

Permalink
Add verbose environment variable (#173)
Browse files Browse the repository at this point in the history
Adds environment variable `VIGILANT_VERBOSE` to control logging.
  • Loading branch information
VerifiedJoseph authored Apr 29, 2024
1 parent b696629 commit e3de7ff
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 3 deletions.
6 changes: 5 additions & 1 deletion daemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
$config = new Config();
$config->validate();

$logger = new Logger(
$config->getTimezone(),
$config->getLoggingLevel()
);

$fetch = new Fetch();
$logger = new Logger($config->getTimezone());
$feeds = new Feeds($config, $logger);

/** @phpstan-ignore-next-line */
Expand Down
6 changes: 6 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ Vigilant can be used a with ntfy server that has password or [access token](http
| `VIGILANT_NOTIFICATION_NTFY_USERNAME` | Ntfy authentication username. |
| `VIGILANT_NOTIFICATION_NTFY_PASSWORD` | Ntfy authentication password. |
| `VIGILANT_NOTIFICATION_NTFY_TOKEN` | Ntfy access token. |

## Logging

| Name | Description |
| ------------------ | --------------------------------------------------------------- |
| `VIGILANT_VERBOSE` | Enable additional logging. Supported values: `true` or `false`. |
11 changes: 11 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Config

/** @var array<string, int|string|false> $defaults Default values for config parameters */
private array $defaults = [
'logging_level' => 1,
'feeds_file' => 'feeds.yaml',
'notification_gotify_priority' => 4,
'notification_ntfy_priority' => 3,
Expand All @@ -51,13 +52,23 @@ public function validate(): void
{
$this->requireConfigFile();

$this->validate->verbose();
$this->validate->timezone();
$this->validate->folder($this->getCachePath());
$this->validate->feedsFile();
$this->validate->notificationService($this->notificationServices);
$this->config = $this->validate->getConfig();
}

/**
* Return logging level
* @return int
*/
public function getLoggingLevel(): int
{
return $this->config['logging_level'];
}

/**
* Returns timezone
*
Expand Down
10 changes: 10 additions & 0 deletions src/Config/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public function getConfig(): array
return $this->config;
}

/**
* Check if a environment variable is a boolean
*
* @param string $name Variable name excluding prefix
*/
public function isEnvBoolean(string $name): bool
{
return in_array($this->getEnv($name), ['true', 'false']);
}

/**
* Check for an environment variable
*
Expand Down
17 changes: 17 additions & 0 deletions src/Config/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ public function folder(string $path): void
}
}

/**
* Validate `VIGILANT_VERBOSE`
* @throws ConfigException if value is not a boolean
*/
public function verbose(): void
{
if ($this->hasEnv('VERBOSE') === true) {
if ($this->isEnvBoolean('VERBOSE') === false) {
throw new ConfigException('Verbose environment variable value must a boolean [VIGILANT_VERBOSE]');
}

if ($this->getEnv('VERBOSE') === 'true') {
$this->config['logging_level'] = 2;
}
}
}

/**
* Validate `VIGILANT_TIMEZONE`
* @throws ConfigException if invalid timezone is given
Expand Down
12 changes: 12 additions & 0 deletions tests/Config/AbstractValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ public function testGetConfig(): void
$this->assertEquals($expected, $class->getConfig());
}

/**
* Test `isEnvBoolean`
*/
public function testIsEnvBoolean(): void
{
putenv('VIGILANT_TEST=true');

$class = new class ([]) extends AbstractValidator {
};
$this->assertTrue($class->isEnvBoolean('TEST'));
}

/**
* Test `getEnv()`
*/
Expand Down
28 changes: 28 additions & 0 deletions tests/Config/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,34 @@ public function testExtensions(): void
$validate->extensions(['pgp']);
}

/**
* Test `verbose()`
*/
public function testVerbose(): void
{
putenv('VIGILANT_VERBOSE=true');

$validate = new Validator(self::$defaults);
$validate->verbose();
$config = $validate->getConfig();

$this->assertEquals(2, $config['logging_level']);
}

/**
* Test `verbose()` with `VIGILANT_VERBOSE` value not a boolean
*/
public function testVerboseNotBoolean(): void
{
$this->expectException(ConfigException::class);
$this->expectExceptionMessage('Verbose environment variable value must a boolean');

putenv('VIGILANT_VERBOSE=string');

$validate = new Validator(self::$defaults);
$validate->verbose();
}

/**
* Test `timezone()`
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public function testValidate(): void
$this->assertEquals('hello', $config->getNtfyTopic());
}

/**
* Test `getLoggingLevel()`
*/
public function testGetLoggingLevel(): void
{
$config = new Config();
$this->assertEquals(
self::$defaults['logging_level'],
$config->getLoggingLevel()
);
}

/**
* Test `getTimezone()`
*/
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected static function getSamplePath(string $name): string
protected function resetEnvs(): void
{
// Unset environment variables before each test
putenv('VIGILANT_VERBOSE');
putenv('VIGILANT_TIMEZONE');
putenv('VIGILANT_FEEDS_FILE');
putenv('VIGILANT_NOTIFICATION_SERVICE');
Expand Down
7 changes: 5 additions & 2 deletions vigilant.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
$config = new Config();
$config->validate();

$logger = new Logger($config->getTimezone());
$logger->debug(sprintf('Vigilant version %s', Version::get()));
$logger = new Logger(
$config->getTimezone(),
$config->getLoggingLevel()
);
$logger->debug(sprintf('Vigilant v%s', Version::get()));

$fetch = new Fetch();
$feeds = new Feeds($config, $logger);
Expand Down

0 comments on commit e3de7ff

Please sign in to comment.