|
| 1 | +package de.rwth.idsg.steve.utils; |
| 2 | + |
| 3 | +import com.google.common.base.Optional; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.apache.logging.log4j.LogManager; |
| 6 | +import org.apache.logging.log4j.core.Appender; |
| 7 | +import org.apache.logging.log4j.core.LoggerContext; |
| 8 | +import org.apache.logging.log4j.core.appender.FileAppender; |
| 9 | +import org.apache.logging.log4j.core.appender.MemoryMappedFileAppender; |
| 10 | +import org.apache.logging.log4j.core.appender.RandomAccessFileAppender; |
| 11 | +import org.apache.logging.log4j.core.appender.RollingFileAppender; |
| 12 | +import org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender; |
| 13 | +import org.apache.logging.log4j.core.impl.Log4jContextFactory; |
| 14 | +import org.apache.logging.log4j.core.selector.ContextSelector; |
| 15 | +import org.apache.logging.log4j.spi.LoggerContextFactory; |
| 16 | + |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.nio.file.Paths; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Random; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Sevket Goekay <goekay@dbis.rwth-aachen.de> |
| 25 | + * @since 05.11.2015 |
| 26 | + */ |
| 27 | +@Slf4j |
| 28 | +public class LogFileRetriever { |
| 29 | + |
| 30 | + private List<Path> logPathList; |
| 31 | + private Random random = new Random(); |
| 32 | + |
| 33 | + public LogFileRetriever() { |
| 34 | + logPathList = getActiveLogFilePaths(); |
| 35 | + } |
| 36 | + |
| 37 | + public Optional<Path> getPath() { |
| 38 | + Path p; |
| 39 | + if (logPathList.isEmpty()) { |
| 40 | + p = null; |
| 41 | + } else if (logPathList.size() == 1) { |
| 42 | + p = logPathList.get(0); |
| 43 | + } else { |
| 44 | + p = rollTheDice(); |
| 45 | + } |
| 46 | + return Optional.fromNullable(p); |
| 47 | + } |
| 48 | + |
| 49 | + public String getLogFilePathOrErrorMessage() { |
| 50 | + Optional<Path> p = getPath(); |
| 51 | + if (p.isPresent()) { |
| 52 | + return p.get().toAbsolutePath().toString(); |
| 53 | + } else { |
| 54 | + return getErrorMessage(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public String getErrorMessage() { |
| 59 | + return "Not available"; |
| 60 | + } |
| 61 | + |
| 62 | + // ------------------------------------------------------------------------- |
| 63 | + // Private helpers |
| 64 | + // ------------------------------------------------------------------------- |
| 65 | + |
| 66 | + /** |
| 67 | + * If the user configured multiple file appenders, which log file should we choose? |
| 68 | + * Clearly, the only sane solution is rolling the dice. |
| 69 | + * Easter egg mode: On |
| 70 | + */ |
| 71 | + private Path rollTheDice() { |
| 72 | + log.trace("Rolling the dice..."); |
| 73 | + int index = random.nextInt(logPathList.size()); |
| 74 | + return logPathList.get(index); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * We cannot presume that the default file name/location setting won't be changed by the user. |
| 79 | + * Therefore, we should be able to retrieve that info from the underlying logging mechanism |
| 80 | + * by iterating over appenders. |
| 81 | + */ |
| 82 | + private List<Path> getActiveLogFilePaths() { |
| 83 | + LoggerContextFactory factory = LogManager.getFactory(); |
| 84 | + ContextSelector selector = ((Log4jContextFactory) factory).getSelector(); |
| 85 | + |
| 86 | + List<Path> fileNameList = new ArrayList<>(); |
| 87 | + for (LoggerContext ctx : selector.getLoggerContexts()) { |
| 88 | + for (Appender appender : ctx.getConfiguration().getAppenders().values()) { |
| 89 | + String fileName = extractFileName(appender); |
| 90 | + if (fileName != null) { |
| 91 | + fileNameList.add(Paths.get(fileName)); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + return fileNameList; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * File appender types do not share a "write-to-file" superclass. |
| 100 | + */ |
| 101 | + private String extractFileName(Appender a) { |
| 102 | + if (a instanceof FileAppender) { |
| 103 | + return ((FileAppender) a).getFileName(); |
| 104 | + |
| 105 | + } else if (a instanceof RollingFileAppender) { |
| 106 | + return ((RollingFileAppender) a).getFileName(); |
| 107 | + |
| 108 | + } else if (a instanceof RollingRandomAccessFileAppender) { |
| 109 | + return ((RollingRandomAccessFileAppender) a).getFileName(); |
| 110 | + |
| 111 | + } else if (a instanceof RandomAccessFileAppender) { |
| 112 | + return ((RandomAccessFileAppender) a).getFileName(); |
| 113 | + |
| 114 | + } else if (a instanceof MemoryMappedFileAppender) { |
| 115 | + return ((MemoryMappedFileAppender) a).getFileName(); |
| 116 | + |
| 117 | + } else { |
| 118 | + return null; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments