/** * Filters based on category * @param \Nimbles\Core\Log\Entry $entry * @return bool */ public function apply(Entry $entry) { if (!is_string($this->getOption('category'))) { throw new Exception\InvalidCategory('Category must be specified'); } return $entry->getOption('category') === $this->getOption('category'); }
/** * Filters based on level * @param \Nimbles\Core\Log\Entry $entry * @throws \Nimbles\Core\Log\Filter\Exception\InvalidLevel */ public function apply(Entry $entry) { $levels = $this->getOption('level'); switch ($this->getOption('type')) { case self::LEVEL_ABOVE: if (!is_int($levels) || $levels < 0 || $levels > 7) { throw new Exception\InvalidLevel('Level must be a valid level between LOG_DEBUG and LOG_EMERG'); } return $entry->getOption('level') <= $this->getOption('level'); break; case self::LEVEL_BELOW: if (!is_int($levels) || $levels < 0 || $levels > 7) { throw new Exception\InvalidLevel('Level must be a valid level between LOG_DEBUG and LOG_EMERG'); } return $entry->getOption('level') >= $this->getOption('level'); break; case self::LEVEL_INCLUDE: case self::LEVEL_EXCLUDE: if (!is_array($levels) && !$levels instanceof \ArrayObject) { $levels = array($levels); } else { if ($levels instanceof \ArrayObject) { $levels = $levels->getArrayCopy(); } } sort($levels, SORT_NUMERIC); if (0 === count($levels)) { throw Exception\InvalidLevel('Levels must not be empty'); } else { if ($levels[0] < 0 || $levels[count($levels) - 1] > 7) { throw new Exception\InvalidLevel('Level must be a valid level between LOG_DEBUG and LOG_EMERG'); } } if (self::LEVEL_INCLUDE === $this->getOption('type') && in_array($entry->getOption('level'), $levels)) { return true; } if (self::LEVEL_EXCLUDE === $this->getOption('type') && !in_array($entry->getOption('level'), $levels)) { return true; } break; } return false; }
/** * Tests constructing a log entry from an array */ public function testConstructArray() { $ts = new \Nimbles\Core\DateTime(); $entry = new Log\Entry(array('message' => 'Hello world', 'timestamp' => $ts, 'extra' => 'test')); $this->assertEquals('Hello world', $entry->getOption('message')); $this->assertEquals(getmypid(), $entry->getOption('pid')); $this->assertEquals(LOG_INFO, $entry->getOption('level')); $this->assertEquals($ts, $entry->getOption('timestamp')); $this->assertNull($entry->getOption('category')); $this->assertEquals('test', $entry->getOption('extra')); }
/** * Gets an option formatted from an entry * @param \Nimbles\Core\Log\Entry $entry * @param string $option * @return null|string */ public function getFormattedOption(Entry $entry, $option) { $value = $entry->getOption($option); switch ($option) { case 'timestamp': if (!$value instanceof DateTime) { return null; } return $value->format(DateTime::ISO8601); break; case 'level': if (array_key_exists($value, $this->_levels)) { return $this->_levels[$value]; } break; case 'category': if (null === $value) { return 'uncategorised'; } break; } return $value; }
/** * Tests the construct of a simple formatter with no params * @return void */ public function testConstructNoParams() { $formatter = new \Nimbles\Core\Log\Formatter\Simple(); $this->assertEquals(sprintf('%s %d INFO - uncategorised - This is a test message', $this->_entry->getOption('timestamp')->format(\Nimbles\Core\DateTime::ISO8601), getmypid()), $formatter->format($this->_entry)); }