Log4j 2
  1. Log4j 2
  2. LOG4J2-330

NoSQLAppender cannot insert log event to MongoDB

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-beta8
    • Fix Version/s: 2.0-rc1
    • Component/s: Appenders
    • Labels:
      None
    • Environment:

      JDK 1.7, Mongo Driver 2.11.2

      Description

      Seems like NoSQLAppender can't insert any log events to Mongo due to lack of serializer for Level enum.

      2013-07-31 18:19:27,831 ERROR An exception occurred processing Appender databaseAppender java.lang.IllegalArgumentException: can't serialize class org.apache.logging.log4j.Level
      at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:270)
      at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:174)
      at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:120)
      at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
      at com.mongodb.OutMessage.putObject(OutMessage.java:289)
      at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:239)
      at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:204)
      at com.mongodb.DBCollection.insert(DBCollection.java:148)
      at com.mongodb.DBCollection.insert(DBCollection.java:91)
      at org.apache.logging.log4j.core.appender.db.nosql.mongo.MongoDBConnection.insertObject(MongoDBConnection.java:61)

      Here is my config

      <configuration status="WARN">
      <appenders>
      <NoSql name="databaseAppender" suppressExceptions="true">
      <MongoDb collectionName="applicationLog" factoryClassName="com.borsch.DBManager" factoryMethodName="getNewMongoClient" writeConcernConstant="NONE" username="root" password="qwe"/>
      </NoSql>
      </appenders>
      <loggers>
      <root level="error">
      <appender-ref ref="databaseAppender"/>
      </root>
      </loggers>
      </configuration>

      For now I used following workaround (code is in Scala)

       
      class LevelBSONEncoder extends org.bson.Transformer {
        def transform(o: Any): AnyRef =
          o match { case level: org.apache.logging.log4j.Level => new java.lang.Integer(level.intLevel()) }
      }
      
      // which is used like this
      
      org.bson.BSON.addEncodingHook(org.apache.logging.log4j.Level.ERROR.getClass, new LevelBSONEncoder())
      

      I think it should be done in appender.

        Activity

          People

          • Assignee:
            Nick Williams
            Reporter:
            Ruslan
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development