private void registerLogFileTailThreads() throws IOException { final Collection<String> outputFileNames = new HashSet<>(); final Collection<Appender> loggerAppenders = this.loggerContext.getConfiguration().getAppenders().values(); loggerAppenders.forEach(appender -> { if (appender instanceof FileAppender) { outputFileNames.add(((FileAppender) appender).getFileName()); } else if (appender instanceof RandomAccessFileAppender) { outputFileNames.add(((RandomAccessFileAppender) appender).getFileName()); } else if (appender instanceof RollingFileAppender) { outputFileNames.add(((RollingFileAppender) appender).getFileName()); } else if (appender instanceof MemoryMappedFileAppender) { outputFileNames.add(((MemoryMappedFileAppender) appender).getFileName()); } else if (appender instanceof RollingRandomAccessFileAppender) { outputFileNames.add(((RollingRandomAccessFileAppender) appender).getFileName()); } }); outputFileNames.forEach(s -> { final Tailer t = new Tailer(new File(s), new LogTailerListener(), 100, false, true); final Thread thread = new Thread(t); thread.setPriority(Thread.MIN_PRIORITY); thread.setName(s); thread.start(); }); }
/** * File appender types do not share a "write-to-file" superclass. */ private String extractFileName(Appender a) { if (a instanceof FileAppender) { return ((FileAppender) a).getFileName(); } else if (a instanceof RollingFileAppender) { return ((RollingFileAppender) a).getFileName(); } else if (a instanceof RollingRandomAccessFileAppender) { return ((RollingRandomAccessFileAppender) a).getFileName(); } else if (a instanceof RandomAccessFileAppender) { return ((RandomAccessFileAppender) a).getFileName(); } else if (a instanceof MemoryMappedFileAppender) { return ((MemoryMappedFileAppender) a).getFileName(); } else { return null; } }
/** * <p> * This method loads the required properties into log4j for the logger * </p> * * @param logFileDir * Log file directory * @param taskAttemptID * The task attempt id * * @throws IOException * If any error occurs * @throws URISyntaxException * @throws SAXException * @throws ParserConfigurationException * @throws TransformerException */ public static void loadLogger(String logFileDir, String taskAttemptID) throws IOException, URISyntaxException, ParserConfigurationException, SAXException, TransformerException { LoggerContext ctx = (LoggerContext) LogManager.getContext(false); ctx.reconfigure(); Configuration config = ctx.getConfiguration(); MemoryMappedFileAppender memoryMappedfileAppender = createMemoryMappedFileAppender(config, LOG_APPENDER_NAME + taskAttemptID, logFileDir, taskAttemptID, 0); memoryMappedfileAppender.start(); AppenderRef[] ar = new AppenderRef [1]; ar[0] = AppenderRef.createAppenderRef(LOG_APPENDER_NAME + taskAttemptID , Level.INFO, null); LoggerConfig lgf = LoggerConfig.createLogger("false",Level.INFO , LOG_CATEGORY + taskAttemptID , null, ar, null, config, null); config.addLogger(LOG_CATEGORY + taskAttemptID, lgf); ctx.getLogger(LOG_CATEGORY + taskAttemptID).addAppender(memoryMappedfileAppender); ctx.updateLoggers(); ctx.start(); mapReduceLoggers = new ArrayList<Logger>(1); mapReduceLoggers.add(LogManager.getLogger(LOG_CATEGORY + taskAttemptID)); LOG.debug("Finished loading logger"); }
private static MemoryMappedFileAppender createMemoryMappedFileAppender(Configuration config, String appenderName, String logFileDir, String taskAttemptID, Object loggerNumber) { StringBuilder logFileName = new StringBuilder(JobUtil.getAndReplaceHolders(logFileDir)); if (!(loggerNumber instanceof Integer && (Integer) loggerNumber == 0)) { logFileName.append(loggerNumber).append("-"); } logFileName.append(IPRetriever.getCurrentIP()).append(SEPARATOR_UNDERSCORE).append(taskAttemptID).append(LOG_FILE_EXT); // pattern layout PatternLayout pl = PatternLayout.createLayout(LOG_PATTERN, config, null,Charsets.UTF_8 ,false, false, null, null); return MemoryMappedFileAppender.createAppender(logFileName.toString(), "append", appenderName, "false", "33554432", null, pl, null, null, null, config); }
public static void addFileAppender(String filename) { if (instance == null) { LOGGER.error("CustomFileAppender hasn't been initalized, ignoring output " + filename); return; } final AbstractOutputStreamAppender<? extends OutputStreamManager> appender; if (mmap_appender) appender = MemoryMappedFileAppender.createAppender(filename, "false", filename, "false", "8192", "false", instance.getLayout(), instance.getFilter(), "false", "false", new DefaultConfiguration()); else appender = FileAppender.createAppender(filename, "false", "false", filename, "false", "false", "true", "8192", instance.getLayout(), instance.getFilter(), "false", "false", new DefaultConfiguration()); LOGGER.info("registering custom logfile '{}'", appender); instance.appenders.add(appender); }
/** * Gets configuration as JSON. * Depends on the log4j core API. * * @param request the request * @param response the response * @return the configuration * @throws Exception the exception */ @GetMapping(value = "/getConfiguration") @ResponseBody public Map<String, Object> getConfiguration(final HttpServletRequest request, final HttpServletResponse response) throws Exception { ensureEndpointAccessIsAuthorized(request, response); Assert.notNull(this.loggerContext); final Collection<Map<String, Object>> configuredLoggers = new HashSet<>(); getLoggerConfigurations().forEach(config -> { final Map<String, Object> loggerMap = new HashMap<>(); loggerMap.put("name", StringUtils.defaultIfBlank(config.getName(), LOGGER_NAME_ROOT)); loggerMap.put("state", config.getState()); if (config.getProperties() != null) { loggerMap.put("properties", config.getProperties()); } loggerMap.put("additive", config.isAdditive()); loggerMap.put("level", config.getLevel().name()); final Collection<String> appenders = new HashSet<>(); config.getAppenders().keySet().stream().map(key -> config.getAppenders().get(key)).forEach(appender -> { final ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE); builder.append("name", appender.getName()); builder.append("state", appender.getState()); builder.append("layoutFormat", appender.getLayout().getContentFormat()); builder.append("layoutContentType", appender.getLayout().getContentType()); if (appender instanceof FileAppender) { builder.append(FILE_PARAM, ((FileAppender) appender).getFileName()); builder.append(FILE_PATTERN_PARAM, "(none)"); } if (appender instanceof RandomAccessFileAppender) { builder.append(FILE_PARAM, ((RandomAccessFileAppender) appender).getFileName()); builder.append(FILE_PATTERN_PARAM, "(none)"); } if (appender instanceof RollingFileAppender) { builder.append(FILE_PARAM, ((RollingFileAppender) appender).getFileName()); builder.append(FILE_PATTERN_PARAM, ((RollingFileAppender) appender).getFilePattern()); } if (appender instanceof MemoryMappedFileAppender) { builder.append(FILE_PARAM, ((MemoryMappedFileAppender) appender).getFileName()); builder.append(FILE_PATTERN_PARAM, "(none)"); } if (appender instanceof RollingRandomAccessFileAppender) { builder.append(FILE_PARAM, ((RollingRandomAccessFileAppender) appender).getFileName()); builder.append(FILE_PATTERN_PARAM, ((RollingRandomAccessFileAppender) appender).getFilePattern()); } appenders.add(builder.build()); }); loggerMap.put("appenders", appenders); configuredLoggers.add(loggerMap); }); final Map<String, Object> responseMap = new HashMap<>(); responseMap.put("loggers", configuredLoggers); return responseMap; }