sequence

This article focuses on Dubbo’s LogbackContainer

LogbackContainer

Dubbo – 2.7.2 / dubbo – container/dubbo – container – logback/SRC/main/Java/org/apache/dubbo/container/logback LogbackContainer. Ja va

public class LogbackContainer implements Container {

    public static final String LOGBACK_FILE = "dubbo.logback.file";

    public static final String LOGBACK_LEVEL = "dubbo.logback.level";

    public static final String LOGBACK_MAX_HISTORY = "dubbo.logback.maxhistory";

    public static final String DEFAULT_LOGBACK_LEVEL = "ERROR";

    @Override
    public void start() {
        String file = ConfigUtils.getProperty(LOGBACK_FILE);
        if(file ! = null && file.length() > 0) { String level = ConfigUtils.getProperty(LOGBACK_LEVEL);if (StringUtils.isEmpty(level)) {
                level = DEFAULT_LOGBACK_LEVEL;
            }
            // maxHistory=0 Infinite history
            int maxHistory = StringUtils.parseInteger(ConfigUtils.getProperty(LOGBACK_MAX_HISTORY));

            doInitializer(file, level, maxHistory);
        }
    }

    @Override
    public void stop() {
    }

    /**
     * Initializer logback
     *
     * @param file
     * @param level
     * @param maxHistory
     */
    private void doInitializer(String file, String level, int maxHistory) {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
        rootLogger.detachAndStopAllAppenders();

        // appender
        RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
        fileAppender.setContext(loggerContext);
        fileAppender.setName("application");
        fileAppender.setFile(file);
        fileAppender.setAppend(true);

        // policy
        TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
        policy.setContext(loggerContext);
        policy.setMaxHistory(maxHistory);
        policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}");
        policy.setParent(fileAppender);
        policy.start();
        fileAppender.setRollingPolicy(policy);

        // encoder
        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setContext(loggerContext);
        encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n");
        encoder.start();
        fileAppender.setEncoder(encoder);

        fileAppender.start();

        rootLogger.addAppender(fileAppender);
        rootLogger.setLevel(Level.toLevel(level));
        rootLogger.setAdditive(false); }}Copy the code
  • LogbackContainer implements the Container interface. The start method checks whether dubo.logback. file is specified. If it is, dubo.logback. Then get dubo.logback. maxHistory and call doInitializer. To gain the rootLogger doInitializer method first, then perform detachAndStopAllAppenders, then configure RollingFileAppender, and specify the policy for TimeBasedRollingPolicy, And set the PatternLayoutEncoder, then start the fileAppender and add it to the rootLogger

The instance

Dubbo – 2.7.2 / dubbo – container/dubbo – container – logback/SRC/test/Java/org/apache/dubbo/container/logback/LogbackContainerTes t.java

public class LogbackContainerTest {

    private static final Logger logger = LoggerFactory.getLogger(LogbackContainerTest.class);

    @Test
    public void testContainer() {
        LogbackContainer container = (LogbackContainer) ExtensionLoader.getExtensionLoader(Container.class)
                .getExtension("logback");
        container.start();

        logger.debug("Test debug:" + this.getClass().getName());
        logger.warn("Test warn:" + this.getClass().getName());
        logger.info("Test info:" + this.getClass().getName());
        logger.error("Test error:"+ this.getClass().getName()); container.stop(); }}Copy the code
  • This is verified by starting the Container for logBack and then using logger output

summary

LogbackContainer implements the Container interface. The start method checks whether dubo.logback. file is specified. If it is, dubo.logback. Then get dubo.logback. maxHistory and call doInitializer. To gain the rootLogger doInitializer method first, then perform detachAndStopAllAppenders, then configure RollingFileAppender, and specify the policy for TimeBasedRollingPolicy, And set the PatternLayoutEncoder, then start the fileAppender and add it to the rootLogger

doc

  • LogbackContainer