Skip to content

Commit

Permalink
MGR-145
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Müller authored and Matthias Müller committed Oct 18, 2022
1 parent d033961 commit 77535b7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
Expand Up @@ -51,20 +51,17 @@
public class LogViewer implements Webservice, InitializingBean {

private static final String WEBAPP_ROOT = "${webapp.root}";
private static final String APPNG_APPENDER = "log4j.appender.appng.File";
protected static final String PERM_LOG_VIEWER = "platform.logfile";

@Value("${platform." + Platform.Property.PLATFORM_ROOT_PATH + "}")
private String rootPath;

private String logFileLocation;
private Properties log4jProps;

public void afterPropertiesSet() throws Exception {
try (InputStream propsIs = new FileInputStream(new File(rootPath, LogConfig.LOG4J_PROPS))) {
Properties log4jProps = new Properties();
log4jProps = new Properties();
log4jProps.load(propsIs);
logFileLocation = log4jProps.getProperty(APPNG_APPENDER);
logFileLocation = logFileLocation.replace(WEBAPP_ROOT, rootPath);
}
}

Expand All @@ -75,7 +72,7 @@ public byte[] processRequest(Site site, Application application, Environment env
Subject subject = environment.getSubject();
if (subject != null && subject.isAuthenticated()
&& request.getPermissionProcessor().hasPermission(PERM_LOG_VIEWER)) {
File logFile = getLogfile();
File logFile = getLogfile(request.getParameter("appender"));

int maxLines = 1000;
String parameter = request.getParameter("lines");
Expand All @@ -96,18 +93,25 @@ public byte[] processRequest(Site site, Application application, Environment env
}
}
}
} catch (

IOException e) {
} catch (IOException e) {
throw new BusinessException(e);
}
}
return result.toString().getBytes();

}

File getLogfile() {
return new File(logFileLocation);
File getLogfile(String appenderName) {
String path = getAppenderPath(appenderName);
if (null == path) {
path = getAppenderPath("appng");
}
return new File(path);
}

public String getAppenderPath(String appenderName) {
return log4jProps.getProperty(String.format("log4j.appender.%s.File", appenderName)).replace(WEBAPP_ROOT,
rootPath);
}

public String getContentType() {
Expand Down
Expand Up @@ -109,7 +109,7 @@ public byte[] processRequest(Site site, Application application, Environment env
addArchiveEntry(os, "lib" + EXT_TXT, jars);
addArchiveEntry(os, "directories" + EXT_TXT, listDirectory(rootPath));
addArchiveEntry(os, "threads" + EXT_TXT, threadViewer.getThreadDump(null, null));
addArchiveEntry(os, logViewer.getLogfile());
addArchiveEntry(os, logViewer.getLogfile(null));
addArchiveEntry(os, logConfig.getConfigFile());
addArchiveEntry(os, new File(rootPath, "WEB-INF/conf/appNG" + EXT_PROPERTIES));
return out.toByteArray();
Expand Down
Expand Up @@ -39,14 +39,16 @@ public void test() throws BusinessException {
PermissionProcessor permissionProcessor = Mockito.mock(PermissionProcessor.class);
Mockito.when(request.getPermissionProcessor()).thenReturn(permissionProcessor);
Mockito.when(request.getParameter("lines")).thenReturn("2");
Mockito.when(request.getParameter("appender")).thenReturn("appng");
Mockito.when(permissionProcessor.hasPermission(PERM_LOG_VIEWER)).thenReturn(true);
byte[] processRequest = processRequest(null, null, environment, request);
String result = new String(processRequest);
Assert.assertEquals("log4j.appender.appng.File = ${webapp.root}/appNG.log", result);
}

@Override
File getLogfile() {
File getLogfile(String appender) {
Assert.assertEquals("appng", appender);
try {
return new File(getClass().getClassLoader().getResource("log4j.properties").toURI());
} catch (URISyntaxException e) {
Expand Down

0 comments on commit 77535b7

Please sign in to comment.