LoggerUtil { public static void setupMDC(String service) { MDC.put("apex.service", service); String value = StramClientUtils.getHostName(); MDC.put("apex.node", value == null ? "unknown" : value); value = System.getenv(Environment.USER.key()); if (value != null) { MDC.put("apex.user", value); } value = System.getenv(Environment.CONTAINER_ID.name()); if (value != null) { ContainerId containerId = ConverterUtils.toContainerId(value); ApplicationId applicationId = containerId.getApplicationAttemptId().getApplicationId(); MDC.put("apex.containerId", containerId.toString()); MDC.put("apex.applicationId", applicationId.toString()); } value = System.getProperty(APPLICATION_NAME.getLongName()); if (value != null) { MDC.put("apex.application", value); } }  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 testSetupMDC() { String appenderName = "mdcTestAppender"; String service = "test"; String application = "my application"; String args = "log4j.appender.mdcTestAppender=com.datatorrent.stram.util.LoggerUtilTest$TestAppender" + ",log4j.appender.mdcTestAppender.layout=org.apache.log4j.PatternLayout" + ",log4j.appender.mdcTestAppender.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n"; System.setProperty(APPLICATION_NAME.getLongName(), application); LoggerUtil.setupMDC(service); LoggerUtil.addAppenders(log4jLogger, new String[] {appenderName}, args, ","); TestAppender appender = (TestAppender)log4jLogger.getAppender(appenderName); LoggerUtilTest.logger.debug(args); assertEquals(service, appender.mdcProperties.get("apex.service")); String node = StramClientUtils.getHostName(); assertEquals(node == null ? "unknown" : node, appender.mdcProperties.get("apex.node")); assertEquals(application, appender.mdcProperties.get("apex.application")); assertTrue(LoggerUtil.removeAppender(log4jLogger, appenderName)); }