コード例 #1
0
ファイル: Bunyan.php プロジェクト: zalora/punyan
 /**
  * @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;
 }
コード例 #2
0
ファイル: Logger.php プロジェクト: zalora/punyan
 /**
  * @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;
         }
     }
 }
コード例 #3
0
ファイル: NsTest.php プロジェクト: zalora/punyan
 /**
  * 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));
 }
コード例 #4
0
ファイル: Regexp.php プロジェクト: zalora/punyan
 /**
  * @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;
 }
コード例 #5
0
ファイル: RegexpTest.php プロジェクト: zalora/punyan
 /**
  * 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));
 }
コード例 #6
0
ファイル: PriorityTest.php プロジェクト: zalora/punyan
 /**
  * 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');
 }
コード例 #7
0
ファイル: AbstractWriterTest.php プロジェクト: zalora/punyan
 /**
  * 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));
 }
コード例 #8
0
ファイル: NoOpTest.php プロジェクト: zalora/punyan
 /**
  * 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]);
 }
コード例 #9
0
ファイル: BunyanTest.php プロジェクト: zalora/punyan
 /**
  * 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);
 }
コード例 #10
0
ファイル: NoWriterTest.php プロジェクト: zalora/punyan
 /**
  * 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));
 }
コード例 #11
0
ファイル: CallbackTest.php プロジェクト: zalora/punyan
 /**
  * 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));
 }
コード例 #12
0
ファイル: WebTest.php プロジェクト: zalora/punyan
 /**
  * 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]);
 }
コード例 #13
0
ファイル: NoFilterTest.php プロジェクト: zalora/punyan
 /**
  * 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));
 }
コード例 #14
0
ファイル: LoggerTest.php プロジェクト: zalora/punyan
 /**
  * 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));
 }