Esempio n. 1
0
 /**
  * @param LogEvent $logEvent
  * @return string
  */
 public function format(LogEvent $logEvent)
 {
     $logEvent->setHostname(gethostname());
     $logEvent->setPid(getmypid());
     $logEvent->setV(static::BUNYAN_VERSION);
     return json_encode($logEvent->getArrayCopy()) . PHP_EOL;
 }
Esempio n. 2
0
 /**
  * @param int $level
  * @param string $msg
  * @param array $context
  * @return void
  */
 public function log($level, $msg, array $context = array())
 {
     // Check for mute and existing writers
     if (count($this->writers) === 0 || $this->options['mute'] === true) {
         return;
     }
     if (!is_numeric($level) || $level <= 0) {
         throw new \InvalidArgumentException('Invalid log level, please choose one from interface ILogger');
     }
     $logEvent = LogEvent::create($level, $msg, $context, $this->appName, $this->exceptionHandler);
     // Check global filters
     $accept = true;
     /* @var $filter Filter\IFilter */
     foreach ($this->filters as $filter) {
         if (!($filter->accept($logEvent) && $accept)) {
             return;
         }
     }
     // Send log event to Writers (Threads would be cool here...)
     /* @var $writer IWriter */
     foreach ($this->writers as $writer) {
         $bubble = $writer->log($logEvent);
         if ($bubble === false) {
             break;
         }
     }
 }
Esempio n. 3
0
 /**
  * If no origin is provided, the filter returns false, this can e.g. happen if someone sets origin to false
  */
 public function testEmptyOrigin()
 {
     $config = json_decode(sprintf('{ "namespace": "%s" }', 'Zalora\\\\Punyan'), true);
     $filter = new Ns($config);
     $logEvent = LogEvent::create(ILogger::LEVEL_WARN, 'Hallo', array(), 'PHPUnit');
     $this->assertFalse($filter->accept($logEvent));
 }
Esempio n. 4
0
 /**
  * @param LogEvent $event
  * @return string
  */
 protected function getFieldValue(LogEvent $event)
 {
     $fields = explode('.', $this->field);
     $value = $event->getArrayCopy();
     for ($x = 0; $x < count($fields); $x++) {
         if (array_key_exists($fields[$x], $value)) {
             $value = $value[$fields[$x]];
             continue;
         }
         break;
     }
     if ($x < count($fields) || is_array($value) || is_object($value)) {
         return null;
     }
     return (string) $value;
 }
Esempio n. 5
0
 /**
  * The default field is msg, nested fields are named like 'levelone.leveltwo.levelthree'
  */
 public function testMatchingRegexpWithNestedFieldName()
 {
     $configCustomField = json_decode(sprintf($this->regexpConfigStub, '|^/|', 'proc.uri', 'false'), true);
     $context = array('proc' => array('uri' => '/info.php'));
     $logEvent = LogEvent::create(ILogger::LEVEL_WARN, 'Foo Bar', $context, 'PHPUnit');
     $regex = new Regexp($configCustomField);
     $this->assertTrue($regex->accept($logEvent));
 }
Esempio n. 6
0
 /**
  * Create some log events with different priorities
  */
 protected function setUp()
 {
     $this->filters[ILogger::LEVEL_TRACE] = LogEvent::create(ILogger::LEVEL_TRACE, '', array(), 'PHPUnit');
     $this->filters[ILogger::LEVEL_INFO] = LogEvent::create(ILogger::LEVEL_INFO, '', array(), 'PHPUnit');
     $this->filters[ILogger::LEVEL_WARN] = LogEvent::create(ILogger::LEVEL_WARN, '', array(), 'PHPUnit');
     $this->filters[ILogger::LEVEL_ERROR] = LogEvent::create(ILogger::LEVEL_ERROR, '', array(), 'PHPUnit');
     $this->filters[ILogger::LEVEL_FATAL] = LogEvent::create(ILogger::LEVEL_FATAL, '', array(), 'PHPUnit');
 }
Esempio n. 7
0
 /**
  * Using a writer with a blocking filter
  */
 public function testNoPassFilters()
 {
     $config = array('mute' => false, 'url' => 'php://memory', 'filters' => array(array('DiscoBouncer' => array())));
     $logEvent = LogEvent::create(ILogger::LEVEL_ERROR, 'Shut up', array(), 'PHPUnit');
     $writer = new Stream($config);
     $writer->log($logEvent);
     $stream = $writer->getStream();
     fseek($stream, 0);
     $this->assertEmpty(stream_get_contents($stream));
 }
Esempio n. 8
0
 /**
  * Run the process method and watch it do nothing
  */
 public function testProcess()
 {
     $configWithoutProcessor = array('url' => 'php://memory', 'filters' => array());
     $configWithProcessor = array('url' => 'php://memory', 'filters' => array(), 'processors' => array(array('NoOp' => array())));
     $logEvent = LogEvent::create(ILogger::LEVEL_INFO, 'Hello PHPUnit', array('time' => time()), 'PHPUnit');
     $writerWithoutProcessor = new Stream($configWithoutProcessor);
     $writerWithProcessor = new Stream($configWithProcessor);
     $writerWithoutProcessor->log($logEvent);
     $writerWithProcessor->log($logEvent);
     $output = array();
     $output[] = stream_get_contents($writerWithoutProcessor->getStream(), -1, 0);
     $output[] = stream_get_contents($writerWithProcessor->getStream(), -1, 0);
     // Prove that NoOp doesn't do shit
     $this->assertEquals($output[0], $output[1]);
 }
Esempio n. 9
0
 /**
  * Here some mandatory fields for the Bunyan format are added. The other fields
  * which make up the format are tested in the LogEvent test
  */
 public function testFormat()
 {
     $logEvent = LogEvent::create(ILogger::LEVEL_WARN, 'Hallo Test', array(), 'PHPUnit');
     $formatter = new Bunyan();
     $formattedString = $formatter->format($logEvent);
     // Must be well formed JSON
     $this->assertJson($formattedString);
     // Decode it and test if the fields the formatter added are present
     $logEventArray = json_decode($formattedString, true);
     $this->assertNotEmpty($logEventArray);
     $this->assertInternalType('array', $logEventArray);
     $this->assertArrayHasKey('hostname', $logEventArray);
     $this->assertArrayHasKey('pid', $logEventArray);
     $this->assertArrayHasKey('v', $logEventArray);
 }
Esempio n. 10
0
 /**
  * If bubbling is set to false, _write() must return false, otherwise true
  */
 public function testBubbling()
 {
     $configNoBubbling = array('bubble' => false, 'url' => 'php://memory', 'filters' => array());
     $configWithBubbling = array('bubble' => true, 'url' => 'php://memory', 'filters' => array());
     $configWithBubblingByDefault = array('url' => 'php://memory', 'filters' => array());
     $configWithFaultyBubbleSetting = array('bubble' => 'yeeha!', 'url' => 'php://memory', 'filters' => array());
     $logEvent = LogEvent::create(ILogger::LEVEL_ERROR, 'Bubble Bobble', array(), 'PHPUnit');
     $noBubbleWriter = new NoWriter($configNoBubbling);
     $configuredBubbleWriter = new NoWriter($configWithBubbling);
     $defaultBubbleWriter = new NoWriter($configWithBubblingByDefault);
     $wrongConfiguredBubbleWriter = new NoWriter($configWithFaultyBubbleSetting);
     $this->assertFalse($noBubbleWriter->log($logEvent));
     $this->assertTrue($configuredBubbleWriter->log($logEvent));
     $this->assertTrue($defaultBubbleWriter->log($logEvent));
     $this->assertTrue($wrongConfiguredBubbleWriter->log($logEvent));
 }
Esempio n. 11
0
 /**
  * Test working callbacks with all possible outcomes
  */
 public function testWorkingCallback()
 {
     $configTrue = json_decode(sprintf($this->callbackConfigStub, str_replace('\\', '\\\\', __CLASS__) . '::callbackReturningTrue'), true);
     $configFalse = json_decode(sprintf($this->callbackConfigStub, str_replace('\\', '\\\\', __CLASS__) . '::callbackReturningFalse'), true);
     $configNoParam = json_decode(sprintf($this->callbackConfigStub, str_replace('\\', '\\\\', __CLASS__) . '::callbackWithoutParameterReturningTrue'), true);
     $configNoParamNoReturn = json_decode(sprintf($this->callbackConfigStub, str_replace('\\', '\\\\', __CLASS__) . '::callbackWithoutParameterReturningVoid'), true);
     $logEvent = LogEvent::create(ILogger::LEVEL_WARN, '', array(), 'PHPUnit');
     $callbackFilterTrue = new Callback($configTrue);
     $callbackFilterFalse = new Callback($configFalse);
     $callbackFilterNoParam = new Callback($configNoParam);
     $callbackFilterNoParamNoReturn = new Callback($configNoParamNoReturn);
     $this->assertTrue($callbackFilterTrue->accept($logEvent));
     $this->assertFalse($callbackFilterFalse->accept($logEvent));
     $this->assertTrue($callbackFilterNoParam->accept($logEvent));
     $this->assertFalse($callbackFilterNoParamNoReturn->accept($logEvent));
 }
Esempio n. 12
0
 /**
  * Test onDemand flag
  */
 public function testProcessOnDemand()
 {
     $server['REQUEST_URI'] = '/test.php';
     $server['REQUEST_METHOD'] = 'HUSTLE';
     $server['SERVER_NAME'] = gethostname();
     $server['HTTP_REFERER'] = 'https://duckduckgo.com/?q=zalora+singapore';
     $logEvent = LogEvent::create(ILogger::LEVEL_INFO, 'Hello PHPUnit', array('time' => time()), 'PHPUnit');
     $processorOnDemandTrue = new Web(array('onDemand' => true));
     $processorOnDemandFalse = new Web(array('onDemand' => false));
     $processorOnDemandTrue->process($logEvent, $server);
     $this->assertArrayNotHasKey(IProcessor::PROCESSOR_DATA_KEY, $logEvent);
     $processorOnDemandFalse->process($logEvent, $server);
     $this->assertEquals($server['REQUEST_URI'], $logEvent[IProcessor::PROCESSOR_DATA_KEY][Web::PROCESSOR_KEY]['url']);
     $this->assertEquals($server['REQUEST_METHOD'], $logEvent[IProcessor::PROCESSOR_DATA_KEY][Web::PROCESSOR_KEY]['http_method']);
     $this->assertEquals($server['SERVER_NAME'], $logEvent[IProcessor::PROCESSOR_DATA_KEY][Web::PROCESSOR_KEY]['server']);
     $this->assertEquals($server['HTTP_REFERER'], $logEvent[IProcessor::PROCESSOR_DATA_KEY][Web::PROCESSOR_KEY]['referrer']);
     $this->assertArrayNotHasKey('ip', $logEvent[IProcessor::PROCESSOR_DATA_KEY][Web::PROCESSOR_KEY]);
 }
Esempio n. 13
0
 /**
  * Always return true, good for unit tests
  */
 public function testAccept()
 {
     $logEvent = LogEvent::create(ILogger::LEVEL_WARN, '', array(), 'PHPUnit');
     $noFilter = new NoFilter(array());
     $this->assertTrue($noFilter->accept($logEvent));
 }
Esempio n. 14
0
 /**
  * Test the fatal method
  */
 public function testFatal()
 {
     $logger = $this->getMemoryLogger();
     $time = time();
     $stream = $this->getCurrentStreamFromLogger($logger);
     $formatter = new Bunyan();
     $logEvent = LogEvent::create(ILogger::LEVEL_FATAL, 'PHPUnit', array('time' => $time), 'PHPUnit');
     $logEvent['origin'] = Logger::getLogOrigin(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
     $logger->fatal('PHPUnit', array('time' => $time));
     $streamContent = stream_get_contents($stream, -1, 0);
     $this->assertEquals(json_decode($formatter->format($logEvent), true), json_decode($streamContent, true));
 }