You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4php-dev@logging.apache.org by ih...@apache.org on 2011/05/15 15:11:28 UTC
svn commit: r1103366 - in /logging/log4php/trunk/src: changes/ main/php/
main/php/appenders/ main/php/helpers/ main/php/layouts/ test/php/appenders/
test/php/helpers/ test/php/layouts/
Author: ihabunek
Date: Sun May 15 13:11:28 2011
New Revision: 1103366
URL: http://svn.apache.org/viewvc?rev=1103366&view=rev
Log:
Work on the MongoDB appender (original code provided by Vladimir Gorej) :
* Added LoggerLoggingEventBsonifier helper class for constructing bson arrays as required by MongoDB appender.
* Adapted LoggerAppenderMongoDB to use the bsonifier helper instead of a layout.
* Removed LoggerLayoutBson since it is no longer used (maybe rework the code for a JSON appender later).
* Adapted tests where needed.
Added:
logging/log4php/trunk/src/main/php/helpers/LoggerLoggingEventBsonifier.php
logging/log4php/trunk/src/test/php/helpers/LoggerLoggingEventBsonifierTest.php
- copied, changed from r1103330, logging/log4php/trunk/src/test/php/layouts/LoggerMongoDbBsonLayoutTest.php
Removed:
logging/log4php/trunk/src/main/php/layouts/LoggerLayoutBson.php
logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBLayoutTest.php
logging/log4php/trunk/src/test/php/layouts/LoggerMongoDbBsonLayoutTest.php
Modified:
logging/log4php/trunk/src/changes/changes.xml
logging/log4php/trunk/src/main/php/Logger.php
logging/log4php/trunk/src/main/php/appenders/LoggerAppenderMongoDB.php
logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBTest.php
Modified: logging/log4php/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/changes/changes.xml?rev=1103366&r1=1103365&r2=1103366&view=diff
==============================================================================
--- logging/log4php/trunk/src/changes/changes.xml (original)
+++ logging/log4php/trunk/src/changes/changes.xml Sun May 15 13:11:28 2011
@@ -24,6 +24,7 @@
</properties>
<body>
<release version="2.1" description="Stabilizing">
+ <action type="fix" issue="LOG4PHP-110" by="Vladimir Gorej, Ivan Habunek">Adapted MongoDB appender to better fit in log4php codebase.</action>
<action type="fix" issue="LOG4PHP-126" by="Peter Chapman, Christian Grobmeier">LoggerConfiguratorPhp does not appear to respect appender file property from config</action>
<action type="fix" issue="LOG4PHP-118" by="Craig Marvelley">Additivity cannot be disabled through log4php.properties ini file.</action>
<action type="update" issue="LOG4PHP-110" by="Vladimir Gorej, Christian Grobmeier">Added MongoDB appender</action>
Modified: logging/log4php/trunk/src/main/php/Logger.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/Logger.php?rev=1103366&r1=1103365&r2=1103366&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/Logger.php (original)
+++ logging/log4php/trunk/src/main/php/Logger.php Sun May 15 13:11:28 2011
@@ -90,6 +90,7 @@ class Logger {
'LoggerDatePatternConverter' => '/helpers/LoggerDatePatternConverter.php',
'LoggerLiteralPatternConverter' => '/helpers/LoggerLiteralPatternConverter.php',
'LoggerLocationPatternConverter' => '/helpers/LoggerLocationPatternConverter.php',
+ 'LoggerLoggingEventBsonifier' => '/helpers/LoggerLoggingEventBsonifier.php',
'LoggerMDCPatternConverter' => '/helpers/LoggerMDCPatternConverter.php',
'LoggerNamedPatternConverter' => '/helpers/LoggerNamedPatternConverter.php',
'LoggerBasicPatternConverter' => '/helpers/LoggerBasicPatternConverter.php',
Modified: logging/log4php/trunk/src/main/php/appenders/LoggerAppenderMongoDB.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/appenders/LoggerAppenderMongoDB.php?rev=1103366&r1=1103365&r2=1103366&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/appenders/LoggerAppenderMongoDB.php (original)
+++ logging/log4php/trunk/src/main/php/appenders/LoggerAppenderMongoDB.php Sun May 15 13:11:28 2011
@@ -21,30 +21,6 @@
/**
* Appender for writing to MongoDB.
*
- * Format of log event (for exception):
- * {
- * "_id": MongoId
- * "timestamp": MongoDate,
- * "level":"ERROR",
- * "thread":"2556",
- * "message":"testmessage",
- * "loggerName":"testLogger",
- * "fileName":"NA",
- * "method":"getLocationInformation",
- * "lineNumber":"NA",
- * "className":"LoggerLoggingEvent",
- * "exception":{
- * "message":"exception2",
- * "code":0,
- * "stackTrace":"stackTrace of Exception",
- * "innerException":{
- * "message":"exception1",
- * "code":0,
- * "stackTrace":"stactTrace of inner Exception"
- * }
- * }
- * }
- *
* This class has been originally contributed from Vladimir Gorej
* (http://github.com/log4mongo/log4mongo-php).
*
@@ -61,28 +37,31 @@ class LoggerAppenderMongoDB extends Logg
private static $DEFAULT_DB_NAME = 'log4php_mongodb';
private static $DEFAULT_COLLECTION_NAME = 'logs';
- private $hostname;
- private $port;
- private $dbName;
- private $collectionName;
+ protected $hostname;
+ protected $port;
+ protected $dbName;
+ protected $collectionName;
- private $connection;
- private $collection;
+ protected $connection;
+ protected $collection;
+ protected $bsonifier;
- private $userName;
- private $password;
+ protected $userName;
+ protected $password;
+
+ protected $canAppend = false;
- private $canAppend = false;
+ protected $requiresLayout = false;
public function __construct($name = '') {
parent::__construct($name);
$this->hostname = self::$DEFAULT_MONGO_URL_PREFIX.self::$DEFAULT_MONGO_HOST;
$this->port = self::$DEFAULT_MONGO_PORT;
$this->dbName = self::$DEFAULT_DB_NAME;
- $this->collectionName = self::$DEFAULT_COLLECTION_NAME;
- $this->requiresLayout = false;
- $this->setLayout(new LoggerLayoutBson());
+ $this->collectionName = self::$DEFAULT_COLLECTION_NAME;
+ $this->bsonifier = new LoggerLoggingEventBsonifier();
}
+
/**
* Setup db connection.
* Based on defined options, this method connects to db defined in {@link $dbNmae}
@@ -112,24 +91,13 @@ class LoggerAppenderMongoDB extends Logg
}
/**
- * Set the Layout for this appender. Per default the LoggerLayoutBson
- * is used as format. It can be overwritten with your own format using this
- * setLayout method, even when a layout is not required.
- * @see LoggerAppender::setLayout()
- * @param LoggerLayout $layout
- */
- public function setLayout($layout) {
- $this->layout = $layout;
- }
-
- /**
* Appends a new event to the mongo database.
*
* @throws LoggerException If the pattern conversion or the INSERT statement fails.
*/
public function append(LoggerLoggingEvent $event) {
if ($this->canAppend == true && $this->collection != null) {
- $document = (array) $this->getLayout()->format($event);
+ $document = $this->bsonifier->bsonify($event);
$this->collection->insert($document);
}
}
Added: logging/log4php/trunk/src/main/php/helpers/LoggerLoggingEventBsonifier.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/helpers/LoggerLoggingEventBsonifier.php?rev=1103366&view=auto
==============================================================================
--- logging/log4php/trunk/src/main/php/helpers/LoggerLoggingEventBsonifier.php (added)
+++ logging/log4php/trunk/src/main/php/helpers/LoggerLoggingEventBsonifier.php Sun May 15 13:11:28 2011
@@ -0,0 +1,104 @@
+<?php
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * 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.
+ *
+ * @package log4php
+ * @subpackage helpers
+ */
+
+/**
+ * Helper class used by MongoDB appender.
+ *
+ * This class has been originally contributed from Vladimir Gorej
+ * (http://github.com/log4mongo/log4mongo-php).
+ */
+class LoggerLoggingEventBsonifier
+{
+ /**
+ * Bson-ify logging event into mongo bson
+ *
+ * @param LoggerLoggingEvent $event
+ * @return array
+ */
+ public function bsonify(LoggerLoggingEvent $event) {
+ $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()
+ );
+
+ $this->addLocationInformation($document, $event->getLocationInformation());
+ $this->addThrowableInformation($document, $event->getThrowableInformation());
+
+ return $document;
+ }
+
+ /**
+ * Adding, if exists, location information into bson document
+ *
+ * @param array $document
+ * @param LoggerLocationInfo $locationInfo
+ */
+ protected function addLocationInformation(&$document, LoggerLocationInfo $locationInfo = null) {
+ if ($locationInfo != null) {
+ $document['fileName'] = $locationInfo->getFileName();
+ $document['method'] = $locationInfo->getMethodName();
+ $document['lineNumber'] = ($locationInfo->getLineNumber() == 'NA') ? 'NA' : (int) $locationInfo->getLineNumber();
+ $document['className'] = $locationInfo->getClassName();
+ }
+ }
+
+ /**
+ * Adding, if exists, throwable information into bson document
+ *
+ * @param array $document
+ * @param LoggerThrowableInformation $throwableInfo
+ */
+ protected function addThrowableInformation(&$document, LoggerThrowableInformation $throwableInfo = null) {
+ if ($throwableInfo != null) {
+ $document['exception'] = $this->bsonifyThrowable($throwableInfo->getThrowable());
+ }
+ }
+
+ /**
+ * Bson-ifying php native Exception object
+ * Support for innner exceptions
+ *
+ * @param Exception $ex
+ * @return array
+ */
+ protected function bsonifyThrowable(Exception $ex) {
+
+ $bsonException = array(
+ 'message' => $ex->getMessage(),
+ 'code' => $ex->getCode(),
+ 'stackTrace' => $ex->getTraceAsString(),
+ );
+
+ if (method_exists($ex, 'getPrevious') && $ex->getPrevious() !== null) {
+ $bsonException['innerException'] = $this->bsonifyThrowable($ex->getPrevious());
+ }
+
+ return $bsonException;
+ }
+}
+
+?>
\ No newline at end of file
Modified: logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBTest.php
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBTest.php?rev=1103366&r1=1103365&r2=1103366&view=diff
==============================================================================
--- logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBTest.php (original)
+++ logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBTest.php Sun May 15 13:11:28 2011
@@ -19,7 +19,7 @@
*/
/**
- * Testclass for the default layout.
+ * Testclass for the MongoDB appender.
*
* This class has been originally contributed from Vladimir Gorej
* (http://github.com/log4mongo/log4mongo-php).
@@ -35,8 +35,8 @@ class LoggerAppenderMongoDBTest extends
protected static $event;
public static function setUpBeforeClass() {
- self::$appender = new LoggerAppenderMongoDB('mongo_appender');
- self::$event = new LoggerLoggingEvent("LoggerAppenderMongoDBTest", new Logger("TEST"), LoggerLevel::getLevelError(), "testmessage");
+ self::$appender = new LoggerAppenderMongoDB('mongo_appender');
+ self::$event = new LoggerLoggingEvent("LoggerAppenderMongoDBTest", new Logger("TEST"), LoggerLevel::getLevelError(), "testmessage");
}
public static function tearDownAfterClass() {
Copied: logging/log4php/trunk/src/test/php/helpers/LoggerLoggingEventBsonifierTest.php (from r1103330, logging/log4php/trunk/src/test/php/layouts/LoggerMongoDbBsonLayoutTest.php)
URL: http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/helpers/LoggerLoggingEventBsonifierTest.php?p2=logging/log4php/trunk/src/test/php/helpers/LoggerLoggingEventBsonifierTest.php&p1=logging/log4php/trunk/src/test/php/layouts/LoggerMongoDbBsonLayoutTest.php&r1=1103330&r2=1103366&rev=1103366&view=diff
==============================================================================
--- logging/log4php/trunk/src/test/php/layouts/LoggerMongoDbBsonLayoutTest.php (original)
+++ logging/log4php/trunk/src/test/php/helpers/LoggerLoggingEventBsonifierTest.php Sun May 15 13:11:28 2011
@@ -29,29 +29,19 @@
* @subpackage appenders
* @since 2.1
*/
-class LoggerMongoDbBsonLayoutTest extends PHPUnit_Framework_TestCase {
+class LoggerLoggingEventBsonifierTest extends PHPUnit_Framework_TestCase {
protected static $logger;
- protected static $layout;
+ protected static $bsonifier;
public static function setUpBeforeClass() {
- self::$logger = Logger::getLogger('test.Logger');
- self::$layout = new LoggerLayoutBson();
+ self::$logger = Logger::getLogger('test.Logger');
+ self::$bsonifier = new LoggerLoggingEventBsonifier();
}
public static function tearDownAfterClass() {
self::$logger = null;
- self::$layout = null;
- }
-
- public function testActivateOptions() {
- $result = self::$layout->activateOptions();
- $this->assertTrue($result);
- }
-
- public function testgetContentType() {
- $result = self::$layout->getContentType();
- $this->assertEquals('application/bson', $result);
+ self::$bsonifier = null;
}
public function testFormatSimple() {
@@ -61,7 +51,7 @@ class LoggerMongoDbBsonLayoutTest extend
LoggerLevel::getLevelWarn(),
'test message'
);
- $bsonifiedEvent = self::$layout->format($event);
+ $bsonifiedEvent = self::$bsonifier->bsonify($event);
$this->assertEquals('WARN', $bsonifiedEvent['level']);
$this->assertEquals('test message', $bsonifiedEvent['message']);
@@ -75,7 +65,7 @@ class LoggerMongoDbBsonLayoutTest extend
LoggerLevel::getLevelWarn(),
'test message'
);
- $bsonifiedEvent = self::$layout->format($event);
+ $bsonifiedEvent = self::$bsonifier->bsonify($event);
$this->assertEquals('NA', $bsonifiedEvent['fileName']);
$this->assertEquals('getLocationInformation', $bsonifiedEvent['method']);
@@ -92,12 +82,12 @@ class LoggerMongoDbBsonLayoutTest extend
microtime(true),
new Exception('test exception', 1)
);
- $bsonifiedEvent = self::$layout->format($event);
+ $bsonifiedEvent = self::$bsonifier->bsonify($event);
- $this->assertTrue(array_key_exists('exception', $bsonifiedEvent));
+ $this->assertArrayHasKey('exception', $bsonifiedEvent);
$this->assertEquals(1, $bsonifiedEvent['exception']['code']);
$this->assertEquals('test exception', $bsonifiedEvent['exception']['message']);
- $this->assertContains('[internal function]: LoggerMongoDbBsonLayoutTest', $bsonifiedEvent['exception']['stackTrace']);
+ $this->assertContains('[internal function]: LoggerLoggingEventBsonifierTest', $bsonifiedEvent['exception']['stackTrace']);
}
public function testFormatThrowableInfoWithInnerException() {
@@ -109,13 +99,13 @@ class LoggerMongoDbBsonLayoutTest extend
microtime(true),
new Exception('test exeption', 1, new Exception('test exception inner', 2))
);
- $bsonifiedEvent = self::$layout->format($event);
+ $bsonifiedEvent = self::$bsonifier->bsonify($event);
$this->assertTrue(array_key_exists('exception', $bsonifiedEvent));
$this->assertTrue(array_key_exists('innerException', $bsonifiedEvent['exception']));
$this->assertEquals(2, $bsonifiedEvent['exception']['innerException']['code']);
$this->assertEquals('test exception inner', $bsonifiedEvent['exception']['innerException']['message']);
- $this->assertContains('[internal function]: LoggerMongoDbBsonLayoutTest', $bsonifiedEvent['exception']['stackTrace']);
+ $this->assertContains('[internal function]: LoggerLoggingEventBsonifierTest', $bsonifiedEvent['exception']['stackTrace']);
}
@@ -126,7 +116,7 @@ class LoggerMongoDbBsonLayoutTest extend
LoggerLevel::getLevelWarn(),
'test message'
);
- $bsonifiedEvent = self::$layout->bsonify($event);
+ $bsonifiedEvent = self::$bsonifier->bsonify($event);
$this->assertEquals('WARN', $bsonifiedEvent['level']);
$this->assertEquals('test message', $bsonifiedEvent['message']);
@@ -140,7 +130,7 @@ class LoggerMongoDbBsonLayoutTest extend
LoggerLevel::getLevelWarn(),
'test message'
);
- $bsonifiedEvent = self::$layout->bsonify($event);
+ $bsonifiedEvent = self::$bsonifier->bsonify($event);
$this->assertEquals('NA', $bsonifiedEvent['fileName']);
$this->assertEquals('getLocationInformation', $bsonifiedEvent['method']);
@@ -157,12 +147,12 @@ class LoggerMongoDbBsonLayoutTest extend
microtime(true),
new Exception('test exception', 1)
);
- $bsonifiedEvent = self::$layout->bsonify($event);
+ $bsonifiedEvent = self::$bsonifier->bsonify($event);
$this->assertTrue(array_key_exists('exception', $bsonifiedEvent));
$this->assertEquals(1, $bsonifiedEvent['exception']['code']);
$this->assertEquals('test exception', $bsonifiedEvent['exception']['message']);
- $this->assertContains('[internal function]: LoggerMongoDbBsonLayoutTest', $bsonifiedEvent['exception']['stackTrace']);
+ $this->assertContains('[internal function]: LoggerLoggingEventBsonifierTest', $bsonifiedEvent['exception']['stackTrace']);
}
public function testBsonifyThrowableInfoWithInnerException() {
@@ -174,13 +164,13 @@ class LoggerMongoDbBsonLayoutTest extend
microtime(true),
new Exception('test exeption', 1, new Exception('test exception inner', 2))
);
- $bsonifiedEvent = self::$layout->bsonify($event);
+ $bsonifiedEvent = self::$bsonifier->bsonify($event);
$this->assertTrue(array_key_exists('exception', $bsonifiedEvent));
$this->assertTrue(array_key_exists('innerException', $bsonifiedEvent['exception']));
$this->assertEquals(2, $bsonifiedEvent['exception']['innerException']['code']);
$this->assertEquals('test exception inner', $bsonifiedEvent['exception']['innerException']['message']);
- $this->assertContains('[internal function]: LoggerMongoDbBsonLayoutTest', $bsonifiedEvent['exception']['stackTrace']);
+ $this->assertContains('[internal function]: LoggerLoggingEventBsonifierTest', $bsonifiedEvent['exception']['stackTrace']);
}
public function testIsThreadInteger() {
@@ -190,7 +180,7 @@ class LoggerMongoDbBsonLayoutTest extend
LoggerLevel::getLevelWarn(),
'test message'
);
- $bsonifiedEvent = self::$layout->bsonify($event);
+ $bsonifiedEvent = self::$bsonifier->bsonify($event);
$this->assertTrue(is_int($bsonifiedEvent['thread']));
}
@@ -201,7 +191,7 @@ class LoggerMongoDbBsonLayoutTest extend
LoggerLevel::getLevelWarn(),
'test message'
);
- $bsonifiedEvent = self::$layout->bsonify($event);
+ $bsonifiedEvent = self::$bsonifier->bsonify($event);
$this->assertTrue(is_int($bsonifiedEvent['lineNumber']) || $bsonifiedEvent['lineNumber'] == 'NA');
}
@@ -214,7 +204,7 @@ class LoggerMongoDbBsonLayoutTest extend
microtime(true),
new Exception('test exeption', 1, new Exception('test exception inner', 2))
);
- $bsonifiedEvent = self::$layout->bsonify($event);
+ $bsonifiedEvent = self::$bsonifier->bsonify($event);
$this->assertTrue(is_int($bsonifiedEvent['exception']['code']));
}
}