2010-09-27 73 views
18

我試圖以編程方式設置Logback appender路徑。 (RollingFileAppender與FixedWindowRollingPolicy是精確的)以編程方式設置Logback Appender路徑

我這樣做是因爲我想使我的用戶設置首選項對話框(Eclipse RCP的)

我已經試過這樣的日誌路徑,但我不會改變從什麼配置文件中定義的日誌路徑:

Logger logback_logger = (ch.qos.logback.classic.Logger)LoggerFactory 
    .getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); 
RollingFileAppender<ILoggingEvent> rfappender = 
    (RollingFileAppender<ILoggingEvent>)logback_logger.getAppender("FILE"); 
rfappender.setFile(newFile); 
FixedWindowRollingPolicy rollingPolicy = 
    (FixedWindowRollingPolicy)rfappender.getRollingPolicy(); 
rollingPolicy.setFileNamePattern(newPattern); 

回答

13

使用系統屬性和重新加載配置文件似乎清潔:

變化logback.xml文件:

<file>${log_path:-}myfile.log</file> 
.... 
<FileNamePattern>${log_path:-}myfile.%i.log</FileNamePattern> 

這將設置默認位置的工作目錄。 然後,使用:

System.setProperty("log_path", my_log_path); 

//Reload: 
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); 
ContextInitializer ci = new ContextInitializer(lc); 
lc.reset(); 
try { 
    //I prefer autoConfig() over JoranConfigurator.doConfigure() so I wouldn't need to find the file myself. 
    ci.autoConfig(); 
} catch (JoranException e) { 
    // StatusPrinter will try to log this 
    e.printStackTrace(); 
} 
StatusPrinter.printInCaseOfErrorsOrWarnings(lc); 
+2

使用ContextInitializer是非常不正確的。看到我的答案是正確的方法。 – Ceki 2011-10-19 17:07:46

+1

很酷的東西!我改變了一件事:我使用lc.putProperty(「log_path」,my_log_path)而不是System.setProperty。它看起來更好,因爲使用0個全局變量。 – Sasha 2015-08-04 17:11:03

5

望着的logback代碼,我已經找到了解決辦法:

rollingPolicy.stop(); 
rfappender.stop(); 
rollingPolicy.start(); 
rfappender.start(); 

這會導致Logback使用新的定義。不過,它仍然像是一種解決方法。

26

一旦編程配置的appender,你需要調用它的start()方法。如果appender具有子組件,請首先在子組件上調用start()。然後,將appender添加到您選擇的記錄器中。

下面是一個例子:

import ch.qos.logback.classic.Logger; 
import ch.qos.logback.classic.encoder.PatternLayoutEncoder; 
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy; 
import ch.qos.logback.core.rolling.RollingFileAppender; 
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy; 
import ch.qos.logback.core.util.StatusPrinter; 
import org.slf4j.LoggerFactory; 
import ch.qos.logback.classic.LoggerContext; 

public class Main { 
    public static void main(String[] args) { 
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); 

    RollingFileAppender rfAppender = new RollingFileAppender(); 
    rfAppender.setContext(loggerContext); 
    rfAppender.setFile("testFile.log"); 
    FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy(); 
    rollingPolicy.setContext(loggerContext); 
    // rolling policies need to know their parent 
    // it's one of the rare cases, where a sub-component knows about its parent 
    rollingPolicy.setParent(rfAppender); 
    rollingPolicy.setFileNamePattern("testFile.%i.log.zip"); 
    rollingPolicy.start(); 

    SizeBasedTriggeringPolicy triggeringPolicy = new ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy(); 
    triggeringPolicy.setMaxFileSize("5MB"); 
    triggeringPolicy.start(); 

    PatternLayoutEncoder encoder = new PatternLayoutEncoder(); 
    encoder.setContext(loggerContext); 
    encoder.setPattern("%-4relative [%thread] %-5level %logger{35} - %msg%n"); 
    encoder.start(); 

    rfAppender.setEncoder(encoder); 
    rfAppender.setRollingPolicy(rollingPolicy); 
    rfAppender.setTriggeringPolicy(triggeringPolicy); 

    rfAppender.start(); 

    // attach the rolling file appender to the logger of your choice 
    Logger logbackLogger = loggerContext.getLogger("Main"); 
    logbackLogger.addAppender(rfAppender); 

    // OPTIONAL: print logback internal status messages 
    StatusPrinter.print(loggerContext); 

    // log something 
    logbackLogger.debug("hello"); 
    } 
} 

上面的代碼是由所述的logback的XML配置器,即Joran,當解析RollingFixedWindow.xml文件所採取的步驟的方案表達。

+4

(這對於記錄日誌內容有點尷尬,但是)這不是我想要做的 - 我想用XML配置我的記錄器,並且只通過代碼更改位置。這樣,高級用戶可以控制細粒度的日誌記錄屬性,新手用戶可以使用該UI。通過代碼重新啓動appender;使用系統屬性和ContextInitializer效果更好,硬編碼更少,爲什麼這是不正確的? – yshalbar 2012-01-25 10:28:42

+0

這種方法在最近發佈的Logback中仍然有效嗎? – 2014-04-02 08:45:51

+0

「RollingFixedWindow.xml」的鏈接已損壞 – 2014-04-02 08:47:02