LoggerUtil { public static synchronized void changeLoggersLevel(@Nonnull Map<String, String> targetChanges) { for (Map.Entry<String, String> changeEntry : targetChanges.entrySet()) { Iterator<Map.Entry<String, Pattern>> patternsIterator = patterns.entrySet().iterator(); while ((patternsIterator.hasNext())) { Map.Entry<String, Pattern> entry = patternsIterator.next(); String finer = entry.getKey(); String wider = changeEntry.getKey(); if (finer.length() < wider.length()) { continue; } boolean remove = false; for (int i = 0; i < wider.length(); i++) { if (wider.charAt(i) == '*') { remove = true; break; } if (wider.charAt(i) != finer.charAt(i)) { break; } else if (i == wider.length() - 1) { remove = true; } } if (remove) { patternsIterator.remove(); patternLevel.remove(finer); } } } for (Map.Entry<String, String> loggerEntry : targetChanges.entrySet()) { String target = loggerEntry.getKey(); patternLevel.put(target, Level.toLevel(loggerEntry.getValue())); patterns.put(target, Pattern.compile(target)); } if (!patternLevel.isEmpty()) { @SuppressWarnings("unchecked") Enumeration<Logger> loggerEnumeration = LogManager.getCurrentLoggers(); while (loggerEnumeration.hasMoreElements()) { Logger classLogger = loggerEnumeration.nextElement(); Level oldLevel = classLogger.getLevel(); Level newLevel = getLevelFor(classLogger.getName()); if (newLevel != null && (oldLevel == null || !newLevel.equals(oldLevel))) { logger.info("changing level of {} to {}", classLogger.getName(), newLevel); classLogger.setLevel(newLevel); } } } }  static ImmutableMap<String, String> getPatternLevels(); static synchronized void changeLoggersLevel(@Nonnull Map<String, String> targetChanges); static synchronized ImmutableMap<String, String> getClassesMatching(@Nonnull String searchKey); static LogFileInformation getLogFileInformation(); static LogFileInformation getLogFileInformation(org.slf4j.Logger logger); static LogFileInformation getLogFileInformation(Logger logger); static boolean addAppender(String name, Properties properties); static boolean addAppender(Logger logger, String name, Properties properties); static boolean addAppenders(String[] names, String args, String propertySeparator); static boolean addAppenders(Logger logger, String[] names, String args, String propertySeparator); static boolean addAppenders(); static boolean removeAppender(String name); static boolean removeAppender(Logger logger, String name); static List<String> getAppendersNames(); static List<String> getAppendersNames(Logger logger); static void setupMDC(String service);  }
@Test public void testLoggerLevels() { org.slf4j.Logger sl4jLogger; org.apache.log4j.Logger log4jLogger; Map<String, String> changes = Maps.newHashMap(); changes.put("_.org.apache.*", "WARN"); changes.put("_.com.datatorrent.*", "WARN"); changes.put("_.com.datatorrent.stram.client.*", "INFO"); changes.put("_.com.datatorrent.stram.api.*", "DEBUG"); LoggerUtil.changeLoggersLevel(changes); sl4jLogger = LoggerFactory.getLogger("_.com.datatorrent.stram.client.DTConfiguration"); assertTrue(sl4jLogger.isInfoEnabled()); assertFalse(sl4jLogger.isDebugEnabled()); sl4jLogger = LoggerFactory.getLogger("_.com.datatorrent.stram.api.StramEvent"); assertTrue(sl4jLogger.isInfoEnabled()); assertTrue(sl4jLogger.isDebugEnabled()); sl4jLogger = LoggerFactory.getLogger("_.com.datatorrent.stram.StreamingAppMaster"); assertFalse(sl4jLogger.isInfoEnabled()); assertFalse(sl4jLogger.isDebugEnabled()); log4jLogger = LogManager.getLogger("_.com.datatorrent.stram.client.DTConfiguration"); assertSame(log4jLogger.getLevel(), Level.INFO); assertSame(log4jLogger.getEffectiveLevel(), Level.INFO); org.apache.log4j.Logger stramEventLogger = LogManager.getLogger("_.com.datatorrent.stram.api.StramEvent"); assertSame(stramEventLogger.getLevel(), Level.DEBUG); assertSame(stramEventLogger.getEffectiveLevel(), Level.DEBUG); org.apache.log4j.Logger streamingAppMasterLogger = LogManager.getLogger("_.com.datatorrent.stram.StreamingAppMaster"); assertSame(streamingAppMasterLogger.getLevel(), Level.WARN); assertSame(streamingAppMasterLogger.getEffectiveLevel(), Level.WARN); changes.clear(); changes.put("_.com.datatorrent.*", "DEBUG"); changes.put("_.com.datatorrent.stram.engine.*", "ERROR"); LoggerUtil.changeLoggersLevel(changes); sl4jLogger = LoggerFactory.getLogger("_.com.datatorrent.stram.client.DTConfiguration"); assertTrue(sl4jLogger.isInfoEnabled()); assertTrue(sl4jLogger.isDebugEnabled()); sl4jLogger = LoggerFactory.getLogger("_.com.datatorrent.stram.api.StramEvent"); assertTrue(sl4jLogger.isInfoEnabled()); assertTrue(sl4jLogger.isDebugEnabled()); sl4jLogger = LoggerFactory.getLogger("_.com.datatorrent.stram.StreamingAppMaster"); assertTrue(sl4jLogger.isInfoEnabled()); assertTrue(sl4jLogger.isDebugEnabled()); sl4jLogger = LoggerFactory.getLogger("_.com.datatorrent.stram.engine.StreamingContainer"); assertFalse(sl4jLogger.isInfoEnabled()); assertFalse(sl4jLogger.isDebugEnabled()); log4jLogger = LogManager.getLogger("_.com.datatorrent.stram.client.DTConfiguration"); assertSame(log4jLogger.getLevel(), Level.DEBUG); assertSame(log4jLogger.getEffectiveLevel(), Level.DEBUG); log4jLogger = LogManager.getLogger("_.com.datatorrent.stram.api.StramEvent"); assertSame(log4jLogger.getLevel(), Level.DEBUG); assertSame(log4jLogger.getEffectiveLevel(), Level.DEBUG); log4jLogger = LogManager.getLogger("_.com.datatorrent.stram.StreamingAppMaster"); assertSame(log4jLogger.getLevel(), Level.DEBUG); assertSame(log4jLogger.getEffectiveLevel(), Level.DEBUG); log4jLogger = LogManager.getLogger("_.com.datatorrent.stram.engine.StreamingContainer"); assertSame(log4jLogger.getLevel(), Level.ERROR); assertSame(log4jLogger.getEffectiveLevel(), Level.ERROR); }