gpt4 book ai didi

java - Logback:如何将日志写入文件

转载 作者:行者123 更新时间:2023-12-01 06:23:08 28 4
gpt4 key购买 nike

我需要将 logback 添加到我的项目中。这是我的测试类:

public class Head implements Runnable {
Thread trd;

Head() {
trd = new Thread(this);
trd.start();
}
static Logger logger = LoggerFactory.getLogger(Head.class);

@Override
public void run() {
MDC.put("logName", "LOG_NAME");
logger.warn("test1");
logger.info("test2");
logger.error("test");
MDC.remove("logName");
}

public static void main(String[] args) {
String URL = "logback-test.xml";
System.out.println(ClassLoader.getSystemResource(URL));
int count = 1;
while(count<=3){
Head head = new Head();
count++;
}
}
}

这是我的 logback-test.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="FILE" class="ch.qos.logback.classic.sift.SiftingAppender">

<discriminator>
<key>logName</key>
<defaultValue>test</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${logName}" class="ch.qos.logback.core.FileAppender">
<file>D:/tmp/${logName}.log</file>
<append>false</append>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d [%thread] %level %mdc %logger{35} - %msg%n</pattern>
</layout>
</appender>
</sift>
</appender>

<root level="ALL">
<appender-ref ref="FILE" />
</root>
</configuration>

我添加了以下常规依赖项:

compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
compile group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.21'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7'
compile group: 'ch.qos.logback', name: 'logback-core', version: '1.1.7'
compile group: 'ch.qos.logback', name: 'logback-access', version: '1.1.7'

因此,我希望日志将保存到文件 D:/tmp/LOG_NAME.log 中,但日志仅写入控制台。你能帮忙解决这个问题吗?

添加调试选项后出现错误:

Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
file:/D:/my_tests/build/resources/main/logback-test.xml
groovy.lang.MissingMethodException: No signature of method: static ch.qos.logback.core.status.OnConsoleStatusListener.addNewInstanceToContext() is applicable for argument types: (ch.qos.logback.classic.LoggerContext) values: [ch.qos.logback.classic.LoggerContext[default]]
at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1500)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1486)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:53)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at ch.qos.logback.classic.gaffer.GafferConfigurator.run(GafferConfigurator.groovy:65)
at ch.qos.logback.classic.gaffer.GafferConfigurator$run.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
at ch.qos.logback.classic.gaffer.GafferConfigurator.run(GafferConfigurator.groovy:44)
at ch.qos.logback.classic.gaffer.GafferUtil.runGafferConfiguratorOn(GafferUtil.java:43)
at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:67)
at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:150)
at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84)
at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
at com.aerlingus.uita.utils.Head.<clinit>(Head.java:20)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)

最佳答案

我使用了您的代码(对日志文件路径进行了一些细微的更改,从 D:/tmp//tmp/)。它按预期工作。

这意味着您在此处提供的内容是正确的。该问题是由其他原因引起的。

鉴于您可以在控制台中看到日志消息,很可能 logback-test.xml 未被读取。要么类路径中有 logback.groovy,要么您的 logback-test.xml 甚至不在类路径中。

请提供系统属性logback.debug=true(例如通过-Dlogback.debug=true)并检查Logback在控制台中打印的消息,以识别您的问题

关于java - Logback:如何将日志写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37534044/

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