在Java项目中,我想将我的日志条目存储到像mongoDB这样的noSQL数据库。这是我根据what apache claims to be working的log4j配置:
<?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,mongo-java-driver以及log4jcore和api包含在我的buildpath中。那么,我在这里错过了什么?随意提出替代方案。
答案 0 :(得分:0)
我在log4j中找到的是端口号丢失了。 示例log4j如下:
1
你可以在https://github.com/PiyushMittl/logging-wrapper-2x2
找到noSQL appender的示例代码参考:https://logging.apache.org/log4j/2.0/manual/appenders.html#NoSQLAppender
注意:上面的例子是使用log4j2.x api。