|
1 | 1 | package de.rwth.idsg.steve.web.controller; |
2 | 2 |
|
3 | 3 | import lombok.extern.slf4j.Slf4j; |
| 4 | +import org.apache.logging.log4j.LogManager; |
| 5 | +import org.apache.logging.log4j.core.Appender; |
| 6 | +import org.apache.logging.log4j.core.LoggerContext; |
| 7 | +import org.apache.logging.log4j.core.appender.FileAppender; |
| 8 | +import org.apache.logging.log4j.core.appender.MemoryMappedFileAppender; |
| 9 | +import org.apache.logging.log4j.core.appender.RandomAccessFileAppender; |
| 10 | +import org.apache.logging.log4j.core.appender.RollingFileAppender; |
| 11 | +import org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender; |
| 12 | +import org.apache.logging.log4j.core.impl.Log4jContextFactory; |
| 13 | +import org.apache.logging.log4j.core.selector.ContextSelector; |
| 14 | +import org.apache.logging.log4j.spi.LoggerContextFactory; |
4 | 15 | import org.springframework.stereotype.Controller; |
5 | 16 | import org.springframework.web.bind.annotation.RequestMapping; |
6 | 17 | import org.springframework.web.bind.annotation.RequestMethod; |
7 | 18 |
|
| 19 | +import javax.annotation.PostConstruct; |
8 | 20 | import javax.servlet.http.HttpServletResponse; |
9 | 21 | import java.io.IOException; |
10 | 22 | import java.io.PrintWriter; |
11 | 23 | import java.nio.charset.StandardCharsets; |
12 | 24 | import java.nio.file.Files; |
13 | 25 | import java.nio.file.Path; |
14 | 26 | import java.nio.file.Paths; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Random; |
15 | 30 |
|
16 | 31 | /** |
17 | 32 | * @author Sevket Goekay <goekay@dbis.rwth-aachen.de> |
|
22 | 37 | @RequestMapping(value = "/manager") |
23 | 38 | public class LogController { |
24 | 39 |
|
25 | | - private final Path logPath = Paths.get(System.getProperty("user.home"), "logs", "steve.log"); |
| 40 | + private List<Path> logPathList; |
| 41 | + private Random random = new Random(); |
| 42 | + private static final String ERROR_MESSAGE = "Not available"; |
| 43 | + |
| 44 | + @PostConstruct |
| 45 | + private void init() { |
| 46 | + logPathList = getActiveLogFilePaths(); |
| 47 | + } |
26 | 48 |
|
27 | 49 | @RequestMapping(value = "/log", method = RequestMethod.GET) |
28 | 50 | public void log(HttpServletResponse response) { |
| 51 | + response.setContentType("text/plain"); |
29 | 52 |
|
30 | 53 | try (PrintWriter writer = response.getWriter()) { |
31 | | - response.setContentType("text/plain"); |
32 | | - |
33 | | - Files.lines(logPath, StandardCharsets.UTF_8) |
34 | | - .forEach(writer::println); |
35 | | - |
| 54 | + Path p = decidePath(); |
| 55 | + if (p == null) { |
| 56 | + writer.write(ERROR_MESSAGE); |
| 57 | + } else { |
| 58 | + Files.lines(p, StandardCharsets.UTF_8) |
| 59 | + .forEach(writer::println); |
| 60 | + } |
36 | 61 | } catch (IOException e) { |
37 | 62 | log.error("Exception happened", e); |
38 | 63 | } |
39 | 64 | } |
40 | 65 |
|
41 | 66 | public String getLogFilePath() { |
42 | | - return logPath.toAbsolutePath().toString(); |
| 67 | + Path p = decidePath(); |
| 68 | + if (p == null) { |
| 69 | + return ERROR_MESSAGE; |
| 70 | + } else { |
| 71 | + return p.toAbsolutePath().toString(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // ------------------------------------------------------------------------- |
| 76 | + // Private helpers |
| 77 | + // ------------------------------------------------------------------------- |
| 78 | + |
| 79 | + private Path decidePath() { |
| 80 | + if (logPathList.isEmpty()) { |
| 81 | + return null; |
| 82 | + } else if (logPathList.size() == 1) { |
| 83 | + return logPathList.get(0); |
| 84 | + } else { |
| 85 | + return rollTheDice(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * If the user configured multiple file appenders, which log file should we choose? |
| 91 | + * Clearly, the only sane solution is rolling the dice. |
| 92 | + * Easter egg mode: On |
| 93 | + */ |
| 94 | + private Path rollTheDice() { |
| 95 | + log.trace("Rolling the dice..."); |
| 96 | + int index = random.nextInt(logPathList.size()); |
| 97 | + return logPathList.get(index); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * We cannot presume that the default file name/location setting won't be changed by the user. |
| 102 | + * Therefore, we should be able to retrieve that info from the underlying logging mechanism |
| 103 | + * by iterating over appenders. |
| 104 | + */ |
| 105 | + private List<Path> getActiveLogFilePaths() { |
| 106 | + LoggerContextFactory factory = LogManager.getFactory(); |
| 107 | + ContextSelector selector = ((Log4jContextFactory) factory).getSelector(); |
| 108 | + |
| 109 | + List<Path> fileNameList = new ArrayList<>(); |
| 110 | + for (LoggerContext ctx : selector.getLoggerContexts()) { |
| 111 | + for (Appender appender : ctx.getConfiguration().getAppenders().values()) { |
| 112 | + String fileName = extractFileName(appender); |
| 113 | + if (fileName != null) { |
| 114 | + fileNameList.add(Paths.get(fileName)); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + return fileNameList; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * File appender types do not share a "write-to-file" superclass. |
| 123 | + */ |
| 124 | + private String extractFileName(Appender a) { |
| 125 | + if (a instanceof FileAppender) { |
| 126 | + return ((FileAppender) a).getFileName(); |
| 127 | + |
| 128 | + } else if (a instanceof RollingFileAppender) { |
| 129 | + return ((RollingFileAppender) a).getFileName(); |
| 130 | + |
| 131 | + } else if (a instanceof RollingRandomAccessFileAppender) { |
| 132 | + return ((RollingRandomAccessFileAppender) a).getFileName(); |
| 133 | + |
| 134 | + } else if (a instanceof RandomAccessFileAppender) { |
| 135 | + return ((RandomAccessFileAppender) a).getFileName(); |
| 136 | + |
| 137 | + } else if (a instanceof MemoryMappedFileAppender) { |
| 138 | + return ((MemoryMappedFileAppender) a).getFileName(); |
| 139 | + |
| 140 | + } else { |
| 141 | + return null; |
| 142 | + } |
43 | 143 | } |
44 | 144 | } |
0 commit comments