示例#1
0
文件: BaseTest.php 项目: nuklehed/zf2
 public function testFormatAllTypes()
 {
     $datetime = new DateTime();
     $object = new stdClass();
     $object->foo = 'bar';
     $formatter = new BaseFormatter();
     $event = array('timestamp' => $datetime, 'priority' => 1, 'message' => 'tottakai', 'extra' => array('float' => 0.2, 'boolean' => false, 'array_empty' => array(), 'array' => range(0, 4), 'traversable_empty' => new EmptyIterator(), 'traversable' => new ArrayIterator(array('id', 42)), 'null' => null, 'object_empty' => new stdClass(), 'object' => $object, 'string object' => new StringObject(), 'resource' => fopen('php://stdout', 'w')));
     $outputExpected = array('timestamp' => $datetime->format($formatter->getDateTimeFormat()), 'priority' => 1, 'message' => 'tottakai', 'extra' => array('boolean' => false, 'float' => 0.2, 'array_empty' => '[]', 'array' => '[0,1,2,3,4]', 'traversable_empty' => '[]', 'traversable' => '["id",42]', 'null' => null, 'object_empty' => 'object(stdClass) {}', 'object' => 'object(stdClass) {"foo":"bar"}', 'string object' => 'Hello World', 'resource' => 'resource(stream)'));
     $this->assertEquals($outputExpected, $formatter->format($event));
 }
示例#2
0
 /**
  * Class constructor
  *
  * @see http://php.net/manual/en/function.date.php
  * @param null|string $format Format specifier for log messages
  * @param null|string $dateTimeFormat Format specifier for DateTime objects in event data
  * @throws Exception\InvalidArgumentException
  */
 public function __construct($format = null, $dateTimeFormat = null)
 {
     if ($format instanceof \Traversable) {
         $format = iterator_to_array($format);
     }
     if (is_array($format)) {
         $dateTimeFormat = isset($format['dateTimeFormat']) ? $format['dateTimeFormat'] : null;
         $format = isset($format['format']) ? $format['format'] : null;
     }
     if (isset($format) && !is_string($format)) {
         throw new Exception\InvalidArgumentException('Format must be a string');
     }
     $this->format = isset($format) ? $format : static::DEFAULT_FORMAT;
     parent::__construct($dateTimeFormat);
 }
示例#3
0
 /**
  * Formats data into a single line to be written by the writer.
  *
  * @param array $event event data
  * @return string formatted line to write to the log
  */
 public function format($event)
 {
     $output = $this->format;
     $event = parent::format($event);
     foreach ($event as $name => $value) {
         if ('extra' == $name && count($value)) {
             $value = $this->normalize($value);
         } elseif ('extra' == $name) {
             // Don't print an empty array
             $value = '';
         }
         $output = str_replace("%{$name}%", $value, $output);
     }
     if (isset($event['extra']) && empty($event['extra']) && false !== strpos($this->format, '%extra%')) {
         $output = rtrim($output, ' ');
     }
     return $output;
 }
示例#4
0
 public function testFormatExtraArrayKeyWithNonArrayValue()
 {
     $formatter = new BaseFormatter();
     $event = array('message' => 'Hi', 'extra' => '');
     $outputExpected = array('message' => 'Hi', 'extra' => '');
     $this->assertEquals($outputExpected, $formatter->format($event));
 }