BaleenLogging extends AbstractBaleenComponent { @Override public void configure(Configuration yaml) throws BaleenException { LOGGER.debug("Configuring logging"); List<Map<String, Object>> loggingConfig = yaml.getAsListOfMaps(CONFIG_LOGGING); List<BaleenLoggerBuilder> builders = new LinkedList<>(); for (Map<String, Object> config : loggingConfig) { BaleenLoggerBuilder logger = configureLogger(config); if (logger != null) { builders.add(logger); } } if (builders.isEmpty()) { LOGGER.warn("No logging configuration provided, using default console logger"); builders.add( new BaleenConsoleLoggerBuilder( DEFAULT_PATTERN, new MinMaxFilter(Level.INFO, Level.ERROR))); } int maxLogsInMemory = yaml.get(Integer.class, "logging.recent", EvictingQueueAppender.DEFAULT_MAX_SIZE); EvictingQueueBuilder evictingQueueBuilder = new EvictingQueueBuilder( DEFAULT_PATTERN, Collections.singletonList(new MinMaxFilter(Level.INFO, Level.ERROR)), maxLogsInMemory); builders.add(evictingQueueBuilder); recentLogAppender = evictingQueueBuilder.getAppender(); configure(builders); LOGGER.info("Logging has been configured"); }  @Override void configure(Configuration yaml); static Double parseToDouble(Object obj); void configure(List<BaleenLoggerBuilder> builders); static void main(String[] args); Collection<RecentLog> getRecentLogs(); static final String DEFAULT_PATTERN; }
@Test public void test() { BaleenLogging logging = new BaleenLogging(); InMemoryLoggingBuilder builder = new InMemoryLoggingBuilder(); logging.configure( Arrays.asList( builder, new BaleenConsoleLoggerBuilder(PATTERN, new MinMaxFilter(Level.INFO, Level.ERROR)))); LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger rootLogger = context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME); int count = 0; Iterator<Appender<ILoggingEvent>> it = rootLogger.iteratorForAppenders(); while (it.hasNext()) { Appender<ILoggingEvent> appender = it.next(); if (appender instanceof OutputStreamAppender) { Encoder<ILoggingEvent> e = ((OutputStreamAppender<ILoggingEvent>) appender).getEncoder(); assertTrue(e instanceof PatternLayoutEncoder); assertEquals(PATTERN, ((PatternLayoutEncoder) e).getPattern()); } count++; } assertEquals(3, count); }
@Test public void config() throws Exception { YamlConfiguration configuration = new YamlConfiguration(BaleenLoggingTest.class, "dummyConfig.yaml"); BaleenLogging logging = new BaleenLogging(); logging.configure(configuration); LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger rootLogger = context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME); int count = 0; Iterator<Appender<ILoggingEvent>> it = rootLogger.iteratorForAppenders(); while (it.hasNext()) { Appender<ILoggingEvent> appender = it.next(); switch (count) { case 0: assertTrue(appender instanceof ConsoleAppender); break; case 1: assertTrue(appender instanceof RollingFileAppender); break; case 2: assertTrue(appender instanceof FileAppender); assertFalse(appender instanceof RollingFileAppender); break; case 3: if (appender instanceof OutputStreamAppender) { Encoder<ILoggingEvent> e = ((OutputStreamAppender<ILoggingEvent>) appender).getEncoder(); assertTrue(e instanceof PatternLayoutEncoder); assertEquals(PATTERN, ((PatternLayoutEncoder) e).getPattern()); } break; case 4: if (appender instanceof EvictingQueueAppender) { assertEquals( EvictingQueueAppender.DEFAULT_MAX_SIZE, ((EvictingQueueAppender<ILoggingEvent>) appender).getMaxSize()); } else { fail("Unknown additional appender"); } break; case 5: if (!(appender instanceof InstrumentedAppender)) { fail("Unknown additional appender"); } break; default: fail("Too many appenders" + appender.getName()); } count++; } assertEquals(6, count); }