Skip to content

Commit 9a3072b

Browse files
authored
Modify Default Logger (#93)
* log to stdout instead of output. Output returns logs as part of the HTTP request * 🖊️ Fixed unit tests, edited Default Logger. * 🖊️ Update years
1 parent eca6b6c commit 9a3072b

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

src/Optimizely/Logger/DefaultLogger.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016, Optimizely
3+
* Copyright 2016,2018 Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -36,11 +36,12 @@ class DefaultLogger implements LoggerInterface
3636
* DefaultLogger constructor.
3737
*
3838
* @param int $minLevel Minimum level of messages to be logged.
39+
* @param string $stream The PHP stream to log output.
3940
*/
40-
public function __construct($minLevel = Logger::INFO)
41+
public function __construct($minLevel = Logger::INFO, $stream = "stdout")
4142
{
4243
$formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message%\n");
43-
$streamHandler = new StreamHandler('php://output', $minLevel);
44+
$streamHandler = new StreamHandler("php://{$stream}", $minLevel);
4445
$streamHandler->setFormatter($formatter);
4546
$this->logger = new Logger('Optimizely');
4647
$this->logger->pushHandler($streamHandler);

src/Optimizely/Optimizely.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2017, Optimizely
3+
* Copyright 2016-2018, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -114,7 +114,9 @@ public function __construct(
114114

115115
if (!$this->validateDatafile($datafile, $skipJsonValidation)) {
116116
$this->_isValid = false;
117-
$this->_logger = new DefaultLogger();
117+
$defaultLogger = new DefaultLogger();
118+
119+
$defaultLogger->log(Logger::ERROR, 'Provided "datafile" has invalid schema.');
118120
$this->_logger->log(Logger::ERROR, 'Provided "datafile" has invalid schema.');
119121
return;
120122
}

tests/LoggerTests/DefaultLoggerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016, Optimizely
3+
* Copyright 2016,2018 Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@ class DefaultLoggerTest extends \PHPUnit_Framework_TestCase
2323
{
2424
public function testDefaultLogger()
2525
{
26-
$logger = new DefaultLogger();
26+
$logger = new DefaultLogger(Logger::INFO, 'output');
2727
$logger->log(Logger::INFO, 'Log me please.');
2828

2929
$this->expectOutputRegex('/Log me please./');

tests/OptimizelyTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2017, Optimizely
3+
* Copyright 2016-2018, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -36,6 +36,8 @@
3636

3737
class OptimizelyTest extends \PHPUnit_Framework_TestCase
3838
{
39+
const OUTPUT_STREAM = 'output';
40+
3941
private $datafile;
4042
private $eventBuilderMock;
4143
private $loggerMock;
@@ -138,7 +140,7 @@ public function testValidateDatafileInvalidFileJsonValidationNotSkipped()
138140

139141
$this->assertFalse(
140142
$validateInputsMethod->invoke(
141-
new Optimizely('Random datafile'),
143+
new Optimizely('Random datafile', null, new DefaultLogger(Logger::INFO, self::OUTPUT_STREAM)),
142144
'Random datafile',
143145
false
144146
)
@@ -164,7 +166,7 @@ public function testValidateDatafileInvalidFileJsonValidationSkipped()
164166
public function testActivateInvalidOptimizelyObject()
165167
{
166168
$optimizelyMock = $this->getMockBuilder(Optimizely::class)
167-
->setConstructorArgs(array('Random datafile', null, $this->loggerMock))
169+
->setConstructorArgs(array('Random datafile', null, new DefaultLogger(Logger::INFO, self::OUTPUT_STREAM)))
168170
->setMethods(array('sendImpressionEvent'))
169171
->getMock();
170172

@@ -452,7 +454,7 @@ public function testActivateExperimentNotRunning()
452454

453455
public function testGetVariationInvalidOptimizelyObject()
454456
{
455-
$optlyObject = new Optimizely('Random datafile');
457+
$optlyObject = new Optimizely('Random datafile', null, new DefaultLogger(Logger::INFO, self::OUTPUT_STREAM));
456458
$optlyObject->getVariation('some_experiment', 'some_user');
457459
$this->expectOutputRegex('/Datafile has invalid format. Failing "getVariation"./');
458460
}
@@ -665,7 +667,7 @@ public function testValidatePreconditionsUserNotInForcedVariationInExperiment()
665667

666668
public function testTrackInvalidOptimizelyObject()
667669
{
668-
$optlyObject = new Optimizely('Random datafile');
670+
$optlyObject = new Optimizely('Random datafile', null, new DefaultLogger(Logger::INFO, self::OUTPUT_STREAM));
669671

670672
// Verify that sendNotifications isn't called
671673
$this->notificationCenterMock->expects($this->never())
@@ -2157,7 +2159,7 @@ public function testGetVariationBucketingIdAttribute()
21572159

21582160
public function testIsFeatureEnabledGivenInvalidDataFile()
21592161
{
2160-
$optlyObject = new Optimizely('Random datafile', null, $this->loggerMock);
2162+
$optlyObject = new Optimizely('Random datafile', null, new DefaultLogger(Logger::INFO, self::OUTPUT_STREAM));
21612163

21622164
$this->expectOutputRegex("/Datafile has invalid format. Failing 'isFeatureEnabled'./");
21632165
$optlyObject->isFeatureEnabled("boolean_feature", "user_id");
@@ -2365,7 +2367,7 @@ public function testIsFeatureEnabledGivenFeatureFlagIsEnabledAndUserIsNotBeingEx
23652367

23662368
public function testGetEnabledFeaturesGivenInvalidDataFile()
23672369
{
2368-
$optlyObject = new Optimizely('Random datafile');
2370+
$optlyObject = new Optimizely('Random datafile', null, new DefaultLogger(Logger::INFO, self::OUTPUT_STREAM));
23692371

23702372
$this->expectOutputRegex("/Datafile has invalid format. Failing 'getEnabledFeatures'./");
23712373

0 commit comments

Comments
 (0)