Browse Source

Adding special formatting of entities to ensure logging being pretty printed and with unescaped unicode characters.

Jan Zdunek 8 years ago
parent
commit
6ddadac246
2 changed files with 20 additions and 1 deletions
  1. 5 0
      src/Log/Engine/BaseLog.php
  2. 15 1
      tests/TestCase/Log/Engine/BaseLogTest.php

+ 5 - 0
src/Log/Engine/BaseLog.php

@@ -15,6 +15,7 @@
 namespace Cake\Log\Engine;
 
 use Cake\Core\InstanceConfigTrait;
+use Cake\Datasource\EntityInterface;
 use JsonSerializable;
 use Psr\Log\AbstractLogger;
 
@@ -95,6 +96,10 @@ abstract class BaseLog extends AbstractLogger
 
         $object = is_object($data);
 
+        if ($object && $data instanceof EntityInterface) {
+            return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
+        }
+
         if ($object && method_exists($data, '__toString')) {
             return (string)$data;
         }

+ 15 - 1
tests/TestCase/Log/Engine/BaseLogTest.php

@@ -95,7 +95,9 @@ class BaseLogTest extends TestCase
      */
     public function testLogUnicodeObjectToString()
     {
-        $stub = $this->createMock(Entity::class);
+        $stub = $this->getMockBuilder(\stdClass::class)
+            ->setMethods(['__toString'])
+            ->getMock();
         $stub->method('__toString')
             ->willReturn(implode($this->testData));
 
@@ -118,4 +120,16 @@ class BaseLogTest extends TestCase
 
         $this->assertUnescapedUnicode($this->testData, $logged);
     }
+
+    /**
+     * Tests the logging output of an entity with property value that contains unicode characters.
+     */
+    public function testLogUnicodeEntity()
+    {
+        $entity = new Entity(['foo' => implode($this->testData)]);
+
+        $logged = $this->logger->log(LogLevel::INFO, $entity);
+
+        $this->assertUnescapedUnicode($this->testData, $logged);
+    }
 }