Open solu-jt0907 opened 3 months ago
Below code work well in logback 1.4.7 but not in logback 1.4.14.
I wanna know the cause of this occurrence.
the development environment is in java 17
import java.text.SimpleDateFormat; import java.util.Date; import org.slf4j.MDC; 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.sift.MDCBasedDiscriminator; import ch.qos.logback.classic.sift.SiftingAppender; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.Appender; import ch.qos.logback.core.Context; import ch.qos.logback.core.FileAppender; import ch.qos.logback.core.sift.AppenderFactory; import ch.qos.logback.core.util.Duration; public class LogbackMessageLogTest { private static final LoggerContext LOG_CTX = new LoggerContext(); private static final String LOGGER_NAME = "ONLMSG"; private static final String LOG_MDC_KEY = "logName"; public static void main(String[] args) { LogbackMessageLogTest test = new LogbackMessageLogTest(); test.test(); } public void test() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm"); long start = System.currentTimeMillis(); for (int i = 0; i < 100L; i++) { // logger String procTime = sdf.format(new Date(start + (i * 500L))); String date = procTime.substring(0, 8); String time = procTime.substring(8); String logName = date.concat("/ONLMSG_").concat(time); MDC.put(LOG_MDC_KEY, logName); Logger logger = getMessageLogger(); logger.error("Hello {}", i + 1); try { Thread.sleep(10); } catch (Exception e) { // ignore... } MDC.clear(); } } private Logger getMessageLogger() { Logger logger = LOG_CTX.exists(LOGGER_NAME); if (logger != null) { return logger; } synchronized (LogbackMessageLogTest.class) { logger = LOG_CTX.exists(LOGGER_NAME); if (logger != null) { return logger; } System.err.println("create logger..."); SiftingAppender sa = new SiftingAppender(); sa.setName(LOGGER_NAME + "SIFT"); sa.setContext(LOG_CTX); MDCBasedDiscriminator discriminator = new MDCBasedDiscriminator(); discriminator.setKey(LOG_MDC_KEY); discriminator.setDefaultValue(LOGGER_NAME + "_unknown"); discriminator.start(); sa.setDiscriminator(discriminator); sa.setMaxAppenderCount(10); sa.setTimeout(Duration.buildByMinutes(3L)); sa.setAppenderFactory(new AppenderFactory<ILoggingEvent>() { @Override public Appender<ILoggingEvent> buildAppender(Context context, String value) { String filename = "./" + value + ".log"; System.err.println("buildAppender " + filename); FileAppender<ILoggingEvent> appender = new FileAppender<>(); appender.setName("ONLMSG_" + value); appender.setContext(context); appender.setAppend(true); appender.setFile(filename); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(context); encoder.setPattern("%msg%n"); encoder.start(); appender.setEncoder(encoder); appender.start(); return appender; } }); sa.start(); logger = LOG_CTX.getLogger(LOGGER_NAME); logger.addAppender(sa); logger.setLevel(Level.INFO); } return logger; } }
Below code work well in logback 1.4.7 but not in logback 1.4.14.
I wanna know the cause of this occurrence.
the development environment is in java 17