logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ihabu...@apache.org
Subject git commit: LOG4PHP-153: Added bufferSize parameter to LoggerAppenderMail
Date Wed, 02 Jan 2013 07:41:23 GMT
Updated Branches:
  refs/heads/LOG4PHP-153 [created] a513de009


LOG4PHP-153: Added bufferSize parameter to LoggerAppenderMail

This parameter determines how many messages are buffered before the buffer
is sent to the recipient. If bufferSize is not set, the appender will send
all messages together on close (same as prior behaviour, so it remains
backward compatible).

Based on patch by Michal Vanek (see JIRA issue).

Signed-off-by: Ivan Habunek <ihabunek@apache.org>


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

Branch: refs/heads/LOG4PHP-153
Commit: a513de009eb0553e976273bf6531f29d15373d63
Parents: 63e20f6
Author: Ivan Habunek <ihabunek@apache.org>
Authored: Wed Jan 2 08:36:40 2013 +0100
Committer: Ivan Habunek <ihabunek@apache.org>
Committed: Wed Jan 2 08:36:40 2013 +0100

----------------------------------------------------------------------
 src/main/php/appenders/LoggerAppenderMail.php |   50 +++++++++++++++++--
 1 files changed, 44 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/a513de00/src/main/php/appenders/LoggerAppenderMail.php
----------------------------------------------------------------------
diff --git a/src/main/php/appenders/LoggerAppenderMail.php b/src/main/php/appenders/LoggerAppenderMail.php
index fe26e1a..c93f518 100644
--- a/src/main/php/appenders/LoggerAppenderMail.php
+++ b/src/main/php/appenders/LoggerAppenderMail.php
@@ -33,6 +33,7 @@
  *     addresses may be specified by separating them with a comma.
  * - **from** - Email address which will be used in the From field.
  * - **subject** - Subject of the email message.
+ * - **bufferSize** - Output buffer size. Number of messages sent together.
  * 
  * @package log4php
  * @subpackage appenders
@@ -65,8 +66,24 @@ class LoggerAppenderMail extends LoggerAppender {
 	 */
 	protected $body = '';
 	
+	/**
+	 * Output buffer size. Number of meessages kept in buffer before sending.
+	 * @var integer
+	 */
+	protected $bufferSize;
+
+	/**
+	 * Number of messages currently in buffer.
+	 * @var string
+	 */
+	protected $bufferCount = 0;
+
 	public function append(LoggerLoggingEvent $event) {
 		$this->body .= $this->layout->format($event);
+		$this->bufferCount += 1;
+		if(isset($this->bufferSize) && $this->bufferCount >= $this->bufferSize)
{
+			$this->send();
+		}
 	}
 
 	public function activateOptions() {
@@ -85,16 +102,27 @@ class LoggerAppenderMail extends LoggerAppender {
 	public function close() {
 		if(!$this->closed) {
 			if(!empty($this->body)) {
-				$message = $this->layout->getHeader() . $this->body . $this->layout->getFooter();
-				$contentType = $this->layout->getContentType();
+				$this->send();
+			}
+			$this->closed = true;
+		}
+	}
 
-				$headers = "From: {$this->from}\r\n";
-				$headers .= "Content-Type: {$contentType}\r\n";
+	protected function send() {
+		$message = $this->layout->getHeader() . $this->body . $this->layout->getFooter();
+		$contentType = $this->layout->getContentType();
 
-				mail($this->to, $this->subject, $message, $headers);
-			}
+		$headers = "From: {$this->from}\r\n";
+		$headers .= "Content-Type: {$contentType}\r\n";
+
+		$success = mail($this->to, $this->subject, $message, $headers);
+		if ($success === false) {
+			$this->warn("Failed sending email. Please check your php.ini settings. Closing appender.");
 			$this->closed = true;
 		}
+
+		$this->bufferCount = 0;
+		$this->body = '';
 	}
 	
 	/** Sets the 'subject' parameter. */
@@ -126,4 +154,14 @@ class LoggerAppenderMail extends LoggerAppender {
 	public function getFrom() {
 		return $this->from;
 	}
+
+	/** Sets the 'bufferSize' parameter. */
+	public function setBufferSize($bufferSize) {
+		$this->setInteger('bufferSize', $bufferSize);
+	}
+
+	/** Returns the 'bufferSize' parameter. */
+	public function getBufferSize() {
+		return $this->bufferSize;
+	}
 }


Mime
View raw message