Skip to content

Commit

Permalink
services - being able to externalize the logging configurations
Browse files Browse the repository at this point in the history
With the suggested modifications, we can define a bean as a string in
any XML configuration files, e.g.:

```
  <bean id="loggingConfigurationPath" class="java.lang.String">
    <constructor-arg value="/etc/geonetwork/log4j"/>
  </bean>
```

Then having a `/etc/geonetwork/log4j/` directory with some log4j
configurations like the default ones from the WEB-INF/classes
subdirectory, we can externalize from the webapp the logging
configuration.

Not having the previous bean defined will fall back onto the current
behaviour (e.g. using the files from the classpath).

the main advantage of this approach is that we can customize the logging
configuration without having to recompile GeoNetwork. We can even add
new configurations without atering the webapp content.
  • Loading branch information
pmauduit committed Jul 9, 2021
1 parent 0ffb466 commit a572112
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
24 changes: 23 additions & 1 deletion services/src/main/java/org/fao/geonet/api/site/LogUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
import org.fao.geonet.exceptions.OperationAbortedEx;
import org.fao.geonet.kernel.setting.Settings;
import org.fao.geonet.repository.SettingRepository;
import org.springframework.beans.BeansException;

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Optional;

/**
Expand All @@ -41,6 +44,7 @@
public class LogUtils {
public static final String DEFAULT_LOG_FILE = "log4j.xml";


/**
* Refresh logger configuration. If settings is not set in database, using default log4j.xml
* file. If requested file does not exist, using default log4j.xml file.
Expand All @@ -55,9 +59,27 @@ public static void refreshLogConfiguration() {
setting = settingOpt.get();
}


String loggingConfigurationPath;
try {
loggingConfigurationPath = (String) ApplicationContextHolder.get().getBean("loggingConfigurationPath");
} catch (BeansException e) {
loggingConfigurationPath = null;
}
// get log config from db settings
String log4jProp = setting != null ? setting.getValue() : DEFAULT_LOG_FILE;
URL url = LogUtils.class.getResource("/" + log4jProp);
URL url ;

if (loggingConfigurationPath != null) {
try {
url = Paths.get(loggingConfigurationPath, log4jProp).toUri().toURL();
} catch (MalformedURLException e) {
url = LogUtils.class.getResource("/" + log4jProp);
}
} else {
url = LogUtils.class.getResource("/" + log4jProp);
}

if (url != null) {
// refresh configuration
DOMConfigurator.configure(url);
Expand Down
13 changes: 10 additions & 3 deletions services/src/main/java/org/fao/geonet/api/site/LoggingApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.fao.geonet.api.API;
import org.fao.geonet.ApplicationContextHolder;
import org.fao.geonet.api.site.model.ListLogFilesResponse;
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.kernel.GeonetworkDataDirectory;
Expand Down Expand Up @@ -91,8 +91,15 @@ public List<ListLogFilesResponse.LogFileResponse> getLogFiles(
) throws Exception {
java.util.List<ListLogFilesResponse.LogFileResponse> logFileList =
new ArrayList<>();
String classesFolder = dataDirectory.getWebappDir() + "/WEB-INF/classes";
File folder = new File(classesFolder);
String loggingConfigurationFolder = dataDirectory.getWebappDir() + "/WEB-INF/classes";
// overrides if a "loggingConfigurationPath" bean is available
try {
loggingConfigurationFolder = (String) ApplicationContextHolder.get().getBean("loggingConfigurationPath");
} catch (Exception e) {
// stick with the folder in the classpath.
}

File folder = new File(loggingConfigurationFolder);

if (folder != null && folder.isDirectory()) {
Pattern pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
Expand Down

0 comments on commit a572112

Please sign in to comment.