我正在使用logback,我试图在我的Java程序中以编程方式设置日志文件名(类似于以编程方式设置Logback Appender路径),我尝试按如下方式调整该解决方案:
在logback-test.xml中:
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>log/${log_file_name}.log</file>
...
Run Code Online (Sandbox Code Playgroud)
然后再在我的Java程序中:
String logFileName = "" + System.currentTimeMillis(); // just for example
System.setProperty("log_file_name", logFileName);
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);
Run Code Online (Sandbox Code Playgroud)
然而,结果是两个日志,一个完整并按我想要的名称命名,例如"1319041145343.log",另一个是空的并命名为"log_file_name_IS_UNDEFINED.log".如何阻止创建其他空日志文件?