Skip to content

Commit

Permalink
MGR-136
Browse files Browse the repository at this point in the history
  • Loading branch information
madness-inc committed May 20, 2022
1 parent 70b3a6e commit 278a867
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 67 deletions.
2 changes: 1 addition & 1 deletion application-home/conf/datasources/ds-logconfig.xml
Expand Up @@ -7,7 +7,7 @@
<title id="logConfig.edit" />
<meta-data bindClass="org.appng.application.manager.business.LogConfig$LogFile">
<field name="content" type="longtext" format="auto">
<label>log4j.properties</label>
<label>Log4j Configuration</label>
</field>
<field name="clusterWide" type="checkbox">
<label>logconfig.spreadClusterWide</label>
Expand Down
8 changes: 7 additions & 1 deletion pom.xml
Expand Up @@ -6,7 +6,7 @@
<artifactId>appng-manager</artifactId>
<packaging>jar</packaging>
<name>appNG Manager</name>
<version>1.19.2-SNAPSHOT</version>
<version>1.20.0-SNAPSHOT</version>
<description><![CDATA[Global appNG administration]]></description>
<url>https://appng.org</url>

Expand Down Expand Up @@ -363,6 +363,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
Expand Down
43 changes: 15 additions & 28 deletions src/main/java/org/appng/application/manager/business/LogConfig.java
Expand Up @@ -22,6 +22,7 @@
import javax.validation.constraints.NotNull;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.appng.api.ActionProvider;
import org.appng.api.ApplicationException;
import org.appng.api.DataContainer;
Expand All @@ -40,10 +41,12 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import lombok.Data;

@Component
public class LogConfig extends ServiceAware implements DataProvider, ActionProvider<LogFile> {

public static final String LOG4J_PROPS = "WEB-INF/conf/log4j.properties";
public @Value("${loggingConfig:WEB-INF/conf/log4j.properties}") String logConfigFile;

@Value("${platform." + Platform.Property.PLATFORM_ROOT_PATH + "}")
private String rootPath;
Expand All @@ -58,7 +61,6 @@ public DataContainer getData(Site site, Application application, Environment env
File configFile = getConfigFile();
String content = FileUtils.readFileToString(configFile, StandardCharsets.UTF_8);
logFile.setContent(content);
getService().createEvent(PlatformEvent.Type.INFO, "Changed " + LOG4J_PROPS);
} catch (IOException e) {
throw new ApplicationException(e);
}
Expand All @@ -67,22 +69,24 @@ public DataContainer getData(Site site, Application application, Environment env
}

public File getConfigFile() {
return new File(rootPath, LOG4J_PROPS);
return new File(rootPath, logConfigFile);
}

public void perform(Site site, Application application, Environment environment, Options options, Request request,
LogFile formBean, FieldProcessor fieldProcessor) {
File configFile = getConfigFile();
try {
String currentContent = FileUtils.readFileToString(configFile, StandardCharsets.UTF_8);
String newContent = formBean.getContent();
String content = formBean.getContent();
String separator = System.lineSeparator();
newContent = newContent.replaceAll("\r\n", separator);

LogConfigChangedEvent logConfigChangedEvent = new LogConfigChangedEvent(site.getName(), newContent,
configFile.getAbsolutePath());
if(StringUtils.LF.equals(separator)) {
content = content.replaceAll("\r\n", separator);
}

if (!currentContent.equals(newContent)) {
if (!currentContent.equals(content)) {
getService().createEvent(PlatformEvent.Type.INFO, "Changed " + configFile);
LogConfigChangedEvent logConfigChangedEvent = new LogConfigChangedEvent(site.getName(), content,
configFile.getAbsolutePath());
logConfigChangedEvent.perform(environment, site);
if (formBean.clusterWide) {
site.sendEvent(logConfigChangedEvent);
Expand All @@ -96,27 +100,10 @@ public void perform(Site site, Application application, Environment environment,
}
}

@Data
public static class LogFile {
private String content;
private @NotNull String content;
private boolean clusterWide = false;

@NotNull
public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public boolean isClusterWide() {
return clusterWide;
}

public void setClusterWide(boolean clusterWide) {
this.clusterWide = clusterWide;
}

}

}
Expand Up @@ -21,33 +21,42 @@
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.PropertyConfigurator;
import org.appng.api.BusinessException;
import org.appng.api.Environment;
import org.appng.api.InvalidConfigurationException;
import org.appng.api.Platform;
import org.appng.api.Scope;
import org.appng.api.messaging.Event;
import org.appng.api.model.Properties;
import org.appng.api.model.Site;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LogConfigChangedEvent extends Event implements Serializable {

private static final long serialVersionUID = 1L;
private final String content;
private final String configFilePath;

public LogConfigChangedEvent(String siteName, String content, String configFilePath) {
super(siteName);
this.content = content;
this.configFilePath = configFilePath;
}

public void perform(Environment environment, Site site) throws InvalidConfigurationException, BusinessException {
try {
Properties platformProps = environment.getAttribute(Scope.PLATFORM, Platform.Environment.PLATFORM_CONFIG);
String rootPath = platformProps.getString(Platform.Property.PLATFORM_ROOT_PATH);
File configFilePath = new File(rootPath, LogConfig.LOG4J_PROPS);
FileUtils.write(configFilePath, content, StandardCharsets.UTF_8);
new PropertyConfigurator().doConfigure(configFilePath.getAbsolutePath(), LogManager.getLoggerRepository());
File configFile = new File(configFilePath).getAbsoluteFile();
FileUtils.write(configFile, content, StandardCharsets.UTF_8);
if (configFilePath.contains("log4j2.xml")) {
org.apache.logging.log4j.core.LoggerContext loggerCtx = org.apache.logging.log4j.core.LoggerContext
.getContext(false);
org.apache.logging.log4j.core.config.Configuration config = new org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory()
.getConfiguration(loggerCtx, "appNG", configFile.toURI());
loggerCtx.reconfigure(config);
} else {
new org.apache.log4j.PropertyConfigurator().doConfigure(configFile.getAbsolutePath(),
org.apache.log4j.LogManager.getLoggerRepository());
}
log.info("Updated {}", configFile.getPath());
} catch (IOException e) {
throw new BusinessException("error while executing ReadloadConfigEvent", e);
}
Expand Down
Expand Up @@ -16,11 +16,8 @@
package org.appng.application.manager.business.webservice;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -32,8 +29,6 @@
import org.appng.api.model.Application;
import org.appng.api.model.Site;
import org.appng.api.model.Subject;
import org.appng.application.manager.business.LogConfig;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

Expand All @@ -48,25 +43,11 @@
*/

@Component
public class LogViewer implements Webservice, InitializingBean {
public class LogViewer implements Webservice {

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;

public void afterPropertiesSet() throws Exception {
try (InputStream propsIs = new FileInputStream(new File(rootPath, LogConfig.LOG4J_PROPS))) {
Properties log4jProps = new Properties();
log4jProps.load(propsIs);
logFileLocation = log4jProps.getProperty(APPNG_APPENDER);
logFileLocation = logFileLocation.replace(WEBAPP_ROOT, rootPath);
}
}
private @Value("${loggingFile:WEB-INF/log/appNG.log}") String logPath;
private @Value("${platform." + Platform.Property.PLATFORM_ROOT_PATH + "}") String rootPath;

public byte[] processRequest(Site site, Application application, Environment environment, Request request)
throws BusinessException {
Expand All @@ -76,7 +57,6 @@ public byte[] processRequest(Site site, Application application, Environment env
if (subject != null && subject.isAuthenticated()
&& request.getPermissionProcessor().hasPermission(PERM_LOG_VIEWER)) {
File logFile = getLogfile();

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

IOException e) {
} catch (IOException e) {
throw new BusinessException(e);
}
}
Expand All @@ -107,7 +85,7 @@ public byte[] processRequest(Site site, Application application, Environment env
}

File getLogfile() {
return new File(logFileLocation);
return new File(rootPath, logPath);
}

public String getContentType() {
Expand Down

0 comments on commit 278a867

Please sign in to comment.