Skip to content

Commit

Permalink
Interpolates uses json_encode on array||object.
Browse files Browse the repository at this point in the history
  • Loading branch information
frqnck committed Oct 2, 2014
1 parent c99f00a commit e89468b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Logger/AbstractLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ public static function interpolate($message, array $context = array())
|| ( is_object($val) && method_exists($val, '__toString') )
) {
$val = (string) $val;
} elseif (is_object($val)) {
$val = '[object: ' . get_class($val) . ']';
} elseif (is_array($val) || is_object($val)) {
$val = @json_encode($val);
} else {
$val = '[type: ' . gettype($val) . ']';
}
Expand Down
27 changes: 21 additions & 6 deletions tests/Logger/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,42 @@ public function testContextOutput($key, $context, $exp)

public function providerContextes()
{
$obj = new \stdClass();
$obj->baz = 'biz';
$obj->nested = new \stdClass();
$obj->nested->buz ='bez';

return array(
array('bool1', true, '[bool: 1]'),
array('bool2', false, '[bool: 0]'),
array('string', 'Foo', 'Foo'),
array('int', 0, '0'),
array('float', 0.5, '0.5'),
array('__toString', new DummyTest(), '__toString...'),
// array('nested', array('with object' => new DummyTest), '[type: array]'),
array('object', new \DateTime(), '[object: DateTime]'),

array('resource', fopen('php://memory', 'r'), '[type: resource]'),
array('stdClass', new \stdClass(), '[object: stdClass]'),
array('null', null, '')
array('null', null, ''),

// objects
array('obj__toString', new DummyTest(), '__toString!'),
array('obj_stdClass', new \stdClass(), '{}'),
array('obj_instance', $obj, '{"baz":"biz","nested":{"buz":"bez"}}'),

// nested arrays...
array('nested_values', array('foo','bar'), '["foo","bar"]'),
array('nested_asso', array('foo'=>1,'bar'=>'2'), '{"foo":1,"bar":"2"}'),
array('nested_object', array(new DummyTest), '[{"foo":"bar"}]'),
array('nested_unicode', array('ƃol-xᴉdɐ'), '["\u0183ol-x\u1d09d\u0250"]'),
);
}

}

class DummyTest
{
public $foo = 'bar';
protected $foo2 = 'bar2';
public function __toString()
{
return '__toString...';
return '__toString!';
}
}

0 comments on commit e89468b

Please sign in to comment.