gpt4 book ai didi

java - 配置 log4j 以使用 mongodb

转载 作者:太空宇宙 更新时间:2023-11-04 13:56:43 27 4
gpt4 key购买 nike

在 Java 项目中,我想将日志条目存储到 noSQL 数据库(例如 mongoDB)。这是我根据 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 添加到我的构建路径中。那么,我在这里缺少什么?也请随意提出替代方案。

最佳答案

我在你的 log4j 中发现端口号丢失了。示例log4j如下:

<Appenders>
<NoSql name="databaseAppender">
<MongoDb databaseName="DB name" collectionName="collection name"
server="localhost:27017" username="user name" password="DB password" />
</NoSql>
</Appenders>

<Loggers>
<Root level="trace">
<AppenderRef ref="databaseAppender" />
</Root>

</Loggers>

你可以在 https://github.com/PiyushMittl/logging-wrapper-2x2 找到我的 noSQL Appender 示例代码

引用:https://logging.apache.org/log4j/2.x/manual/appenders.html#NoSQLAppender

注意:上面的示例使用 log4j2.x api。

关于java - 配置 log4j 以使用 mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29749585/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com