change log level at runtime

This commit is contained in:
Athou
2013-06-09 16:19:49 +02:00
parent 9da0f5e946
commit d212cf66c1
4 changed files with 40 additions and 0 deletions

View File

@@ -72,10 +72,12 @@ public class StartupBean {
@PostConstruct
private void init() {
startupTime = Calendar.getInstance().getTimeInMillis();
if (userDAO.getCount() == 0) {
initialData();
}
applicationSettingsService.applyLogLevel();
initSupportedLanguages();

View File

@@ -7,6 +7,8 @@ import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.log4j.Level;
@Entity
@Table(name = "APPLICATIONSETTINGS")
@SuppressWarnings("serial")
@@ -29,6 +31,7 @@ public class ApplicationSettings extends AbstractModel {
private boolean heavyLoad;
private boolean pubsubhubbub;
private boolean feedbackButton = true;
private String logLevel = Level.INFO.toString();
@Column(length = 255)
private String announcement;
@@ -162,4 +165,12 @@ public class ApplicationSettings extends AbstractModel {
this.databaseUpdateThreads = databaseUpdateThreads;
}
public String getLogLevel() {
return logLevel;
}
public void setLogLevel(String logLevel) {
this.logLevel = logLevel;
}
}

View File

@@ -1,8 +1,14 @@
package com.commafeed.backend.services;
import java.util.Enumeration;
import javax.ejb.Singleton;
import javax.inject.Inject;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.commafeed.backend.dao.ApplicationSettingsDAO;
import com.commafeed.backend.model.ApplicationSettings;
import com.google.common.collect.Iterables;
@@ -18,6 +24,7 @@ public class ApplicationSettingsService {
public void save(ApplicationSettings settings) {
this.settings = settings;
applicationSettingsDAO.saveOrUpdate(settings);
applyLogLevel();
}
public ApplicationSettings get() {
@@ -28,4 +35,17 @@ public class ApplicationSettingsService {
return settings;
}
@SuppressWarnings("unchecked")
public void applyLogLevel() {
String logLevel = get().getLogLevel();
Level level = Level.toLevel(logLevel);
Enumeration<Logger> loggers = LogManager.getCurrentLoggers();
while (loggers.hasMoreElements()) {
Logger logger = loggers.nextElement();
if (logger.getName().startsWith("com.commafeed")) {
logger.setLevel(level);
}
}
}
}