[New] It is now possible to override a logging

Just add a logback.xml file with the appropriate configuration in the
config folder and it will be loaded automatically.
This commit is contained in:
Robert von Burg 2017-09-20 17:41:24 +02:00
parent 060692a5c1
commit fd4d428000
3 changed files with 55 additions and 1 deletions

View File

@ -0,0 +1,48 @@
package li.strolch.agent.api;
import java.io.File;
import java.net.MalformedURLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.util.ContextInitializer;
import ch.qos.logback.core.joran.spi.JoranException;
public class LoggingLoader {
private static final Logger logger = LoggerFactory.getLogger(LoggingLoader.class);
private static final String LOGBACK_XML = "logback.xml";
public static void reloadLogging(File configPathF) {
File logConfigFile = new File(configPathF, LOGBACK_XML);
if (!logConfigFile.exists()) {
logger.info("Not changing loback configuration as " + logConfigFile.getAbsolutePath() + " does not exist.");
} else {
if (!(LoggerFactory.getILoggerFactory() instanceof LoggerContext)) {
logger.error(logConfigFile.getAbsolutePath()
+ " exists, but LoggerFactory is not instance of ch.qos.logback.classic.LoggerContext. Ignoring.");
} else {
logger.info(logConfigFile.getAbsolutePath() + " file exists. Reloading logging configuration from "
+ logConfigFile);
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
try {
loggerContext.reset();
new ContextInitializer(loggerContext).configureByResource(logConfigFile.toURI().toURL());
logger.info("Reloaded logger configuration from " + logConfigFile.getAbsolutePath());
} catch (MalformedURLException | JoranException e) {
try {
new ContextInitializer(loggerContext).autoConfig();
} catch (JoranException e1) {
logger.error("Failed to reload original config after failure to load new config from "
+ logConfigFile.getAbsolutePath(), e);
}
logger.error("Failed to reload logback configuration from file " + logConfigFile, e);
}
}
}
}
}

View File

@ -34,6 +34,7 @@ import li.strolch.runtime.configuration.ConfigurationParser;
import li.strolch.runtime.configuration.RuntimeConfiguration;
import li.strolch.runtime.configuration.StrolchConfiguration;
import li.strolch.utils.helper.StringHelper;
import li.strolch.utils.helper.SystemHelper;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -41,6 +42,7 @@ import li.strolch.utils.helper.StringHelper;
public class StrolchAgent {
public static final String AGENT_VERSION_PROPERTIES = "/agentVersion.properties"; //$NON-NLS-1$
private static final Logger logger = LoggerFactory.getLogger(StrolchAgent.class);
private ComponentContainerImpl container;
@ -176,6 +178,7 @@ public class StrolchAgent {
logger.info(" - Config: " + configPathF.getAbsolutePath());
logger.info(" - Data: " + dataPathF.getAbsolutePath());
logger.info(" - Temp: " + tempPathF.getAbsolutePath());
logger.info(" - user.dir: " + SystemHelper.getUserDir());
this.strolchConfiguration = ConfigurationParser.parseConfiguration(environment, configPathF, dataPathF,
tempPathF);

View File

@ -310,6 +310,8 @@ public class StrolchBootstrapper extends DefaultHandler {
throw new StrolchConfigurationException(msg);
}
LoggingLoader.reloadLogging(this.configPathF);
String env;
if (StringHelper.isEmpty(this.environmentOverride)) {
env = this.environment;
@ -364,7 +366,8 @@ public class StrolchBootstrapper extends DefaultHandler {
}
String root = StringHelper.isEmpty(this.rootS)
? new File(System.getProperty(SYS_PROP_USER_DIR)).getAbsolutePath() : this.rootS;
? new File(System.getProperty(SYS_PROP_USER_DIR)).getAbsolutePath()
: this.rootS;
String config = StringHelper.isEmpty(this.configS) ? PATH_CONFIG : this.configS;
String data = StringHelper.isEmpty(this.dataS) ? PATH_DATA : this.dataS;
String temp = StringHelper.isEmpty(this.tempS) ? PATH_TEMP : this.tempS;