内容简介:本文主要研究一下dubbo的LogbackContainerdubbo-2.7.2/dubbo-container/dubbo-container-logback/src/main/java/org/apache/dubbo/container/logback/LogbackContainer.javadubbo-2.7.2/dubbo-container/dubbo-container-logback/src/test/java/org/apache/dubbo/container/logback/Log
序
本文主要研究一下dubbo的LogbackContainer
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中
doc
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web 2.0 Architectures
Duane Nickull、Dion Hinchcliffe、James Governor / O'Reilly / 2009 / USD 34.99
The "Web 2.0" phenomena has become more pervasive than ever before. It is impacting the very fabric of our society and presents opportunities for those with knowledge. The individuals who understand t......一起来看看 《Web 2.0 Architectures》 这本书的介绍吧!