Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-330

NoSQLAppender cannot insert log event to MongoDB

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.0-beta8
    • 2.0-rc1
    • Appenders
    • None
    • 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.

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            beamerblvd Nick Williams
            expert expert
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment