您好,登錄后才能下訂單哦!
這篇文章給大家介紹dubbo中LogbackContainer的作用是什么,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
dubbo-2.7.2/dubbo-container/dubbo-container-logback/src/main/java/org/apache/dubbo/container/logback/LogbackContainer.java
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); } }
LogbackContainer實現了Container接口,其start方法判斷是否有指定dubbo.logback.file,如果有則進一步獲取dubbo.logback.level,如果為空則默認為ERROR級別,然后再獲取dubbo.logback.maxhistory,最后調用doInitializer;doInitializer方法先獲取rootLogger,然后執行detachAndStopAllAppenders,然后配置RollingFileAppender,并指定policy為TimeBasedRollingPolicy,并設置PatternLayoutEncoder,然后啟動fileAppender,并添加到rootLogger中
dubbo-2.7.2/dubbo-container/dubbo-container-logback/src/test/java/org/apache/dubbo/container/logback/LogbackContainerTest.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(); } }
這里通過啟動logback的container然后再使用logger輸出進行驗證
LogbackContainer實現了Container接口,其start方法判斷是否有指定dubbo.logback.file,如果有則進一步獲取dubbo.logback.level,如果為空則默認為ERROR級別,然后再獲取dubbo.logback.maxhistory,最后調用doInitializer;doInitializer方法先獲取rootLogger,然后執行detachAndStopAllAppenders,然后配置RollingFileAppender,并指定policy為TimeBasedRollingPolicy,并設置PatternLayoutEncoder,然后啟動fileAppender,并添加到rootLogger中
關于dubbo中LogbackContainer的作用是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。