From f1b815a9bbc9d58e09da9bab4f2110c643baa814 Mon Sep 17 00:00:00 2001 From: Jad Bitar Date: Fri, 15 Feb 2013 02:46:10 +0200 Subject: [PATCH 1/3] Add scope to email logging. --- lib/Cake/Network/Email/CakeEmail.php | 9 ++++-- .../Test/Case/Network/Email/CakeEmailTest.php | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index 92446ff25..ade7921f8 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -1066,10 +1066,15 @@ class CakeEmail { $contents = $this->transportClass()->send($this); if (!empty($this->_config['log'])) { $level = LOG_DEBUG; + $scope = 'email'; if ($this->_config['log'] !== true) { - $level = $this->_config['log']; + if (!is_array($this->_config['log'])) { + $this->_config['log'] = array('level' => $this->_config['log']); + } + $this->_config['log'] = array_merge(compact('level', 'scope'), $this->_config['log']); + extract($this->_config['log']); } - CakeLog::write($level, PHP_EOL . $contents['headers'] . PHP_EOL . $contents['message']); + CakeLog::write($level, PHP_EOL . $contents['headers'] . PHP_EOL . $contents['message'], $scope); } return $contents; } diff --git a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php index e76b63750..89c2a23a9 100644 --- a/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php +++ b/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php @@ -1013,6 +1013,35 @@ class CakeEmailTest extends CakeTestCase { CakeLog::drop('email'); } +/** + * testSendWithLogAndScope method + * + * @return void + */ + public function testSendWithLogAndScope() { + CakeLog::config('email', array( + 'engine' => 'FileLog', + 'path' => TMP, + 'types' => array('cake_test_emails'), + 'scopes' => array('email') + )); + CakeLog::drop('default'); + $this->CakeEmail->transport('Debug'); + $this->CakeEmail->to('me@cakephp.org'); + $this->CakeEmail->from('cake@cakephp.org'); + $this->CakeEmail->subject('My title'); + $this->CakeEmail->config(array('log' => array('level' => 'cake_test_emails', 'scope' => 'email'))); + $result = $this->CakeEmail->send("Logging This"); + + App::uses('File', 'Utility'); + $File = new File(TMP . 'cake_test_emails.log'); + $log = $File->read(); + $this->assertTrue(strpos($log, $result['headers']) !== false); + $this->assertTrue(strpos($log, $result['message']) !== false); + $File->delete(); + CakeLog::drop('email'); + } + /** * testSendRender method * From 8eb5ce410aa5419482b3e587ccc8a69b4982f77c Mon Sep 17 00:00:00 2001 From: Jad Bitar Date: Fri, 15 Feb 2013 02:48:38 +0200 Subject: [PATCH 2/3] Add scopes to `Object::log()` signature. --- lib/Cake/Core/Object.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Core/Object.php b/lib/Cake/Core/Object.php index 2e712d080..dc69b3262 100644 --- a/lib/Cake/Core/Object.php +++ b/lib/Cake/Core/Object.php @@ -15,6 +15,7 @@ */ App::uses('Set', 'Utility'); +App::uses('CakeLog', 'Log'); /** * Object class provides a few generic methods used in several subclasses. @@ -151,12 +152,12 @@ class Object { * @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) { - App::uses('CakeLog', 'Log'); + public function log($msg, $type = LOG_ERR, $scope = null) { if (!is_string($msg)) { $msg = print_r($msg, true); } - return CakeLog::write($type, $msg); + + return CakeLog::write($type, $msg, $scope); } /** From 47526e8b4d1134f261182998f9fb077b4e68b6a4 Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 1 Mar 2013 22:34:00 -0500 Subject: [PATCH 3/3] Refactor code to not use extract()/compact() --- lib/Cake/Network/Email/CakeEmail.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index ade7921f8..7e5460cc5 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -1065,16 +1065,21 @@ class CakeEmail { $contents = $this->transportClass()->send($this); if (!empty($this->_config['log'])) { - $level = LOG_DEBUG; - $scope = 'email'; + $config = array( + 'level' => LOG_DEBUG, + 'scope' => 'email' + ); if ($this->_config['log'] !== true) { if (!is_array($this->_config['log'])) { $this->_config['log'] = array('level' => $this->_config['log']); } - $this->_config['log'] = array_merge(compact('level', 'scope'), $this->_config['log']); - extract($this->_config['log']); + $config = array_merge($config, $this->_config['log']); } - CakeLog::write($level, PHP_EOL . $contents['headers'] . PHP_EOL . $contents['message'], $scope); + CakeLog::write( + $config['level'], + PHP_EOL . $contents['headers'] . PHP_EOL . $contents['message'], + $config['scope'] + ); } return $contents; }