Commit f7d71b7d authored by Michael Ritter's avatar Michael Ritter
Browse files

#43 Persist default log4j settings if none are found

parent 829ef374
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
package edu.umiacs.ace.util; package edu.umiacs.ace.util;
import edu.umiacs.ace.monitor.settings.SettingsParameter; import edu.umiacs.ace.monitor.settings.SettingsParameter;
import edu.umiacs.ace.monitor.settings.SettingsUtil;
import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.PropertyConfigurator;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
...@@ -40,6 +41,7 @@ import javax.servlet.ServletContextEvent; ...@@ -40,6 +41,7 @@ import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import javax.servlet.ServletContextListener;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Collectors;
/** /**
* Configure log4j on startup * Configure log4j on startup
...@@ -52,12 +54,18 @@ public class LogContextListener implements ServletContextListener { ...@@ -52,12 +54,18 @@ public class LogContextListener implements ServletContextListener {
public void contextInitialized( ServletContextEvent arg0 ) { public void contextInitialized( ServletContextEvent arg0 ) {
Properties log4jProp = new Properties(); Properties log4jProp = new Properties();
// what happens when this is empty?
EntityManager em = PersistUtil.getEntityManager(); EntityManager em = PersistUtil.getEntityManager();
TypedQuery<SettingsParameter> q = em.createNamedQuery("SettingsParameter.getAttrList", TypedQuery<SettingsParameter> q = em.createNamedQuery("SettingsParameter.getAttrList",
SettingsParameter.class); SettingsParameter.class);
q.setParameter("attr", "%log4j%"); q.setParameter("attr", "%log4j%");
List<SettingsParameter> settings = q.getResultList(); List<SettingsParameter> settings = q.getResultList();
if (settings.isEmpty()) {
settings = SettingsUtil.getDefaultSettings()
.stream()
.filter(s -> s.getName().startsWith("log4"))
.collect(Collectors.toList());
SettingsUtil.updateSettings(settings);
}
for(SettingsParameter s: settings) { for(SettingsParameter s: settings) {
log4jProp.setProperty(s.getName(), s.getValue()); log4jProp.setProperty(s.getName(), s.getValue());
...@@ -68,6 +76,5 @@ public class LogContextListener implements ServletContextListener { ...@@ -68,6 +76,5 @@ public class LogContextListener implements ServletContextListener {
@Override @Override
public void contextDestroyed( ServletContextEvent arg0 ) { public void contextDestroyed( ServletContextEvent arg0 ) {
// throw new UnsupportedOperationException("Not supported yet.");
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment