Logback:如何将日志写入文件

2022-04-19 00:00:00 gradle java logback

我需要将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>

我添加了以下groovy依赖项:

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打印的消息,以确定您的问题

相关文章