Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function format(array $record)
 {
     $vars = parent::format($record);
     $output = $this->format;
     foreach ($vars['extra'] as $var => $val) {
         if (false !== strpos($output, '%extra.' . $var . '%')) {
             $output = str_replace('%extra.' . $var . '%', $this->stringify($val), $output);
             unset($vars['extra'][$var]);
         }
     }
     foreach ($vars['context'] as $var => $val) {
         if (false !== strpos($output, '%context.' . $var . '%')) {
             $output = str_replace('%context.' . $var . '%', $this->stringify($val), $output);
             unset($vars['context'][$var]);
         }
     }
     if ($this->ignoreEmptyContextAndExtra) {
         if (empty($vars['context'])) {
             unset($vars['context']);
             $output = str_replace('%context%', '', $output);
         }
         if (empty($vars['extra'])) {
             unset($vars['extra']);
             $output = str_replace('%extra%', '', $output);
         }
     }
     foreach ($vars as $var => $val) {
         if (false !== strpos($output, '%' . $var . '%')) {
             $output = str_replace('%' . $var . '%', $this->stringify($val), $output);
         }
     }
     return $output;
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function format(array $record) : string
 {
     $record = parent::format($record);
     if (empty($record['datetime'])) {
         $record['datetime'] = gmdate('c');
     }
     $message = ['@timestamp' => $record['datetime'], '@version' => 1, 'host' => $this->systemName];
     if (isset($record['message'])) {
         $message['message'] = $record['message'];
     }
     if (isset($record['channel'])) {
         $message['type'] = $record['channel'];
         $message['channel'] = $record['channel'];
     }
     if (isset($record['level_name'])) {
         $message['level'] = $record['level_name'];
     }
     if ($this->applicationName) {
         $message['type'] = $this->applicationName;
     }
     if (!empty($record['extra'])) {
         foreach ($record['extra'] as $key => $val) {
             $message[$this->extraPrefix . $key] = $val;
         }
     }
     if (!empty($record['context'])) {
         foreach ($record['context'] as $key => $val) {
             $message[$this->contextPrefix . $key] = $val;
         }
     }
     return $this->toJson($message) . "\n";
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function format(array $record)
 {
     $record = parent::format($record);
     if ($this->version === self::V1) {
         $message = $this->formatV1($record);
     } else {
         $message = $this->formatV0($record);
     }
     return $this->toJson($message) . "\n";
 }
Esempio n. 4
0
 public function testFormatExceptions()
 {
     $formatter = new NormalizerFormatter('Y-m-d');
     $e = new \LogicException('bar');
     $e2 = new \RuntimeException('foo', 0, $e);
     $formatted = $formatter->format(array('exception' => $e2));
     $this->assertGreaterThan(5, count($formatted['exception']['trace']));
     $this->assertTrue(isset($formatted['exception']['previous']));
     unset($formatted['exception']['trace'], $formatted['exception']['previous']);
     $this->assertEquals(array('exception' => array('class' => get_class($e2), 'message' => $e2->getMessage(), 'file' => $e2->getFile() . ':' . $e2->getLine())), $formatted);
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function format(array $record)
 {
     $vars = parent::format($record);
     $output = $this->format;
     foreach ($vars['extra'] as $var => $val) {
         if (false !== strpos($output, '%extra.' . $var . '%')) {
             $output = str_replace('%extra.' . $var . '%', $this->convertToString($val), $output);
             unset($vars['extra'][$var]);
         }
     }
     foreach ($vars as $var => $val) {
         $output = str_replace('%' . $var . '%', $this->convertToString($val), $output);
     }
     return $output;
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function format(array $record)
 {
     $vars = parent::format($record);
     $output = $this->format;
     foreach ($vars['extra'] as $var => $val) {
         if (false !== strpos($output, '%extra.' . $var . '%')) {
             $output = str_replace('%extra.' . $var . '%', $this->stringify($val), $output);
             unset($vars['extra'][$var]);
         }
     }
     foreach ($vars['context'] as $var => $val) {
         if (false !== strpos($output, '%context.' . $var . '%')) {
             $output = str_replace('%context.' . $var . '%', $this->stringify($val), $output);
             unset($vars['context'][$var]);
         }
     }
     if ($this->ignoreEmptyContextAndExtra) {
         if (empty($vars['context'])) {
             unset($vars['context']);
             $output = str_replace('%context%', '', $output);
         }
         if (empty($vars['extra'])) {
             unset($vars['extra']);
             $output = str_replace('%extra%', '', $output);
         }
     }
     foreach ($vars as $var => $val) {
         if (false !== strpos($output, '%' . $var . '%')) {
             $output = str_replace('%' . $var . '%', $this->stringify($val), $output);
         }
     }
     // remove leftover %extra.xxx% and %context.xxx% if any
     if (false !== strpos($output, '%')) {
         $output = preg_replace('/%(?:extra|context)\\..+?%/', '', $output);
     }
     return $output;
 }
Esempio n. 7
0
 /**
  *
  * {@inheritdoc}
  *
  */
 public function format(array $record)
 {
     $record = parent::format($record);
     $message = array('@timestamp' => $record['datetime'], '@message' => $record['message'], '@tags' => array($record['channel']), '@source' => $this->systemName);
     if ($this->applicationName) {
         $message['@type'] = $this->applicationName;
     }
     $message['@fields'] = array();
     $message['@fields']['channel'] = $record['channel'];
     $message['@fields']['level'] = $record['level'];
     if (isset($record['extra']['server'])) {
         $message['@source_host'] = $record['extra']['server'];
     }
     if (isset($record['extra']['url'])) {
         $message['@source_path'] = $record['extra']['url'];
     }
     foreach ($record['extra'] as $key => $val) {
         $message['@fields'][$this->extraPrefix . $key] = $val;
     }
     foreach ($record['context'] as $key => $val) {
         $message['@fields'][$this->contextPrefix . $key] = $val;
     }
     return json_encode($message) . "\n";
 }
Esempio n. 8
0
 public function testExceptionTraceWithArgs()
 {
     if (defined('HHVM_VERSION')) {
         $this->markTestSkipped('Not supported in HHVM since it detects errors differently');
     }
     // This happens i.e. in React promises or Guzzle streams where stream wrappers are registered
     // and no file or line are included in the trace because it's treated as internal function
     set_error_handler(function ($errno, $errstr, $errfile, $errline) {
         throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
     });
     try {
         // This will contain $resource and $wrappedResource as arguments in the trace item
         $resource = fopen('php://memory', 'rw+');
         fwrite($resource, 'test_resource');
         $wrappedResource = new TestStreamFoo($resource);
         // Just do something stupid with a resource/wrapped resource as argument
         array_keys($wrappedResource);
     } catch (\Exception $e) {
         restore_error_handler();
     }
     $formatter = new NormalizerFormatter();
     $record = array('context' => array('exception' => $e));
     $result = $formatter->format($record);
     $this->assertRegExp('%"resource":"\\[resource\\]"%', $result['context']['exception']['trace'][0]);
     if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
         $pattern = '%"wrappedResource":"\\[object\\] \\(Monolog\\\\\\\\Formatter\\\\\\\\TestStreamFoo: \\)"%';
     } else {
         $pattern = '%\\\\"resource\\\\":null%';
     }
     // Tests that the wrapped resource is ignored while encoding, only works for PHP <= 5.4
     $this->assertRegExp($pattern, $result['context']['exception']['trace'][0]);
 }
 public function testFormat()
 {
     $formatter = new NormalizerFormatter('Y-m-d');
     $formatted = $formatter->format(array('level_name' => 'ERROR', 'channel' => 'meh', 'message' => 'foo', 'datetime' => new \DateTime(), 'extra' => array('foo' => new TestFooNorm(), 'bar' => new TestBarNorm(), 'baz' => array(), 'res' => fopen('php://memory', 'rb')), 'context' => array('foo' => 'bar', 'baz' => 'qux')));
     $this->assertEquals(array('level_name' => 'ERROR', 'channel' => 'meh', 'message' => 'foo', 'datetime' => date('Y-m-d'), 'extra' => array('foo' => '[object] (Monolog\\Formatter\\TestFooNorm: {"foo":"foo"})', 'bar' => '[object] (Monolog\\Formatter\\TestBarNorm: {})', 'baz' => array(), 'res' => '[resource]'), 'context' => array('foo' => 'bar', 'baz' => 'qux')), $formatted);
 }