Skip to content
Permalink
Browse files

Moving log formatting out of LogTrait.

This feature becomes limiting very easily, specially when having custom
loggers trying to format the message differently.
  • Loading branch information...
lorenzo committed Sep 20, 2014
1 parent df1e4e0 commit 2d4147f6d942712301b9811646e50ea7aa47aae5
@@ -15,6 +15,7 @@
namespace Cake\Log\Engine;
use Cake\Core\InstanceConfigTrait;
use JsonSerializable;
use Psr\Log\AbstractLogger;
/**
@@ -74,4 +75,31 @@ public function scopes() {
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);
}
}
@@ -86,6 +86,7 @@ public function __construct(array $config = array()) {
* @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);
}
@@ -116,6 +116,7 @@ public function __construct(array $config = []) {
* @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)) {
@@ -104,7 +104,7 @@ public function log($level, $message, array $context = []) {
$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);
@@ -312,7 +312,7 @@ public static function engine($name) {
*
* @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
@@ -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);
}
}
@@ -35,14 +35,14 @@ public function tearDown() {
* @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');

0 comments on commit 2d4147f

Please sign in to comment.
You can’t perform that action at this time.