Skip to content

Commit

Permalink
[PAXLOGGING-194] Support use of static logback configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
mcculls committed Apr 28, 2015
1 parent 5c0bb26 commit 00adb8c
Showing 1 changed file with 35 additions and 13 deletions.
Expand Up @@ -47,7 +47,6 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Dictionary;
Expand Down Expand Up @@ -92,6 +91,7 @@ public class PaxLoggingServiceImpl implements PaxLoggingService, org.knopflerfis
private final BundleContext m_bundleContext;
private final PaxContext m_paxContext;
private final boolean m_useStaticContext;
private final String m_staticConfigFile;
private final LoggerContext m_logbackContext;
private final String m_fqcn;

Expand Down Expand Up @@ -129,6 +129,8 @@ public PaxLoggingServiceImpl(@Nonnull BundleContext bundleContext, @Nonnull LogR
m_logbackContext.start();
}

m_staticConfigFile = bundleContext.getProperty("org.ops4j.pax.logging.StaticLogbackFile");

// not strictly necessary because org.apache.felix.cm.impl.ConfigurationManager will configure us, but this
// is a safe precaution. In a typical run, we will reset the logback configuration four times:
// 1) here, in the constructor
Expand Down Expand Up @@ -230,22 +232,32 @@ public void updated( Dictionary configuration )

if( configuration == null )
{
configureDefaults();
if (m_staticConfigFile != null) {
// maintain the existing configuration
} else {
configureDefaults();
}
return;
}

Object configfile = configuration.get(LOGBACK_CONFIG_FILE_KEY);
if (configfile != null) {
File f = new File(configfile.toString());
if (f.exists()) {
try {
configureLogback(f);
} catch (RuntimeException e) {
m_logbackContext.getStatusManager().add(new WarnStatus("Error loading Logback configuration from '" + f + "'", m_logbackContext, e));
}
if (configfile.equals(m_staticConfigFile)) {
// maintain the existing configuration
} else {
m_logbackContext.getStatusManager().add(new WarnStatus("Configuration said to load '" + f + "' but that file does not exist", m_logbackContext));
configureLogback(null);
File f = new File(configfile.toString());
if (f.exists()) {
try {
configureLogback(f);
} catch (RuntimeException e) {
m_logbackContext.getStatusManager().add(new WarnStatus(
"Error loading Logback configuration from '" + f + "'", m_logbackContext, e));
}
} else {
m_logbackContext.getStatusManager().add(new WarnStatus(
"Configuration said to load '" + f + "' but that file does not exist", m_logbackContext));
configureLogback(null);
}
}
} else {
configureLogback(null);
Expand All @@ -257,8 +269,18 @@ public void updated( Dictionary configuration )

private void configureDefaults()
{
ConsoleAppender<ILoggingEvent> consoleAppender = configureLogbackDefaults();
consoleAppender.addInfo("default: setting up console logging at WARN level");
if (m_staticConfigFile != null) {
File f = new File(m_staticConfigFile);
try {
configureLogback(f);
} catch (RuntimeException e) {
m_logbackContext.getStatusManager().add(new WarnStatus(
"Error loading Logback configuration from '" + f + "'", m_logbackContext, e));
}
} else {
ConsoleAppender<ILoggingEvent> consoleAppender = configureLogbackDefaults();
consoleAppender.addInfo("default: setting up console logging at WARN level");
}

String levelName;
levelName = m_bundleContext.getProperty( DEFAULT_SERVICE_LOG_LEVEL );
Expand Down

0 comments on commit 00adb8c

Please sign in to comment.