Author: grobmeier
Date: Tue Feb 15 17:41:47 2011
New Revision: 1070984
URL: http://svn.apache.org/viewvc?rev=1070984&view=rev
Log:
LOG4PHP-110: Added mongodb appender from Vladimir Gorej (with modifications)
Added:
logging/log4php/trunk/src/main/php/appenders/LoggerAppenderMongoDB.php
(with props)
logging/log4php/trunk/src/main/php/layouts/LoggerMongoDBBsonLayout.php
(with props)
logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBLayoutTest.php
(with props)
logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBTest.php
(with props)
logging/log4php/trunk/src/test/php/layouts/LoggerMongoDbBsonLayoutTest.php
(with props)
Modified:
logging/log4php/trunk/src/changes/changes.xml
logging/log4php/trunk/src/main/php/Logger.php
Modified: logging/log4php/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/src/changes/changes.xml?rev=1070984&r1=1070983&r2=1070984&view=diff
==============================================================================
--- logging/log4php/trunk/src/changes/changes.xml (original)
+++ logging/log4php/trunk/src/changes/changes.xml Tue Feb 15 17:41:47 2011
@@ -24,6 +24,7 @@
</properties>
<body>
<release version="2.1" description="Stabilizing">
+ <action type="update" issue="LOG4PHP-110" by="Vladimir Gorej,
Christian Grobmeier">Added MongoDB appender</action>
<action type="fix" issue="LOG4PHP-131" by="Ivan Habunek">File
appenders parameters (removed overloading of setFile()).</action>
<action type="fix" issue="LOG4PHP-133" by="Dmitry
Katemirov,Ivan Habunek">PDO appender doesn't close connections</action>
<action type="fix" by="Ivan Habunek">Replaced calls to
deprecated PHPUnit method assertTypeOf() with assertInternalType() and
assertInstanceOf().</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=1070984&r1=1070983&r2=1070984&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/Logger.php (original)
+++ logging/log4php/trunk/src/main/php/Logger.php Tue Feb 15 17:41:47 2011
@@ -117,6 +117,8 @@ class Logger {
'LoggerFilterLevelMatch' =>
'/filters/LoggerFilterLevelMatch.php',
'LoggerFilterLevelRange' =>
'/filters/LoggerFilterLevelRange.php',
'LoggerFilterStringMatch' =>
'/filters/LoggerFilterStringMatch.php',
+ 'LoggerAppenderMongoDB' =>
'/appenders/LoggerAppenderMongoDB.php',
+ 'LoggerMongoDBBsonLayout' =>
'/layouts/LoggerMongoDBBsonLayout.php'
);
/**
Added: 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=1070984&view=auto
==============================================================================
--- logging/log4php/trunk/src/main/php/appenders/LoggerAppenderMongoDB.php
(added)
+++ logging/log4php/trunk/src/main/php/appenders/LoggerAppenderMongoDB.php Tue
Feb 15 17:41:47 2011
@@ -0,0 +1,214 @@
+<?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
+ */
+
+/**
+ * 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).
+ *
+ * @version $Revision: 806678 $
+ * @package log4php
+ * @subpackage appenders
+ * @since 2.1
+ */
+class LoggerAppenderMongoDB extends LoggerAppender {
+
+ private static $DEFAULT_MONGO_URL_PREFIX = 'mongodb://';
+ private static $DEFAULT_MONGO_HOST = 'localhost';
+ private static $DEFAULT_MONGO_PORT = 27017;
+ private static $DEFAULT_DB_NAME = 'log4php_mongodb';
+ private static $DEFAULT_COLLECTION_NAME = 'logs';
+
+ private $hostname;
+ private $port;
+ private $dbName;
+ private $collectionName;
+
+ private $connection;
+ private $collection;
+
+ private $userName;
+ private $password;
+
+ private $canAppend = 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 LoggerMongoDBBsonLayout());
+ }
+ /**
+ * Setup db connection.
+ * Based on defined options, this method connects to db defined in
{@link $dbNmae}
+ * and creates a {@link $collection}
+ * @return boolean true if all ok.
+ * @throws an Exception if the attempt to connect to the requested
database fails.
+ */
+ public function activateOptions() {
+ try {
+ $this->connection = new Mongo(sprintf('%s:%d',
$this->hostname, $this->port));
+ $db = $this->connection->selectDB($this->dbName);
+ if ($this->userName !== null && $this->password !==
null) {
+ $authResult =
$db->authenticate($this->userName, $this->password);
+ if ($authResult['ok'] == floatval(0)) {
+ throw new
Exception($authResult['errmsg'], $authResult['ok']);
+ }
+ }
+
+ $this->collection =
$db->selectCollection($this->collectionName);
+ } catch (Exception $ex) {
+ $this->canAppend = false;
+ throw new LoggerException($ex);
+ }
+
+ $this->canAppend = true;
+ return true;
+ }
+
+ /**
+ * Set the Layout for this appender. Per default the
LoggerMongoDBBsonLayout
+ * 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);
+ $this->collection->insert($document);
+ }
+ }
+
+ /**
+ * Closes the connection to the logging database
+ */
+ public function close() {
+ if($this->closed != true) {
+ $this->collection = null;
+ if ($this->connection !== null) {
+ $this->connection->close();
+ $this->connection = null;
+ }
+ $this->closed = true;
+ }
+ }
+
+ public function __destruct() {
+ $this->close();
+ }
+
+ public function setHost($hostname) {
+ if (!preg_match('/^mongodb\:\/\//', $hostname)) {
+ $hostname = self::$DEFAULT_MONGO_URL_PREFIX.$hostname;
+ }
+ $this->hostname = $hostname;
+ }
+
+ public function getHost() {
+ return $this->hostname;
+ }
+
+ public function setPort($port) {
+ $this->port = $port;
+ }
+
+ public function getPort() {
+ return $this->port;
+ }
+
+ public function setDatabaseName($dbName) {
+ $this->dbName = $dbName;
+ }
+
+ public function getDatabaseName() {
+ return $this->dbName;
+ }
+
+ public function setCollectionName($collectionName) {
+ $this->collectionName = $collectionName;
+ }
+
+ public function getCollectionName() {
+ return $this->collectionName;
+ }
+
+ public function setUserName($userName) {
+ $this->userName = $userName;
+ }
+
+ public function getUserName() {
+ return $this->userName;
+ }
+
+ public function setPassword($password) {
+ $this->password = $password;
+ }
+
+ public function getPassword() {
+ return $this->password;
+ }
+
+ public function getConnection() {
+ return $this->connection;
+ }
+
+ public function getCollection() {
+ return $this->collection;
+ }
+}
+?>
\ No newline at end of file
Propchange:
logging/log4php/trunk/src/main/php/appenders/LoggerAppenderMongoDB.php
------------------------------------------------------------------------------
svn:eol-style = native
Added: logging/log4php/trunk/src/main/php/layouts/LoggerMongoDBBsonLayout.php
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/layouts/LoggerMongoDBBsonLayout.php?rev=1070984&view=auto
==============================================================================
--- logging/log4php/trunk/src/main/php/layouts/LoggerMongoDBBsonLayout.php
(added)
+++ logging/log4php/trunk/src/main/php/layouts/LoggerMongoDBBsonLayout.php Tue
Feb 15 17:41:47 2011
@@ -0,0 +1,131 @@
+<?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
+ */
+
+/**
+ *
+ * Base layout class for custom logging collections
+ *
+ * example implementation:
+ *
+ * class LoggerMongoDbBsonIpLayout extends LoggerMongoDbBsonLayout {
+ *
+ * public function format(LoggerLoggingEvent $event) {
+ * $document = parent::format($event);
+ * $document['ip'] = $event->getMDC('ip');
+ *
+ * return $document;
+ * }
+ * }
+ *
+ * This class has been originally contributed from Vladimir Gorej
+ * (http://github.com/log4mongo/log4mongo-php).
+ *
+ * @version $Revision: 806678 $
+ * @package log4php
+ * @subpackage appenders
+ * @since 2.1
+ */
+class LoggerMongoDBBsonLayout extends LoggerLayout {
+
+ public function __construct() { }
+
+ public function format(LoggerLoggingEvent $event) {
+ $document = $this->bsonify($event);
+ return $document;
+ }
+
+ public function getContentType() {
+ return "application/bson";
+ }
+
+ /**
+ * Bson-ify logging event into mongo bson
+ *
+ * @param LoggerLoggingEvent $event
+ * @return ArrayObject
+ */
+ public function bsonify(LoggerLoggingEvent $event) {
+ $timestampSec = (int) $event->getTimestamp();
+ $timestampUsec = (int) (($event->getTimestamp() -
$timestampSec) * 1000000);
+
+ $document = new ArrayObject(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 ArrayObject $document
+ * @param LoggerLocationInfo $locationInfo *
+ */
+ protected function addLocationInformation(ArrayObject $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 ArrayObject $document
+ * @param LoggerThrowableInformation $throwableInfo
+ */
+ protected function addThrowableInformation(ArrayObject $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
Propchange:
logging/log4php/trunk/src/main/php/layouts/LoggerMongoDBBsonLayout.php
------------------------------------------------------------------------------
svn:eol-style = native
Added:
logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBLayoutTest.php
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBLayoutTest.php?rev=1070984&view=auto
==============================================================================
---
logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBLayoutTest.php
(added)
+++
logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBLayoutTest.php
Tue Feb 15 17:41:47 2011
@@ -0,0 +1,66 @@
+<?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
+ */
+
+
+/**
+ * Test with an external layout.
+ *
+ * This class has been originally contributed from Vladimir Gorej
+ * (http://github.com/log4mongo/log4mongo-php).
+ *
+ * @version $Revision: 806678 $
+ * @package log4php
+ * @subpackage appenders
+ * @since 2.1
+ */
+class LoggerAppenderMongoDBLayoutTest extends PHPUnit_Framework_TestCase {
+
+ protected static $appender;
+ protected static $layout;
+ protected static $event;
+
+ public static function setUpBeforeClass() {
+ self::$appender = new
LoggerAppenderMongoDB('mongo_appender');
+ self::$layout = new LoggerMongoDBBsonLayout();
+ self::$appender->setLayout(self::$layout);
+ self::$event = new
LoggerLoggingEvent("LoggerAppenderMongoDBLayoutTest", new Logger("TEST"),
LoggerLevel::getLevelError(), "testmessage");
+ }
+
+ public static function tearDownAfterClass() {
+ self::$appender->close();
+ self::$appender = null;
+ self::$layout = null;
+ self::$event = null;
+ }
+
+ public function testMongoDB() {
+ self::$appender->activateOptions();
+ $mongo = self::$appender->getConnection();
+ $db = $mongo->selectDB('log4php_mongodb');
+ $db->drop('logs');
+ $collection = $db->selectCollection('logs');
+
+ self::$appender->append(self::$event);
+
+ $this->assertNotEquals($collection->findOne(), null,
'Collection should return one record');
+ }
+
+}
+?>
\ No newline at end of file
Propchange:
logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBLayoutTest.php
------------------------------------------------------------------------------
svn:eol-style = native
Added:
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=1070984&view=auto
==============================================================================
--- logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBTest.php
(added)
+++ logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBTest.php
Tue Feb 15 17:41:47 2011
@@ -0,0 +1,196 @@
+<?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
+ */
+
+/**
+ * Testclass for the default layout.
+ *
+ * This class has been originally contributed from Vladimir Gorej
+ * (http://github.com/log4mongo/log4mongo-php).
+ *
+ * @version $Revision: 806678 $
+ * @package log4php
+ * @subpackage appenders
+ * @since 2.1
+ */
+class LoggerAppenderMongoDBTest extends PHPUnit_Framework_TestCase {
+
+ protected static $appender;
+ protected static $event;
+
+ public static function setUpBeforeClass() {
+ self::$appender = new
LoggerAppenderMongoDB('mongo_appender');
+ self::$event = new
LoggerLoggingEvent("LoggerAppenderMongoDBTest", new Logger("TEST"),
LoggerLevel::getLevelError(), "testmessage");
+ }
+
+ public static function tearDownAfterClass() {
+ self::$appender->close();
+ self::$appender = null;
+ self::$event = null;
+ }
+
+ public function test__construct() {
+ $appender = new LoggerAppenderMongoDB('mongo_appender');
+ $this->assertTrue($appender instanceof LoggerAppenderMongoDB);
+ }
+
+ public function testSetGetHost() {
+ $expected = 'mongodb://localhost';
+ self::$appender->setHost($expected);
+ $result = self::$appender->getHost();
+ $this->assertEquals($expected, $result, 'Host doesn\'t match
expted value');
+ }
+
+ public function testSetGetHostMongoPrefix() {
+ $expected = 'mongodb://localhost';
+ self::$appender->setHost('localhost');
+ $result = self::$appender->getHost();
+ $this->assertEquals($expected, $result, 'Host doesn\'t match
expted value');
+ }
+
+ public function testSetPort() {
+ $expected = 27017;
+ self::$appender->setPort($expected);
+ $result = self::$appender->getPort();
+ $this->assertEquals($expected, $result, 'Port doesn\'t match
expted value');
+ }
+
+ public function testGetPort() {
+ $expected = 27017;
+ self::$appender->setPort($expected);
+ $result = self::$appender->getPort();
+ $this->assertEquals($expected, $result, 'Port doesn\'t match
expted value');
+ }
+
+ public function testSetDatabaseName() {
+ $expected = 'log4php_mongodb';
+ self::$appender->setDatabaseName($expected);
+ $result = self::$appender->getDatabaseName();
+ $this->assertEquals($expected, $result, 'Database name doesn\'t
match expted value');
+ }
+
+ public function testGetDatabaseName() {
+ $expected = 'log4php_mongodb';
+ self::$appender->setDatabaseName($expected);
+ $result = self::$appender->getDatabaseName();
+ $this->assertEquals($expected, $result, 'Database name doesn\'t
match expted value');
+ }
+
+ public function testSetCollectionName() {
+ $expected = 'logs';
+ self::$appender->setCollectionName($expected);
+ $result = self::$appender->getCollectionName();
+ $this->assertEquals($expected, $result, 'Collection name
doesn\'t match expted value');
+ }
+
+ public function testGetCollectionName() {
+ $expected = 'logs';
+ self::$appender->setCollectionName($expected);
+ $result = self::$appender->getCollectionName();
+ $this->assertEquals($expected, $result, 'Collection name
doesn\'t match expted value');
+ }
+
+ public function testSetUserName() {
+ $expected = 'char0n';
+ self::$appender->setUserName($expected);
+ $result = self::$appender->getUserName();
+ $this->assertEquals($expected, $result, 'UserName doesn\'t
match expted value');
+ }
+
+ public function testGetUserName() {
+ $expected = 'char0n';
+ self::$appender->setUserName($expected);
+ $result = self::$appender->getUserName();
+ $this->assertEquals($expected, $result, 'UserName doesn\'t
match expted value');
+ }
+
+ public function testSetPassword() {
+ $expected = 'secret pass';
+ self::$appender->setPassword($expected);
+ $result = self::$appender->getPassword();
+ $this->assertEquals($expected, $result, 'Password doesn\'t
match expted value');
+ }
+
+ public function testGetPassword() {
+ $expected = 'secret pass';
+ self::$appender->setPassword($expected);
+ $result = self::$appender->getPassword();
+ $this->assertEquals($expected, $result, 'Password doesn\'t
match expted value');
+ }
+
+ public function testActivateOptionsNoCredentials() {
+ try {
+ self::$appender->setUserName(null);
+ self::$appender->setPassword(null);
+ self::$appender->activateOptions();
+ } catch (Exception $ex) {
+ $this->fail('Activating appender options was not
successful');
+ }
+ }
+
+ public function testAppend() {
+ self::$appender->append(self::$event);
+ }
+
+ public function testMongoDB() {
+ self::$appender->activateOptions();
+ $mongo = self::$appender->getConnection();
+ $db = $mongo->selectDB('log4php_mongodb');
+ $db->drop('logs');
+ $collection = $db->selectCollection('logs');
+
+ self::$appender->append(self::$event);
+
+ $this->assertNotEquals(null, $collection->findOne(),
'Collection should return one record');
+ }
+
+ public function testMongoDBException() {
+ self::$appender->activateOptions();
+ $mongo = self::$appender->getConnection();
+ $db = $mongo->selectDB('log4php_mongodb');
+ $db->drop('logs');
+ $collection = $db->selectCollection('logs');
+
+ $throwable = new Exception('exception1');
+
+ self::$appender->append(new
LoggerLoggingEvent("LoggerAppenderMongoDBTest", new Logger("TEST"),
LoggerLevel::getLevelError(), "testmessage", microtime(true), $throwable));
+
+ $this->assertNotEquals(null, $collection->findOne(),
'Collection should return one record');
+ }
+
+ public function testMongoDBInnerException() {
+ self::$appender->activateOptions();
+ $mongo = self::$appender->getConnection();
+ $db = $mongo->selectDB('log4php_mongodb');
+ $db->drop('logs');
+ $collection = $db->selectCollection('logs');
+
+ $throwable1 = new Exception('exception1');
+ $throwable2 = new Exception('exception2', 0, $throwable1);
+
+ self::$appender->append(new
LoggerLoggingEvent("LoggerAppenderMongoDBTest", new Logger("TEST"),
LoggerLevel::getLevelError(), "testmessage", microtime(true), $throwable2));
+
+ $this->assertNotEquals(null, $collection->findOne(),
'Collection should return one record');
+ }
+
+ public function testClose() {
+ self::$appender->close();
+ }
+}
+?>
\ No newline at end of file
Propchange:
logging/log4php/trunk/src/test/php/appenders/LoggerAppenderMongoDBTest.php
------------------------------------------------------------------------------
svn:eol-style = native
Added:
logging/log4php/trunk/src/test/php/layouts/LoggerMongoDbBsonLayoutTest.php
URL:
http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/layouts/LoggerMongoDbBsonLayoutTest.php?rev=1070984&view=auto
==============================================================================
--- logging/log4php/trunk/src/test/php/layouts/LoggerMongoDbBsonLayoutTest.php
(added)
+++ logging/log4php/trunk/src/test/php/layouts/LoggerMongoDbBsonLayoutTest.php
Tue Feb 15 17:41:47 2011
@@ -0,0 +1,221 @@
+<?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
+ */
+
+/**
+ * Layout test.
+ *
+ * This class has been originally contributed from Vladimir Gorej
+ * (http://github.com/log4mongo/log4mongo-php).
+ *
+ * @version $Revision: 806678 $
+ * @package log4php
+ * @subpackage appenders
+ * @since 2.1
+ */
+class LoggerMongoDbBsonLayoutTest extends PHPUnit_Framework_TestCase {
+
+ protected static $logger;
+ protected static $layout;
+
+ public static function setUpBeforeClass() {
+ self::$logger = Logger::getLogger('test.Logger');
+ self::$layout = new LoggerMongoDBBsonLayout();
+ }
+
+ 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);
+ }
+
+ public function testFormatSimple() {
+ $event = new LoggerLoggingEvent(
+ 'testFqcn',
+ self::$logger,
+ LoggerLevel::getLevelWarn(),
+ 'test message'
+ );
+ $bsonifiedEvent = self::$layout->format($event);
+
+ $this->assertEquals('WARN', $bsonifiedEvent['level']);
+ $this->assertEquals('test message', $bsonifiedEvent['message']);
+ $this->assertEquals('test.Logger',
$bsonifiedEvent['loggerName']);
+ }
+
+ public function testFormatLocationInfo() {
+ $event = new LoggerLoggingEvent(
+ 'testFqcn',
+ self::$logger,
+ LoggerLevel::getLevelWarn(),
+ 'test message'
+ );
+ $bsonifiedEvent = self::$layout->format($event);
+
+ $this->assertEquals('NA', $bsonifiedEvent['fileName']);
+ $this->assertEquals('getLocationInformation',
$bsonifiedEvent['method']);
+ $this->assertEquals('NA', $bsonifiedEvent['lineNumber']);
+ $this->assertEquals('LoggerLoggingEvent',
$bsonifiedEvent['className']);
+ }
+
+ public function testFormatThrowableInfo() {
+ $event = new LoggerLoggingEvent(
+ 'testFqcn',
+ self::$logger,
+ LoggerLevel::getLevelWarn(),
+ 'test message',
+ microtime(true),
+ new Exception('test exception', 1)
+ );
+ $bsonifiedEvent = self::$layout->format($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']);
+ }
+
+ public function testFormatThrowableInfoWithInnerException() {
+ $event = new LoggerLoggingEvent(
+ 'testFqcn',
+ self::$logger,
+ LoggerLevel::getLevelWarn(),
+ 'test message',
+ microtime(true),
+ new Exception('test exeption', 1, new Exception('test
exception inner', 2))
+ );
+ $bsonifiedEvent = self::$layout->format($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']);
+ }
+
+
+ public function testBsonifySimple() {
+ $event = new LoggerLoggingEvent(
+ 'testFqcn',
+ self::$logger,
+ LoggerLevel::getLevelWarn(),
+ 'test message'
+ );
+ $bsonifiedEvent = self::$layout->bsonify($event);
+
+ $this->assertEquals('WARN', $bsonifiedEvent['level']);
+ $this->assertEquals('test message', $bsonifiedEvent['message']);
+ $this->assertEquals('test.Logger',
$bsonifiedEvent['loggerName']);
+ }
+
+ public function testBsonifyLocationInfo() {
+ $event = new LoggerLoggingEvent(
+ 'testFqcn',
+ self::$logger,
+ LoggerLevel::getLevelWarn(),
+ 'test message'
+ );
+ $bsonifiedEvent = self::$layout->bsonify($event);
+
+ $this->assertEquals('NA', $bsonifiedEvent['fileName']);
+ $this->assertEquals('getLocationInformation',
$bsonifiedEvent['method']);
+ $this->assertEquals('NA', $bsonifiedEvent['lineNumber']);
+ $this->assertEquals('LoggerLoggingEvent',
$bsonifiedEvent['className']);
+ }
+
+ public function testBsonifyThrowableInfo() {
+ $event = new LoggerLoggingEvent(
+ 'testFqcn',
+ self::$logger,
+ LoggerLevel::getLevelWarn(),
+ 'test message',
+ microtime(true),
+ new Exception('test exception', 1)
+ );
+ $bsonifiedEvent = self::$layout->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']);
+ }
+
+ public function testBsonifyThrowableInfoWithInnerException() {
+ $event = new LoggerLoggingEvent(
+ 'testFqcn',
+ self::$logger,
+ LoggerLevel::getLevelWarn(),
+ 'test message',
+ microtime(true),
+ new Exception('test exeption', 1, new Exception('test
exception inner', 2))
+ );
+ $bsonifiedEvent = self::$layout->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']);
+ }
+
+ public function testIsThreadInteger() {
+ $event = new LoggerLoggingEvent(
+ 'testFqcn',
+ self::$logger,
+ LoggerLevel::getLevelWarn(),
+ 'test message'
+ );
+ $bsonifiedEvent = self::$layout->bsonify($event);
+ $this->assertTrue(is_int($bsonifiedEvent['thread']));
+ }
+
+ public function testIsLocationInfoLineNumberIntegerOrNA() {
+ $event = new LoggerLoggingEvent(
+ 'testFqcn',
+ self::$logger,
+ LoggerLevel::getLevelWarn(),
+ 'test message'
+ );
+ $bsonifiedEvent = self::$layout->bsonify($event);
+ $this->assertTrue(is_int($bsonifiedEvent['lineNumber']) ||
$bsonifiedEvent['lineNumber'] == 'NA');
+ }
+
+ public function testIsThrowableInfoExceptionCodeInteger() {
+ $event = new LoggerLoggingEvent(
+ 'testFqcn',
+ self::$logger,
+ LoggerLevel::getLevelWarn(),
+ 'test message',
+ microtime(true),
+ new Exception('test exeption', 1, new Exception('test
exception inner', 2))
+ );
+ $bsonifiedEvent = self::$layout->bsonify($event);
+ $this->assertTrue(is_int($bsonifiedEvent['exception']['code']));
+ }
+}
+?>
\ No newline at end of file
Propchange:
logging/log4php/trunk/src/test/php/layouts/LoggerMongoDbBsonLayoutTest.php
------------------------------------------------------------------------------
svn:eol-style = native