logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ihabu...@apache.org
Subject [2/3] git commit: LOG4PHP-144: LoggerAppenderMail should set Content-type header
Date Mon, 31 Dec 2012 10:56:33 GMT
LOG4PHP-144: LoggerAppenderMail should set Content-type header

Modified appender to set the Content-type header which it retrieves
from the layout.

Also improved tests, and removed the dry mode testing. It's better to get
the appender to actually send an email. For this I used the
log4php@gmail.com address which is accessible by the Logging Services PMC.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4php/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4php/commit/ae11f080
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4php/tree/ae11f080
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4php/diff/ae11f080

Branch: refs/heads/develop
Commit: ae11f080a47f0580750013cc10f1efb6c2764adb
Parents: 81ab4e1
Author: Ivan Habunek <ihabunek@apache.org>
Authored: Mon Dec 31 11:39:15 2012 +0100
Committer: Ivan Habunek <ihabunek@apache.org>
Committed: Mon Dec 31 11:47:53 2012 +0100

----------------------------------------------------------------------
 src/changes/changes.xml                           |    3 +-
 src/main/php/appenders/LoggerAppenderMail.php     |   13 ++-
 src/test/php/appenders/LoggerAppenderMailTest.php |   62 +++++++++-------
 3 files changed, 45 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/ae11f080/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 0d60b44..03eabc2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -20,7 +20,8 @@
 		<title>Apache log4php changelog</title>
 	</properties>
 	<body>
-		<release version="2.3.1" date="SVN">
+		<release version="develop">
+			<action date="2012-12-31" type="update" issue="LOG4PHP-144" dev="Ivan Habunek">Improved
LoggerAppenderMail to set the Content-type header as defined in layout.</action>
 		</release>
 		<release version="2.3.0" date="2012-10-13">
 			<action date="2012-10-07" type="fix" issue="LOG4PHP-163" dev="Ivan Habunek" due-to="Daniel
Wong" due-to-email="dan at dsmwong dot com">LoggerPatternConverter formats max incorrectly</action>

http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/ae11f080/src/main/php/appenders/LoggerAppenderMail.php
----------------------------------------------------------------------
diff --git a/src/main/php/appenders/LoggerAppenderMail.php b/src/main/php/appenders/LoggerAppenderMail.php
index 9e34b40..29d88c2 100644
--- a/src/main/php/appenders/LoggerAppenderMail.php
+++ b/src/main/php/appenders/LoggerAppenderMail.php
@@ -84,12 +84,15 @@ class LoggerAppenderMail extends LoggerAppender {
 			$to = $this->to;
 	
 			if(!empty($this->body) and $from !== null and $to !== null and $this->layout !==
null) {
-				$subject = $this->subject;
 				if(!$this->dry) {
-					mail(
-						$to, $subject, 
-						$this->layout->getHeader() . $this->body . $this->layout->getFooter(),
-						"From: {$from}\r\n");
+					$message = $this->layout->getHeader() . $this->body . $this->layout->getFooter();
+					$subject = $this->subject;
+					$contentType = $this->layout->getContentType();
+					
+					$headers = "From: {$from}\r\n";
+					$headers .= "Content-Type: {$contentType}\r\n";
+					
+					mail($to, $subject, $message, $headers);
 				} else {
 				    echo "DRY MODE OF MAIL APP.: Send mail to: ".$to." with content: ".$this->body;
 				}

http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/ae11f080/src/test/php/appenders/LoggerAppenderMailTest.php
----------------------------------------------------------------------
diff --git a/src/test/php/appenders/LoggerAppenderMailTest.php b/src/test/php/appenders/LoggerAppenderMailTest.php
index 8dffd8c..a08c4c2 100644
--- a/src/test/php/appenders/LoggerAppenderMailTest.php
+++ b/src/test/php/appenders/LoggerAppenderMailTest.php
@@ -6,16 +6,16 @@
  * The ASF licenses this file to You under the Apache License, Version 2.0
  * (the "License"); you may not use this file except in compliance with
  * the License.  You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- * 
- * @category   tests   
+ *
+ * @category   tests
  * @package    log4php
  * @subpackage appenders
  * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
@@ -26,34 +26,42 @@
  * @group appenders
  */
 class LoggerAppenderMailTest extends PHPUnit_Framework_TestCase {
-        
+
 	public function testRequiresLayout() {
-		$appender = new LoggerAppenderMail(); 
+		$appender = new LoggerAppenderMail();
 		self::assertTrue($appender->requiresLayout());
 	}
-	
+
 	public function testMail() {
-		$appender = new LoggerAppenderMail("myname ");
-		
-		$layout = new LoggerLayoutSimple();
-		$appender->setLayout($layout);
-		$appender->setDry(true);
-		$appender->setTo('test@example.com');
-		$appender->setFrom('Testsender');
-		
+		$appender = new LoggerAppenderMail("testAppender");
+		$appender->setTo('log4php@gmail.com');
+		$appender->setFrom('log4php@localhost');
+		$appender->setSubject("Testing text/plain " . date('Y-m-d H:i:s'));
 		$appender->activateOptions();
-		$event = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelError(),
"testmessage");
-		$event2 = new LoggerLoggingEvent("LoggerAppenderEchoTest", new Logger("TEST"), LoggerLevel::getLevelError(),
"testmessage2");
-		 
-		ob_start();
-		$appender->append($event);
-		$appender->append($event2);
+
+		$appender->append(LoggerTestHelper::getTraceEvent('tracing'));
+		$appender->append(LoggerTestHelper::getDebugEvent('debugging'));
+		$appender->append(LoggerTestHelper::getInfoEvent('informing'));
+		$appender->append(LoggerTestHelper::getWarnEvent('warning'));
+		$appender->append(LoggerTestHelper::getErrorEvent('erring'));
+		$appender->append(LoggerTestHelper::getFatalEvent('fatality!'));
 		$appender->close();
-		$v = ob_get_contents();
-		ob_end_clean();
+	}
 
-		$e = "DRY MODE OF MAIL APP.: Send mail to: test@example.com with content: ERROR - testmessage".PHP_EOL."ERROR
- testmessage2".PHP_EOL;
-		self::assertEquals($e, $v);
-    }
-    
+	public function testMailHTML() {
+		$appender = new LoggerAppenderMail("testAppender");
+		$appender->setLayout(new LoggerLayoutHtml());
+		$appender->setTo('log4php@gmail.com');
+		$appender->setFrom('log4php@localhost');
+		$appender->setSubject("Testing text/html " . date('Y-m-d H:i:s'));
+		$appender->activateOptions();
+
+		$appender->append(LoggerTestHelper::getTraceEvent('tracing'));
+		$appender->append(LoggerTestHelper::getDebugEvent('debugging'));
+		$appender->append(LoggerTestHelper::getInfoEvent('informing'));
+		$appender->append(LoggerTestHelper::getWarnEvent('warning'));
+		$appender->append(LoggerTestHelper::getErrorEvent('erring'));
+		$appender->append(LoggerTestHelper::getFatalEvent('fatality!'));
+		$appender->close();
+	}
 }


Mime
View raw message