logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ihabu...@apache.org
Subject git commit: LOG4PHP-204: added support for MDC context in mongo appender.
Date Sat, 14 Dec 2013 14:14:48 GMT
Updated Branches:
  refs/heads/develop 1b558631d -> 4e7dd2157


LOG4PHP-204: added support for MDC context in mongo appender.

Signed-off-by: Vladimir Gorej <gorej@codescale.net>
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/4e7dd215
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4php/tree/4e7dd215
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4php/diff/4e7dd215

Branch: refs/heads/develop
Commit: 4e7dd2157e7322ba538d0fe805352ff639d14abe
Parents: 1b55863
Author: Vladimir Gorej <gorej@codescale.net>
Authored: Fri Jun 21 11:29:39 2013 +0200
Committer: Ivan Habunek <ihabunek@apache.org>
Committed: Sat Dec 14 14:39:14 2013 +0100

----------------------------------------------------------------------
 .../php/appenders/LoggerAppenderMongoDB.php     | 21 ++++++++++---------
 .../php/appenders/LoggerAppenderMongoDBTest.php | 22 ++++++++++++++++++++
 2 files changed, 33 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/4e7dd215/src/main/php/appenders/LoggerAppenderMongoDB.php
----------------------------------------------------------------------
diff --git a/src/main/php/appenders/LoggerAppenderMongoDB.php b/src/main/php/appenders/LoggerAppenderMongoDB.php
index 7486ded..f327378 100644
--- a/src/main/php/appenders/LoggerAppenderMongoDB.php
+++ b/src/main/php/appenders/LoggerAppenderMongoDB.php
@@ -265,8 +265,9 @@ class LoggerAppenderMongoDB extends LoggerAppender {
 			# Connection string generation.
 			if ($this->connectionString === null) {
 				$connectionString = sprintf('%s%s:%d', self::DEFAULT_MONGO_URL_PREFIX,
-							    preg_replace('/^'.preg_quote(self::DEFAULT_MONGO_URL_PREFIX, '/').'/', '', $this->host),
-							    $this->port);
+											preg_replace('/^'.preg_quote(self::DEFAULT_MONGO_URL_PREFIX, '/').'/',
+														 '', $this->host),
+											$this->port);
 			} else {
 				$connectionString = $this->connectionString;
 			}
@@ -313,7 +314,8 @@ class LoggerAppenderMongoDB extends LoggerAppender {
 
 	/**
 	 * Converts the logging event into an array which can be logged to mongodb.
-	 * 
+	 * Note that MDC context keys that are the same as mongo appender default keys will be overridden.
+	 *
 	 * @param LoggerLoggingEvent $event
 	 * @return array The array representation of the logging event.
 	 */
@@ -321,13 +323,12 @@ class LoggerAppenderMongoDB extends LoggerAppender {
 		$timestampSec = (int) $event->getTimestamp();
 		$timestampUsec = (int) (($event->getTimestamp() - $timestampSec) * 1000000);
 
-		$document = array(
-			'timestamp' => new MongoDate($timestampSec, $timestampUsec),
-			'level' => $event->getLevel()->toString(),
-			'thread' => (int) $event->getThreadName(),
-			'message' => $event->getMessage(),
-			'loggerName' => $event->getLoggerName() 
-		);	
+		$document = LoggerMDC::getMap();
+		$document['timestamp'] = new MongoDate($timestampSec, $timestampUsec);
+		$document['level'] = $event->getLevel()->toString();
+		$document['thread'] = (int) $event->getThreadName();
+		$document['message'] = $event->getMessage();
+		$document['loggerName'] = $event->getLoggerName();
 
 		$locationInfo = $event->getLocationInformation();
 		if ($locationInfo != null) {

http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/4e7dd215/src/test/php/appenders/LoggerAppenderMongoDBTest.php
----------------------------------------------------------------------
diff --git a/src/test/php/appenders/LoggerAppenderMongoDBTest.php b/src/test/php/appenders/LoggerAppenderMongoDBTest.php
index 7bdf0f2..db4acf8 100644
--- a/src/test/php/appenders/LoggerAppenderMongoDBTest.php
+++ b/src/test/php/appenders/LoggerAppenderMongoDBTest.php
@@ -55,6 +55,7 @@ class LoggerAppenderMongoDBTest extends PHPUnit_Framework_TestCase {
 			}
 		}
 		unset($this->appender);
+		LoggerMDC::clear();
 	}
 
 	public function testConnectionString() {
@@ -215,6 +216,27 @@ class LoggerAppenderMongoDBTest extends PHPUnit_Framework_TestCase {
 		$this->assertTrue(is_int($record['thread']));
 		$this->assertSame(getmypid(), $record['thread']);
 		$this->assertTrue(is_int($record['lineNumber']) || $record['lineNumber'] == 'NA');
+		$this->assertEquals(10, count($record));
+	}
+
+	public function testFormatMDC() {
+		$this->appender->activateOptions();
+		LoggerMDC::put('extra_data', 'extra data');
+		$record = $this->logOne($this->event);
+		$this->assertEquals(1, count(LoggerMDC::getMap()));
+		$this->assertEquals(11, count($record));
+		$this->assertArrayHasKey('extra_data', $record);
+		$this->assertEquals('extra data', $record['extra_data']);
+	}
+
+	public function testFormatMDCOverride() {
+		$this->appender->activateOptions();
+		LoggerMDC::put('fileName', 'extra data');
+		$record = $this->logOne($this->event);
+		$this->assertEquals(1, count(LoggerMDC::getMap()));
+		$this->assertEquals(10, count($record));
+		$this->assertArrayHasKey('fileName', $record);
+		$this->assertEquals('NA', $record['fileName']);
 	}
 
 	public function testFormatThrowableInfo() {


Mime
View raw message