Beispiel #1
0
 /**
  * Set the current log level
  * @param $level
  * @throws Exceptions\InvalidArgumentException
  */
 public function setLevel($level)
 {
     if ($level instanceof LoggerLevel) {
         $this->level = $level;
         return;
     }
     if (is_int($level) || is_string($level)) {
         $this->level = LoggerLevel::findLevel($level);
         return;
     }
     throw new InvalidArgumentException('You must provide a valid LoggerLevel argument.');
 }
Beispiel #2
0
 public function testLtEq()
 {
     $logLevel1 = LoggerLevel::custom(57, 'hines');
     $logLevel2 = LoggerLevel::custom(57, 'hines');
     $logLevel3 = LoggerLevel::getStandardLevelByName('fatal');
     $this->assertTrue($logLevel1->isLessThanOrEqual($logLevel2));
     $this->assertTrue($logLevel1->isLessThanOrEqual('debug'));
     $this->assertTrue($logLevel1->isLessThanOrEqual(500));
     $this->assertFalse($logLevel1->isLessThanOrEqual($logLevel3));
     $this->assertFalse($logLevel1->isLessThanOrEqual('emergency'));
     $this->assertFalse($logLevel1->isLessThanOrEqual(-2147483647));
 }
Beispiel #3
0
    public function testAppend()
    {
        $appender = new Console();
        $streamProp = new ReflectionProperty($appender, 'fp');
        $memStream = \fopen('php://memory', 'rw');
        $streamProp->setAccessible(true);
        $streamProp->setValue($appender, $memStream);
        $expected = 'Test Log Message' . PHP_EOL;
        $appender->append($expected, []);
        \fseek($memStream, 0);
        $actual = \stream_get_contents($memStream);
        $this->assertEquals(trim(preg_replace('/\\s+/', ' ', $expected)), trim(preg_replace('/\\s+/', ' ', $actual)));
        $pos = ftell($memStream);
        $message = 'Test {ary} Message';
        $context = ['ary' => ['one', 'two' => [1, 1]]];
        $appender->append($message, $context);
        $expected = 'Test Array
(
    [0] => one
    [two] => Array
        (
            [0] => 1
            [1] => 1
        )

)
 Message';
        \fseek($memStream, $pos);
        $actual = \stream_get_contents($memStream);
        $pos = ftell($memStream);
        $this->assertEquals(trim(preg_replace('/\\s+/', ' ', $expected)), trim(preg_replace('/\\s+/', ' ', $actual)));
        $message = 'Test {exception} Message';
        $context = ['exception' => new \Exception('A PHPUNIT Test.')];
        $appender->append($message, $context);
        $expected = 'A PHPUNIT Test.';
        \fseek($memStream, $pos);
        $actual = \stream_get_contents($memStream);
        $pos = ftell($memStream);
        $this->assertContains($expected, $actual);
        $message = 'Test {object} Message';
        $context = ['object' => $this];
        $appender->append($message, $context);
        $expected = 'ConsoleTest::testAppend';
        \fseek($memStream, $pos);
        $actual = \stream_get_contents($memStream);
        $pos = ftell($memStream);
        $this->assertContains($expected, $actual);
        $message = 'Test {object} Message';
        $obj = new stdClass();
        $obj->tp = "test param";
        $context = ['object' => $obj];
        $appender->append($message, $context);
        $expected = 'stdClass';
        \fseek($memStream, $pos);
        $actual = \stream_get_contents($memStream);
        $pos = ftell($memStream);
        $this->assertContains($expected, $actual);
        $message = 'Test {object} Message';
        $context = ['object' => \Piton\Common\LoggerLevel::findLevel('alert')];
        $appender->append($message, $context);
        $expected = 'ALERT';
        \fseek($memStream, $pos);
        $actual = \stream_get_contents($memStream);
        $pos = ftell($memStream);
        $this->assertContains($expected, $actual);
        $message = 'Test {boool} Message';
        $context = ['boool' => TRUE];
        $appender->append($message, $context);
        $expected = 'TRUE';
        \fseek($memStream, $pos);
        $actual = \stream_get_contents($memStream);
        $pos = ftell($memStream);
        $this->assertContains($expected, $actual);
        $message = 'Test {boool} Message';
        $context = ['boool' => FALSE];
        $appender->append($message, $context);
        $expected = 'FALSE';
        \fseek($memStream, $pos);
        $actual = \stream_get_contents($memStream);
        $pos = ftell($memStream);
        $this->assertContains($expected, $actual);
        $message = 'Test {numeric} Message';
        $context = ['numeric' => 1.9872222];
        $appender->append($message, $context);
        $expected = '1.9872222';
        \fseek($memStream, $pos);
        $actual = \stream_get_contents($memStream);
        $pos = ftell($memStream);
        $this->assertContains($expected, $actual);
        $message = 'Test {resource} Message';
        $context = ['resource' => $memStream];
        $appender->append($message, $context);
        $expected = '(stream)';
        \fseek($memStream, $pos);
        $actual = \stream_get_contents($memStream);
        $pos = ftell($memStream);
        $this->assertContains($expected, $actual);
        $message = 'Test {null} Message';
        $context = ['null' => NULL];
        $appender->append($message, $context);
        $expected = 'NULL_TOKEN';
        \fseek($memStream, $pos);
        $actual = \stream_get_contents($memStream);
        $pos = ftell($memStream);
        $this->assertContains($expected, $actual);
    }