1
在一個Java項目中,我想將我的日誌條目存儲到像mongoDB這樣的noSQL數據庫中。下面是根據what apache claims to be working我log4j的配置:配置log4j使用mongodb
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<NoSql name="databaseAppender">
<MongoDb databaseName="testLogDB" collectionName="applicationLog" server="127.0.0.1"
username="user" password="123456" />
</NoSql>
</Appenders>
<Loggers>
<Root level="warn">
<AppenderRef ref="databaseAppender"/>
</Root>
</Loggers>
</Configuration>
和代碼片段來進行測試:
static final Logger logger = LogManager.getLogger(Log4j2Example.class.getName());
public static void main(String[] args) {
logger.error("Log4j Example.");
}
我得到這個錯誤:
Apr 20, 2015 5:56:33 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2015-04-20 17:56:33,475 ERROR Unable to invoke factory method in class class org.apache.logging.log4j.nosql.appender.mongodb.MongoDbProvider for element MongoDb. java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:135)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:766)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:706)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:698)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:698)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:358)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:161)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:359)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:420)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:138)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:207)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:160)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:492)
at logging.Log4j2Example.<clinit>(Log4j2Example.java:7)
Caused by: java.lang.NoSuchMethodError: com.mongodb.DB.isAuthenticated()Z
at org.apache.logging.log4j.nosql.appender.mongodb.MongoDbProvider.createNoSqlProvider(MongoDbProvider.java:178)
... 19 more
2015-04-20 17:56:33,484 ERROR NoSQL provider not specified for appender [databaseAppender].
2015-04-20 17:56:33,485 ERROR Null object returned for NoSql in Appenders.
2015-04-20 17:56:33,504 ERROR Unable to locate appender databaseAppender for logger
我已經包含的log4j的NoSQL,蒙戈-java-driver,以及log4jcore和api到我的構建路徑。那麼,我在這裏錯過了什麼?隨意建議替代品。
對於那些將來可能面臨這種情況的人,我一直在使用mongo-java-driver-3.0.0和log4j-nosql-2.2,我發現它們在幾種方法中不兼容。向後移動到mongo-java-driver-2.13.0解決了這個問題。 –