logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ralph Goers <ralph.go...@dslextreme.com>
Subject Re: Two Question for NoSql MongoDB appender
Date Sat, 17 Aug 2013 04:25:41 GMT
FWIW, rather than drag in another dependency you might just want to catch RuntimeException
instead of MongoException.  Of course it will have to come after the catch block for the IllegalStateException.
And if you want you can just do 

if (e.getClass.getName().equals("com.mongodb.MongoException) {
  LOGGER.error(..)
} else {
  throw e;
}

if you want the same behavior as what you have below.

Ralph

On Aug 16, 2013, at 9:20 PM, Ralph Goers wrote:

> https://github.com/mongodb/mongo-java-driver/blob/master/src/main/com/mongodb/MongoException.java
shows an import for org.bson.BSONObject.  The pom.xml for mongo-java-driver doesn't contain
a transitive dependency for that and mvn dependency:tree on core doesn't show it.
> 
> Ralph
> 
> 
> On Aug 16, 2013, at 3:48 PM, Nick Williams wrote:
> 
>> Guys, I'm having a hard time with this simple fix that should have taken five minutes.
I'm getting test failures due to NoClassDefFoundErrors that shouldn't happen.
>> 
>> Here are the tests in error:
>>   CategoryTest.setupClass:52 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.testTraceWithException:415 ? NoClassDefFound com/mongodb/MongoExcep...
>>   LoggerTest.tearDown:75 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.testLog:459 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.tearDown:75 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.testRB1:295 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.tearDown:75 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.testRB2:314 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.tearDown:75 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.testRB3:334 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.tearDown:75 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.testTrace:388 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.tearDown:75 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.testAdditivity1:119 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.tearDown:75 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.testAdditivity2:144 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.tearDown:75 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.testAdditivity3:183 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.tearDown:75 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.testIsTraceEnabled:443 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.tearDown:75 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.testExists:355 ? NoClassDefFound com/mongodb/MongoException
>>   LoggerTest.tearDown:75 ? NoClassDefFound com/mongodb/MongoException
>>   LoggingTest.setupClass:44 ? NoClassDefFound com/mongodb/MongoException
>>   LoggingTest.cleanupClass:49 NullPointer
>> 
>> Here's the code I added:
>> 
>>                 try {
>>                     if (!database.authenticate(username, password.toCharArray()))
{
>>                         LOGGER.error("Failed to authenticate against MongoDB server.
Unknown error.");
>>                     }
>>                 } catch (MongoException e) {
>>                     LOGGER.error("Failed to authenticate against MongoDB: " + e.getMessage(),
e);
>>                 } catch (IllegalStateException e) {
>>                     LOGGER.error("Factory-supplied MongoDB database connection already
authenticated with different" +
>>                             "credentials but lost connection.");
>>                 }
>> 
>> Problem is, "database" is an instance of com.mongodb.DB, which is in the same JAR
as com.mongodb.MongoException. If I remove this code, the tests pass. How is this possible?
The DB instance is there with or without this new code, which means the JAR is on the classpath,
which means MongoException should be on the classpath.
>> 
>> Very confused...
>> 
>> Nick
>> 
>> On Aug 16, 2013, at 5:13 PM, Gary Gregory wrote:
>> 
>>> Thank you for the update Nick!
>>> :)
>>> Gary
>>> 
>>> 
>>> On Fri, Aug 16, 2013 at 5:39 PM, Nick Williams <nicholas@nicholaswilliams.net>
wrote:
>>> Answers inline.
>>> 
>>> On Aug 14, 2013, at 2:10 AM, YuCheng Ting wrote:
>>> 
>>>> Hi all,
>>>> 
>>>> I use beta8 log4j2 and wrote log4j2.xml like example in document (http://logging.apache.org/log4j/2.x/manual/appenders.html#NoSQLAppender
):
>>>> 
>>>> 
>>>> <appenders>
>>>>     <NoSql name="databaseAppender">
>>>>         <MongoDb databaseName="applicationDb" collectionName="applicationLog"
>>>>             server="mongo.example.org"
>>>>             username="loggingUser" password="abc123" />
>>>>     </NoSql>
>>>> </appenders>
>>> 
>>> Yep. That's correct.
>>> 
>>>> but I get the two exception:
>>>> 
>>>> 1, "can't serialize class org.apache.logging.log4j.Level" exception in (BasicBSONEncoder.java:270),
I read the code and add follow code in my project before logging, it gone.
>>>> 
>>>>         BSON.addEncodingHook(org.apache.logging.log4j.Level.class, new Transformer()
{
>>>>             @Override
>>>>             public Object transform(Object o) {
>>>>                 return o.toString();
>>>>             }
>>>>         });
>>> 
>>> This bug was reported and fixed a few weeks ago. The fix will be in the next
version, or you can compile locally. https://issues.apache.org/jira/browse/LOG4J2-330
>>> 
>>>> 2, “not authorized for insert test.log”, because my MongoDB need auth
to write, but the the "username" and "password" attributes in log4j2.xml is nearly useless,
after I read source code, found it NOT auth in 
>>>> 
>>>> org.apache.logging.log4j.core.appender.db.nosql.mongo.MongoDBProvider.createNoSQLProvider
>>>> source code line 181 after check username and password and com.mongodb.DB.authenticate
never be called.
>>> 
>>> This is a bug. I'm reporting it and fixing it now. The fix will be in the next
version, or you can compile locally (after I get the change committed, of course).
>>> 
>>>> so I change log4j2.xml :
>>>> 
>>>> <NoSql name="mongodb">
>>>>              <MongoDb collectionName="log" databaseName="test"
>>>>                          factoryClassName="com.yuchs.test.log4j.MainTest"
>>>>                         factoryMethodName="getMongoClient" />
>>>> </NoSql>
>>>> 
>>>> and create MongoClient and call com.mongodb.DB.authenticate method in com.yuchs.test.log4j.MainTest.getMongoClient.
>>>> 
>>>> 
>>>> This is my question:
>>>> 
>>>> 1, Why not add BSON.addEncodingHook code into log4j2 project to avoid basic
exception ? or another rule of method I don't know ?
>>>> 
>>>> 2, Why not auth DB in log4j2 project if password and username is set in log4j2.xml
? or another rule of method I don't know ?
>>>> 
>>>> Thanks everyone!
>>>> 
>>> 
>>> 
>>> 
>>> 
>>> -- 
>>> E-Mail: garydgregory@gmail.com | ggregory@apache.org 
>>> Java Persistence with Hibernate, Second Edition
>>> JUnit in Action, Second Edition
>>> Spring Batch in Action
>>> Blog: http://garygregory.wordpress.com 
>>> Home: http://garygregory.com/
>>> Tweet! http://twitter.com/GaryGregory
>> 
> 


Mime
View raw message