本文介紹了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打印的消息,以確定您的問題
這篇關于Logback:如何將日志寫入文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,