在Spring [Boot]下,如何使用JavaConfig或某种基于注释的配置以编程方式配置Logback。 我希望尽可能避免使用XML文件
欢迎示例
我不在寻找指向以下内容的链接。我已经有这些了。假设我有这些,我的问题是如何达到这个问题的目标。
How to configure Lockback in Java when NOT under Spring
How to set up Logback under Spring or Spring Boot using XML
答案 0 :(得分:1)
是的。如果要以编程方式进行配置,则有可能。例如,以下代码对我有用:
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.FileAppender;
import org.slf4j.LoggerFactory;
public class LoggerUtil {
public static void main(String[] args) {
Logger example1 = createLoggerFor("example1", "example1.log");
Logger example2 = createLoggerFor("example2", "example2.log");
example1.info("test-example1");
example2.info("test-example2");
}
private static Logger createLoggerFor(String string, String file) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
patternLayoutEncoder.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
patternLayoutEncoder.setContext(loggerContext);
patternLayoutEncoder.start();
FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
fileAppender.setFile(file);
fileAppender.setEncoder(patternLayoutEncoder);
fileAppender.setContext(loggerContext);
fileAppender.start();
Logger logger = (Logger) LoggerFactory.getLogger(string);
logger.addAppender(fileAppender);
logger.setLevel(Level.DEBUG);
logger.setAdditive(false); /* set to true if root should log too */
return logger;
}
}