1

你们能演示一下如何在 MongoDB 的 Log4j2 (2.0-beta8) 中使用 NoSQLAppender 吗?

我正在使用 Scala(尽管我认为它不相关)。

我拥有的 Java Mongo DB 驱动程序是 2.11.2

Appender 抛出以下异常

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)
4

1 回答 1

2

好吧,我最终像这样为 Level 枚举实现了转换器

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())

如果有人知道更优雅的解决方案,请告诉我。

PS。嗯,那很快。我报告了这个错误https://issues.apache.org/jira/browse/LOG4J2-330并且维护人员确认建议的修复将被纳入回购。

于 2013-08-01T01:47:33.710 回答