Browse Source

Remove code and tests for non string log message.

$message argument of LoggerInterface::log() must be a string.
ADmad 6 years ago
parent
commit
fa745835a7

+ 6 - 25
src/Log/Engine/BaseLog.php

@@ -17,8 +17,6 @@ declare(strict_types=1);
 namespace Cake\Log\Engine;
 
 use Cake\Core\InstanceConfigTrait;
-use Cake\Datasource\EntityInterface;
-use JsonSerializable;
 use Psr\Log\AbstractLogger;
 
 /**
@@ -81,34 +79,17 @@ abstract class BaseLog extends AbstractLogger
     }
 
     /**
-     * Converts to string the provided data so it can be logged. The context
-     * can optionally be used by log engines to interpolate variables
+     * Formats the message to be logged.
+     *
+     * The context can optionally be used by log engines to interpolate variables
      * or add additional info to the logged message.
      *
-     * @param mixed $data The data to be converted to string and logged.
+     * @param string $message The message to be formatted.
      * @param array $context Additional logging information for the message.
      * @return string
      */
-    protected function _format($data, array $context = []): string
+    protected function _format(string $message, array $context = []): string
     {
-        if (is_string($data)) {
-            return $data;
-        }
-
-        $isObject = is_object($data);
-
-        if ($isObject && $data instanceof EntityInterface) {
-            return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
-        }
-
-        if ($isObject && method_exists($data, '__toString')) {
-            return (string)$data;
-        }
-
-        if ($isObject && $data instanceof JsonSerializable) {
-            return json_encode($data, JSON_UNESCAPED_UNICODE);
-        }
-
-        return print_r($data, true);
+        return $message;
     }
 }

+ 0 - 55
tests/TestCase/Log/Engine/BaseLogTest.php

@@ -15,7 +15,6 @@ declare(strict_types=1);
  */
 namespace Cake\Test\TestCase\Log\Engine;
 
-use Cake\ORM\Entity;
 use Cake\TestSuite\TestCase;
 use Psr\Log\LogLevel;
 use TestApp\Log\Engine\TestBaseLog;
@@ -57,58 +56,4 @@ class BaseLogTest extends TestCase
 
         $this->assertUnescapedUnicode($this->testData, $logged);
     }
-
-    /**
-     * Tests the logging output of an array containing unicode characters.
-     */
-    public function testLogUnicodeArray()
-    {
-        $logged = $this->logger->log(LogLevel::INFO, $this->testData);
-
-        $this->assertUnescapedUnicode($this->testData, $logged);
-    }
-
-    /**
-     * Tests the logging output of an object implementing __toString().
-     * Note: __toString() will return a single string containing unicode characters.
-     */
-    public function testLogUnicodeObjectToString()
-    {
-        $stub = $this->getMockBuilder(\stdClass::class)
-            ->setMethods(['__toString'])
-            ->getMock();
-        $stub->method('__toString')
-            ->willReturn(implode($this->testData));
-
-        $logged = $this->logger->log(LogLevel::INFO, $stub);
-
-        $this->assertUnescapedUnicode($this->testData, $logged);
-    }
-
-    /**
-     * Tests the logging output of an object implementing jsonSerializable().
-     * Note: jsonSerializable() will return an array containing unicode characters.
-     */
-    public function testLogUnicodeObjectJsonSerializable()
-    {
-        $stub = $this->createMock(\JsonSerializable::class);
-        $stub->method('jsonSerialize')
-            ->willReturn($this->testData);
-
-        $logged = $this->logger->log(LogLevel::INFO, $stub);
-
-        $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);
-    }
 }

+ 0 - 19
tests/TestCase/Log/Engine/FileLogTest.php

@@ -20,8 +20,6 @@ namespace Cake\Test\TestCase\Log\Engine;
 
 use Cake\Log\Engine\FileLog;
 use Cake\TestSuite\TestCase;
-use TestApp\Log\Object\JsonObject;
-use TestApp\Log\Object\StringObject;
 
 /**
  * FileLogTest class
@@ -55,23 +53,6 @@ class FileLogTest extends TestCase
 
         $result = file_get_contents(LOGS . 'random.log');
         $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Random: Test warning/', $result);
-
-        $object = new StringObject();
-        $log->log('debug', $object);
-        $this->assertFileExists(LOGS . 'debug.log');
-        $result = file_get_contents(LOGS . 'debug.log');
-        $this->assertStringContainsString('Debug: Hey!', $result);
-
-        $object = new JsonObject();
-        $log->log('debug', $object);
-        $this->assertFileExists(LOGS . 'debug.log');
-        $result = file_get_contents(LOGS . 'debug.log');
-        $this->assertStringContainsString('Debug: ' . json_encode(['hello' => 'world']), $result);
-
-        $log->log('debug', [1, 2]);
-        $this->assertFileExists(LOGS . 'debug.log');
-        $result = file_get_contents(LOGS . 'debug.log');
-        $this->assertStringContainsString('Debug: ' . print_r([1, 2], true), $result);
     }
 
     /**

+ 0 - 22
tests/test_app/TestApp/Log/Object/JsonObject.php

@@ -1,22 +0,0 @@
-<?php
-declare(strict_types=1);
-
-namespace TestApp\Log\Object;
-
-use JsonSerializable;
-
-/**
- * used for testing when an serializable is passed to a logger
- */
-class JsonObject implements JsonSerializable
-{
-    /**
-     * String representation of the object
-     *
-     * @return array
-     */
-    public function jsonSerialize()
-    {
-        return ['hello' => 'world'];
-    }
-}

+ 0 - 20
tests/test_app/TestApp/Log/Object/StringObject.php

@@ -1,20 +0,0 @@
-<?php
-declare(strict_types=1);
-
-namespace TestApp\Log\Object;
-
-/**
- * used for testing when an object is passed to a logger
- */
-class StringObject
-{
-    /**
-     * String representation of the object
-     *
-     * @return string
-     */
-    public function __toString()
-    {
-        return 'Hey!';
-    }
-}