Permalink
Browse files

Merge pull request #885 from markstory/3.0-log-trait

Extract LogTrait (3.0)
  • Loading branch information...
2 parents 976c648 + dbcc0c1 commit daf3e7a63cd8aa6afa0928fcfd5fefd2e5664adf @markstory markstory committed Oct 14, 2012
Showing with 97 additions and 16 deletions.
  1. +3 −16 lib/Cake/Core/Object.php
  2. +39 −0 lib/Cake/Log/LogTrait.php
  3. +55 −0 lib/Cake/Test/TestCase/Log/LogTraitTest.php
View
19 lib/Cake/Core/Object.php
@@ -13,7 +13,8 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Core;
-use Cake\Log\Log;
+
+use Cake\Log\LogTrait;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\Routing\Dispatcher;
@@ -30,6 +31,7 @@
*/
class Object {
+ use LogTrait;
/**
* constructor, no-op
*
@@ -155,21 +157,6 @@ protected function _stop($status = 0) {
}
/**
- * Convenience method to write a message to Log. See Log::write()
- * for more information on writing to logs.
- *
- * @param string $msg Log message
- * @param integer $type Error type constant. Defined in app/Config/core.php.
- * @return boolean Success of log write
- */
- public function log($msg, $type = LOG_ERR) {
- if (!is_string($msg)) {
- $msg = print_r($msg, true);
- }
- return Log::write($type, $msg);
- }
-
-/**
* Allows setting of multiple properties of the object in a single line of code. Will only set
* properties that are part of a class declaration.
*
View
39 lib/Cake/Log/LogTrait.php
@@ -0,0 +1,39 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @since CakePHP(tm) v 3.0.0
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+namespace Cake\Log;
+
+/**
+ * A trait providing an object short-cut method
+ * to logging.
+ *
+ * @package Cake.Log
+ */
+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 integer $type Error type constant. Defined in app/Config/logging.php.
+ * @return boolean Success of log write
+ */
+ public function log($msg, $type = LOG_ERR) {
+ if (!is_string($msg)) {
+ $msg = print_r($msg, true);
+ }
+ return Log::write($type, $msg);
+ }
+
+}
View
55 lib/Cake/Test/TestCase/Log/LogTraitTest.php
@@ -0,0 +1,55 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @since CakePHP(tm) v 3.0.0
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+namespace Cake\Test\TestCase\Log;
+
+use Cake\Log\Log;
+use Cake\Log\LogTrait;
+use Cake\Log\LogInterface;
+use Cake\TestSuite\TestCase;
+
+/**
+ * Test case for LogTrait
+ *
+ * @package Cake.Test.TestCase.Log
+ */
+class LogTraitTest extends TestCase {
+
+ public function tearDown() {
+ parent::tearDown();
+ Log::drop('trait_test');
+ }
+
+/**
+ * Test log method.
+ *
+ * @return void
+ */
+ public function testLog() {
+ $mock = $this->getMock('Cake\Log\LogInterface');
+ $mock->expects($this->at(0))
+ ->method('write')
+ ->with('error', 'Testing');
+
+ $mock->expects($this->at(1))
+ ->method('write')
+ ->with('debug', print_r(array(1, 2), true));
+
+ Log::engine('trait_test', $mock);
+ $subject = $this->getObjectForTrait('Cake\Log\LogTrait');
+
+ $subject->log('Testing');
+ $subject->log(array(1, 2), 'debug');
+ }
+
+}

0 comments on commit daf3e7a

Please sign in to comment.