Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ This SDK is compatible with [Featurevisor](https://featurevisor.com/) v2.0 proje
- [Updating datafile](#updating-datafile)
- [Interval-based update](#interval-based-update)
- [Logging](#logging)
- [Levels](#levels)
- [Customizing levels](#customizing-levels)
- [Handler](#handler)
- [Events](#events)
Expand Down Expand Up @@ -354,21 +353,14 @@ Here's an example of using interval-based update:
## Logging

By default, Featurevisor SDKs will print out logs to the console for `info` level and above.

### Levels

These are all the available log levels:

- `error`
- `warn`
- `info`
- `debug`
Featurevisor PHP-SDK by default uses [PSR-3 standard](https://www.php-fig.org/psr/psr-3/) simple implementation.
You can also choose from many mature implementations like e.g. [Monolog](https://github.com/Seldaek/monolog)

### Customizing levels

If you choose `debug` level to make the logs more verbose, you can set it at the time of SDK initialization.

Setting `debug` level will print out all logs, including `info`, `warn`, and `error` levels.
Setting `debug` level will print out all logs, including `info`, `warning`, and `error` levels.

```php
use function Featurevisor\createInstance;
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"Featurevisor\\Tests\\": "tests/"
}
},
"require": {},
"require": {
"psr/log": "^2.0"
},
"require-dev": {
"php": "^8.0",
"phpunit/phpunit": "^10"
Expand Down
69 changes: 61 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 20 additions & 9 deletions featurevisor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

require __DIR__ . '/vendor/autoload.php';

use Featurevisor\DatafileReader;
use Psr\Log\LogLevel;
use function Featurevisor\createInstance;
use function Featurevisor\createLogger;

/**
* CLI Options
Expand Down Expand Up @@ -93,11 +96,11 @@ function buildDatafiles(string $featurevisorProjectPath, array $environments): a
}

function getLoggerLevel(array $cliOptions): string {
$level = 'warn';
$level = LogLevel::WARNING;
if ($cliOptions['verbose'] === true) {
$level = 'debug';
$level = LogLevel::DEBUG;
} else if ($cliOptions['quiet'] === true) {
$level = 'error';
$level = LogLevel::ERROR;
}
return $level;
}
Expand Down Expand Up @@ -252,9 +255,9 @@ function testSegment(array $assertion, array $segment, string $level): array {
'segments' => []
];

$datafileReader = new \Featurevisor\DatafileReader([
$datafileReader = new DatafileReader([
'datafile' => $datafile,
'logger' => \Featurevisor\createLogger([
'logger' => createLogger([
'level' => $level,
]),
]);
Expand Down Expand Up @@ -303,7 +306,9 @@ function test(array $cliOptions) {
$datafile = $datafilesByEnvironment[$environment];
$sdkInstancesByEnvironment[$environment] = createInstance([
'datafile' => $datafile,
'logLevel' => $level,
'logger' => createLogger([
'level' => $level,
]),
'hooks' => [
[
'name' => 'tester-hook',
Expand Down Expand Up @@ -340,7 +345,9 @@ function test(array $cliOptions) {
$datafile = $datafilesByEnvironment[$environment];
$f = createInstance([
'datafile' => $datafile,
'logLevel' => $level,
'logger' => createLogger([
'level' => $level,
]),
'hooks' => [
[
'name' => 'tester-hook',
Expand Down Expand Up @@ -415,7 +422,9 @@ function benchmark(array $cliOptions) {

$f = createInstance([
'datafile' => $datafilesByEnvironment[$cliOptions['environment']],
'logLevel' => $level,
'logger' => createLogger([
'level' => $level,
]),
]);

$value = null;
Expand Down Expand Up @@ -474,7 +483,9 @@ function assessDistribution(array $cliOptions) {

$f = createInstance([
'datafile' => $datafilesByEnvironment[$cliOptions['environment']],
'logLevel' => $level,
'logger' => createLogger([
'level' => $level,
]),
]);

$value = null;
Expand Down
14 changes: 7 additions & 7 deletions src/DatafileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Featurevisor;

use Psr\Log\LoggerInterface;

class DatafileReader
{
private string $schemaVersion;
private string $revision;
private array $segments;
private array $features;
private Logger $logger;
private LoggerInterface $logger;
private array $regexCache;

public function __construct(array $options)
Expand Down Expand Up @@ -152,12 +154,10 @@ public function allConditionsAreMatched($conditions, array $context): bool
try {
return Conditions::conditionIsMatched($conditions, $context, $getRegex);
} catch (\Exception $e) {
$this->logger->warn($e->getMessage(), [
'error' => $e,
'details' => [
'condition' => $conditions,
'context' => $context,
],
$this->logger->warning($e->getMessage(), [
'exception' => $e,
'condition' => $conditions,
'context' => $context,
]);
return false;
}
Expand Down
14 changes: 8 additions & 6 deletions src/EvaluateNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace Featurevisor;

use Psr\Log\LoggerInterface;

class EvaluateNotFound
{
public static function evaluate(array $options): array
{
$type = $options['type'];
$featureKey = $options['featureKey'];
$variableKey = $options['variableKey'] ?? null;
/** @var LoggerInterface $logger */
$logger = $options['logger'];
$datafileReader = $options['datafileReader'];

Expand All @@ -24,7 +27,7 @@ public static function evaluate(array $options): array
'reason' => Evaluation::FEATURE_NOT_FOUND
];

$logger->warn('feature not found', $result['evaluation']);
$logger->warning('feature not found', $result['evaluation']);

return $result;
}
Expand All @@ -33,7 +36,7 @@ public static function evaluate(array $options): array

// feature: deprecated
if ($type === 'flag' && ($feature['deprecated'] ?? false)) {
$logger->warn('feature is deprecated', ['featureKey' => $featureKey]);
$logger->warning('feature is deprecated', ['featureKey' => $featureKey]);
}

// variableSchema
Expand All @@ -53,15 +56,14 @@ public static function evaluate(array $options): array
'variableKey' => $variableKey
];

$logger->warn('variable schema not found', $result['evaluation']);
$logger->warning('variable schema not found', $result['evaluation']);

return $result;
}

$result['variableSchema'] = $variableSchema;

if ($variableSchema['deprecated'] ?? false) {
$logger->warn('variable is deprecated', [
$logger->warning('variable is deprecated', [
'featureKey' => $featureKey,
'variableKey' => $variableKey
]);
Expand All @@ -76,7 +78,7 @@ public static function evaluate(array $options): array
'reason' => Evaluation::NO_VARIATIONS
];

$logger->warn('no variations', $result['evaluation']);
$logger->warning('no variations', $result['evaluation']);

return $result;
}
Expand Down
4 changes: 3 additions & 1 deletion src/HooksManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Featurevisor;

use Psr\Log\LoggerInterface;

class HooksManager
{
private array $hooks = [];
private Logger $logger;
private LoggerInterface $logger;

public function __construct(array $options)
{
Expand Down
25 changes: 12 additions & 13 deletions src/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Featurevisor;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class Instance
{
private array $context = [];
private Logger $logger;
private LoggerInterface $logger;
private ?array $sticky = null;
private DatafileReader $datafileReader;
private HooksManager $hooksManager;
Expand All @@ -15,9 +18,7 @@ public function __construct(array $options = [])
{
// from options
$this->context = $options['context'] ?? [];
$this->logger = $options['logger'] ?? createLogger([
'level' => $options['logLevel'] ?? Logger::DEFAULT_LEVEL
]);
$this->logger = $options['logger'] ?? new NullLogger();
$this->hooksManager = new HooksManager([
'hooks' => $options['hooks'] ?? [],
'logger' => $this->logger
Expand Down Expand Up @@ -49,11 +50,6 @@ public function __construct(array $options = [])
$this->logger->info('Featurevisor SDK initialized');
}

public function setLogLevel(string $level): void
{
$this->logger->setLevel($level);
}

public function setDatafile($datafile): void
{
try {
Expand All @@ -69,7 +65,7 @@ public function setDatafile($datafile): void
$this->logger->info('datafile set', $details);
$this->emitter->trigger('datafile_set', $details);
} catch (\Exception $e) {
$this->logger->error('could not parse datafile', ['error' => $e->getMessage()]);
$this->logger->error('could not parse datafile', ['error' => $e->getMessage(), 'exception' => $e]);;
}
}

Expand Down Expand Up @@ -211,7 +207,9 @@ public function getVariation(string $featureKey, array $context = [], array $opt

return null;
} catch (\Exception $e) {
$this->logger->error('getVariation', [
$this->logger->error($e->getMessage(), [
'exception' => $e,
'action' => 'getVariation',
'featureKey' => $featureKey,
'error' => $e->getMessage()
]);
Expand Down Expand Up @@ -251,10 +249,11 @@ public function getVariable(string $featureKey, string $variableKey, array $cont
}
return null;
} catch (\Exception $e) {
$this->logger->error('getVariable', [
$this->logger->error($e->getMessage(), [
'exception' => $e,
'action' => 'getVariable',
'featureKey' => $featureKey,
'variableKey' => $variableKey,
'error' => $e->getMessage()
]);
return null;
}
Expand Down
Loading