我需要为动态加载和重新加载的类动态创建 Logback 记录器和各种位。我有这些课程的数量不定,尽管总共不到 150 个。性能是一个大问题,这是一个高度线程化和高度并发的环境,因此使用 MDC 的想法不会因为出现问题时每秒可能进行数千次调用而实现。我也不想直接将记录器传递给类,我希望他们像典型的那样使用 getLogger 将它们从空中拉出来。
每个类都必须将所有错误消息记录到它自己的错误文件中,并将所有跟踪消息记录到它自己的跟踪文件中。这两个文件都需要每晚滚动一次。此外,系统中记录的任何内容也必须最终出现在主系统日志中。
我创建了以下代码片段来尝试这个想法。看起来它会起作用,但结果不是我所期望的。下面是可以直接运行的测试程序:
package logbacktesting;
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.filter.LevelFilter;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
import ch.qos.logback.core.spi.FilterReply;
import ch.qos.logback.core.util.StatusPrinter;
import org.slf4j.LoggerFactory;
public class Main {
public static void main(String[] args) {
Logger templateLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.myapp");
LoggerContext loggerContext = templateLogger.getLoggerContext();
String[] nameList = new String[] {"test1.class", "test2.class"};
// Set up the pattern
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
encoder.start();
// Spin over the names to create all the needed objects
for(int i = 0; i < nameList.length; i++) {
String name = nameList[i];
// Set up the roll over policies and the name when it rolls over
TimeBasedRollingPolicy tracePolicy = new TimeBasedRollingPolicy();
tracePolicy.setContext( loggerContext );
tracePolicy.setFileNamePattern( name + "-Trace-%d{yyyy-MM-dd}.log" );
tracePolicy.setMaxHistory( 30 );
TimeBasedRollingPolicy errorPolicy = new TimeBasedRollingPolicy();
errorPolicy.setContext( loggerContext );
errorPolicy.setFileNamePattern( name + "-Error-logFile.%d{yyyy-MM-dd}.log" );
errorPolicy.setMaxHistory( 30 );
// Set up the filters to ensure things get split as expected
LevelFilter traceFilter = new LevelFilter();
traceFilter.setContext( loggerContext );
traceFilter.setLevel( Level.TRACE );
traceFilter.setOnMatch( FilterReply.ACCEPT );
traceFilter.setOnMismatch( FilterReply.DENY );
LevelFilter errorFilter = new LevelFilter();
errorFilter.setContext( loggerContext );
errorFilter.setLevel( Level.ERROR );
errorFilter.setOnMatch( FilterReply.ACCEPT );
errorFilter.setOnMismatch( FilterReply.DENY );
// Set up the trace and error appenders
RollingFileAppender rollingFileAppenderTrace = new RollingFileAppender();
rollingFileAppenderTrace.setContext(loggerContext);
rollingFileAppenderTrace.setName(name + "-Trace");
rollingFileAppenderTrace.setFile(name + "-Trace.log");
rollingFileAppenderTrace.setEncoder(encoder);
rollingFileAppenderTrace.setRollingPolicy( tracePolicy );
rollingFileAppenderTrace.addFilter( traceFilter );
tracePolicy.setParent( rollingFileAppenderTrace );
RollingFileAppender rollingFileAppenderError = new RollingFileAppender();
rollingFileAppenderError.setContext(loggerContext);
rollingFileAppenderError.setName(name + "-Error");
rollingFileAppenderError.setFile(name + "-Error.log");
rollingFileAppenderError.setEncoder(encoder);
rollingFileAppenderError.setRollingPolicy( errorPolicy );
rollingFileAppenderError.addFilter( errorFilter );
errorPolicy.setParent( rollingFileAppenderError );
// Start everything
tracePolicy.start();
errorPolicy.start();
traceFilter.start();
errorFilter.start();
rollingFileAppenderTrace.start();
rollingFileAppenderError.start();
// attach the rolling file appenders to the logger
Logger logger = (ch.qos.logback.classic.Logger) loggerContext.getLogger(name);
logger.addAppender(rollingFileAppenderTrace);
logger.addAppender(rollingFileAppenderError);
}
StatusPrinter.print(loggerContext);
// Test it to see what happens
for(int i = 0; i < nameList.length; i++) {
String name = nameList[i];
Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(name);
logger.error("error message" );
logger.warn("warn message" );
logger.info("info message" );
logger.debug("debug message" );
logger.trace("trace message" );
}
Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.myapp");
logger.error( "generic error message ");
}
}
这是 logback.xml 文件:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logFile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
如果您运行此代码,它将正确地将所有内容记录到主日志文件中。它将按预期创建 2 个跟踪和 2 个错误日志。但是所有的跟踪消息都不会出现在跟踪日志文件中,并且两条错误消息都会出现在 class2 错误文件中。它的作用就像所有 4 个附加程序都被添加到同一个记录器中,并且只有最后一个被添加的附加程序真正起作用。我很想知道我在这里做什么。谢谢!