Skip to content

Commit

Permalink
Enhance log configurator to avoid ClassNotFoundException
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed May 22, 2013
1 parent e9299eb commit 25e2012
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
6 changes: 0 additions & 6 deletions webcam-capture-drivers/webcam-capture-driver-ipcam/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@
<artifactId>httpmime</artifactId>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.9</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.ds.ipcam.IpCamDriver;
import com.github.sarxos.webcam.ds.ipcam.IpCamStorage;
import com.github.sarxos.webcam.log.WebcamLogConfigurator;


/**
Expand Down Expand Up @@ -40,6 +41,8 @@ public class JpegDasdingStudioExample {

public static void main(String[] args) throws MalformedURLException {

WebcamLogConfigurator.configure("src/examples/resources/cameras.xml");

JFrame f = new JFrame("Dasding Studio Live IP Cameras Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(0, 3, 1, 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;


/**
* Configure loggers.
Expand All @@ -33,23 +30,38 @@ public class WebcamLogConfigurator {
*/
public static void configure(InputStream is) {

ClassLoader cl = Thread.currentThread().getContextClassLoader();

try {
Class.forName("ch.qos.logback.classic.LoggerContext");
} catch (ClassNotFoundException e1) {
LOG.error("Cannot configure logger because logback LoggerContext is not available in classpath");
return;
}

LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
context.reset();
String[] names = {
"ch.qos.logback.classic.LoggerContext",
"ch.qos.logback.classic.joran.JoranConfigurator",
"ch.qos.logback.core.Context"
};

try {
configurator.doConfigure(is);
} catch (JoranException e) {
for (String name : names) {
Class.forName(name, false, cl);
}

Object context = LoggerFactory.getILoggerFactory();

Class<?> cfgc = Class.forName("ch.qos.logback.classic.joran.JoranConfigurator");
Object configurator = cfgc.newInstance();

Method setContext = cfgc.getMethod("setContext");
setContext.invoke(configurator, context);

Method reset = context.getClass().getMethod("reset");
reset.invoke(context, new Object[0]);

Method doConfigure = cfgc.getMethod("doConfigure");
doConfigure.invoke(configurator, is);

} catch (ClassNotFoundException e) {
System.err.println("WLogC: Logback JAR is missing inc lasspath");
} catch (Exception e) {
LOG.error("Joran configuration exception", e);
e.printStackTrace();
}
}

Expand Down

0 comments on commit 25e2012

Please sign in to comment.