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>*


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();
            }
        });*


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.

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!

Reply via email to