Browse Source

Moving log formatting out of LogTrait.

This feature becomes limiting very easily, specially when having custom
loggers trying to format the message differently.
Jose Lorenzo Rodriguez 11 years ago
parent
commit
2d4147f6d9

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

@@ -15,6 +15,7 @@
 namespace Cake\Log\Engine;
 
 use Cake\Core\InstanceConfigTrait;
+use JsonSerializable;
 use Psr\Log\AbstractLogger;
 
 /**
@@ -74,4 +75,31 @@ abstract class BaseLog extends AbstractLogger {
 		return $this->_config['scopes'];
 	}
 
+/**
+ * Converts to string the provided data so it can 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 array $context Additional logging information for the message.
+ * @return string
+ */
+	protected function _format($data, array $context = []) {
+		if (is_string($data)) {
+			return $data;
+		}
+
+		$object = is_object($data);
+
+		if ($object && method_exists('__toString', $data)) {
+			return (string)$data;
+		}
+
+		if ($object && $object instanceof JsonSerializable) {
+			return json_encode($data);
+		}
+
+		return print_r($data, true);
+	}
+
 }

+ 1 - 0
src/Log/Engine/ConsoleLog.php

@@ -86,6 +86,7 @@ class ConsoleLog extends BaseLog {
  * @return bool success of write.
  */
 	public function log($level, $message, array $context = []) {
+		$message = $this->_format($message, $context);
 		$output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message . "\n";
 		return $this->_output->write(sprintf('<%s>%s</%s>', $level, $output, $level), false);
 	}

+ 1 - 0
src/Log/Engine/FileLog.php

@@ -116,6 +116,7 @@ class FileLog extends BaseLog {
  * @return bool success of write.
  */
 	public function log($level, $message, array $context = []) {
+		$message = $this->_format($message, $context);
 		$output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message . "\n";
 		$filename = $this->_getFilename($level);
 		if (!empty($this->_size)) {

+ 1 - 1
src/Log/Engine/SyslogLog.php

@@ -104,7 +104,7 @@ class SyslogLog extends BaseLog {
 			$priority = $this->_levelMap[$level];
 		}
 
-		$messages = explode("\n", $message);
+		$messages = explode("\n", $this->_format($message, $context));
 		foreach ($messages as $message) {
 			$message = sprintf($this->_config['format'], $level, $message);
 			$this->_write($priority, $message);

+ 1 - 1
src/Log/Log.php

@@ -312,7 +312,7 @@ class Log {
  *
  * @param int|string $level The severity level of the message being written.
  *    The value must be an integer or string matching a known level.
- * @param string $message Message content to log
+ * @param mixed $message Message content to log
  * @param string|array $scope The scope(s) a log message is being created in.
  *    See Cake\Log\Log::config() for more information on logging scopes.
  * @return bool Success

+ 6 - 7
src/Log/LogTrait.php

@@ -13,6 +13,8 @@
  */
 namespace Cake\Log;
 
+use Psr\Log\LogLevel;
+
 /**
  * A trait providing an object short-cut method
  * to logging.
@@ -23,16 +25,13 @@ trait LogTrait {
  * Convenience method to write a message to Log. See Log::write()
  * for more information on writing to logs.
  *
- * @param string $msg Log message.
+ * @param mixed $msg Log message.
  * @param int|string $level Error level.
- * @param string|array $scope The name of the log scope.
+ * @param string|array $context Additional log data relevant to this message.
  * @return bool Success of log write.
  */
-	public function log($msg, $level = LOG_ERR, $scope = []) {
-		if (!is_string($msg)) {
-			$msg = print_r($msg, true);
-		}
-		return Log::write($level, $msg, $scope);
+	public function log($msg, $level = LogLevel::ERROR, $context = []) {
+		return Log::write($level, $msg, $context);
 	}
 
 }

+ 2 - 2
tests/TestCase/Log/LogTraitTest.php

@@ -35,14 +35,14 @@ class LogTraitTest extends TestCase {
  * @return void
  */
 	public function testLog() {
-		$mock = $this->getMock('Cake\Log\LogInterface');
+		$mock = $this->getMock('Psr\Log\LoggerInterface');
 		$mock->expects($this->at(0))
 			->method('log')
 			->with('error', 'Testing');
 
 		$mock->expects($this->at(1))
 			->method('log')
-			->with('debug', print_r(array(1, 2), true));
+			->with('debug',array(1, 2));
 
 		Log::config('trait_test', ['engine' => $mock]);
 		$subject = $this->getObjectForTrait('Cake\Log\LogTrait');