OSGi and class loaders make for complications and tricky debugging sessions :-(
Gary On Tue, Nov 29, 2016 at 2:08 PM, Michael Carman <[email protected]> wrote: > To try to tease out whether my problem is at the Eclipse level or the Log4j > level I experimented with loading my class via reflection. > > static { > try { > Class<?> clazz = Class.forName("x.y.z.LogDB"); > Method method = clazz.getMethod("getConnection"); > Connection connection = (Connection) method.invoke(clazz); > connection.close(); > } catch (Exception e) { > e.printStackTrace(); > } > logger = LogManager.getLogger(); > } > > My attempt at reflection is successful. The call to LogManager.getLogger() > results in a ClassNotFoundException. This implies that my problem is that > Log4j isn't picking up on the class path used within my plugin. Is there a > way to configure that? Searching for "log4j classpath" turns up lots of > things about where to put the log4j.properties file. > > I can't be the first person to try to use a JDBC appender with Eclipse RCP. > > -Mike > > > On Tue, Nov 29, 2016 at 3:27 PM Michael Carman <[email protected] > > > wrote: > > > Yes, getConnection() is static. Log4j isn't getting far enough to attempt > > calling it, though. An explicit call to LogDB.getConnection() is > > successful but Log4j's attempts to invoke it via reflection fails trying > to > > load the class. That holds true even if I move my LogDB into the plug-in > > for my application and pre-load it! e.g., in the Activator for my main > > plug-in: > > > > static { > > try { > > LogDB.getConnection(); // succeeds > > } catch (SQLException e) { > > e.printStackTrace(); > > } > > LogManager.getLogger(); // throws ClassNotFoundException > > } > > > > It seems like some sort of class path configuration problem, but I can't > > figure out what to set. > > > > -Mike > > > > On Tue, Nov 29, 2016 at 2:43 PM Gary Gregory <[email protected]> > > wrote: > > > > Is your getConnection() method static? > > > > Gary > > > > On Tue, Nov 29, 2016 at 9:04 AM, Michael Carman < > > [email protected]> > > wrote: > > > > > I'm using Log4j2 in an Eclipse RCP application. I've been using a > > > RollingFileAppender and am trying to switch to a JDBC appender backed > by > > a > > > ConnectionFactory. I've implemented this using the examples at > > > https://logging.apache.org/log4j/2.x/manual/appenders. > html#JDBCAppender > > as > > > a guide but get a ClassNotFoundException at each call to > > > LogManager.getLogger(). e.g. > > > > > > 2016-11-29 10:23:06,233 main ERROR java.lang.ClassNotFoundException: > > > x.y.z.LogDB java.lang.ClassNotFoundException: x.y.z.LogDB > > > at > > > org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal( > > > BundleLoader.java:506) > > > at > > > org.eclipse.osgi.internal.loader.BundleLoader.findClass( > > > BundleLoader.java:422) > > > at > > > org.eclipse.osgi.internal.loader.BundleLoader.findClass( > > > BundleLoader.java:410) > > > at > > > org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass( > > > DefaultClassLoader.java:107) > > > at java.lang.ClassLoader.loadClass(ClassLoader.java:357) > > > at java.lang.Class.forName0(Native Method) > > > at java.lang.Class.forName(Class.java:264) > > > at > > > org.apache.logging.log4j.util.LoaderUtil.loadClass( > LoaderUtil.java:141) > > > at > > > org.apache.logging.log4j.core.appender.db.jdbc. > > > FactoryMethodConnectionSource.createConnectionSource( > > > FactoryMethodConnectionSource.java:82) > > > ... > > > > > > My log4j2.xml defines the appender as: > > > > > > <JDBC name="DB" tableName="APP_LOG"> > > > <ConnectionFactory class="x.y.z.LogDB" method="getConnection" /> > > > <Column name="TIMESTAMP" isEventTimestamp="true" /> > > > <Column name="PID" pattern="%X{pid}" /> > > > <Column name="THREAD" pattern="%thread" /> > > > <Column name="USERNAME" pattern="${env:USERNAME}" /> > > > <Column name="SEVERITY" pattern="%level" /> > > > <Column name="LOGGER" pattern="%logger{1.1.1.*}" /> > > > <Column name="MESSAGE" pattern="%message" /> > > > <Column name="EXCEPTION" pattern="%exception" isClob="true" /> > > > </JDBC> > > > > > > I've looked at the obvious things: the name of my factory class matches > > > between my code and config file, it's declared public, and its package > is > > > exported by the parent plug-in. (The parent plug-in is also required by > > > other plugins via their manifests.) I can't see why the class loader > > > shouldn't be able to find it. I assume there's something simple and > > stupid > > > that I'm missing. > > > > > > I'm using Eclipse 3.6.2 (Helios) and Log4j 2.7. > > > > > > -Mike > > > > > > > > > > > -- > > E-Mail: [email protected] | [email protected] > > Java Persistence with Hibernate, Second Edition > > < > > https://www.amazon.com/gp/product/1617290459/ref=as_li_ > tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1617290459& > linkCode=as2&tag=garygregory-20&linkId=cadb800f39946ec62ea2b1af9fe6a2b8 > > > > > > > <http://// > > ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1617290459> > > JUnit in Action, Second Edition > > < > > https://www.amazon.com/gp/product/1935182021/ref=as_li_ > tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182021& > linkCode=as2&tag=garygregory-20&linkId=31ecd1f6b6d1eaf8886ac902a24de418%22 > > > > > > > <http://// > > ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182021> > > Spring Batch in Action > > < > > https://www.amazon.com/gp/product/1935182951/ref=as_li_ > tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182951& > linkCode=%7B%7BlinkCode%7D%7D&tag=garygregory-20&linkId=%7B% > 7Blink_id%7D%7D%22%3ESpring+Batch+in+Action > > > > > <http://// > > ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182951> > > Blog: http://garygregory.wordpress.com > > Home: http://garygregory.com/ > > Tweet! http://twitter.com/GaryGregory > > > > > -- E-Mail: [email protected] | [email protected] Java Persistence with Hibernate, Second Edition <https://www.amazon.com/gp/product/1617290459/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1617290459&linkCode=as2&tag=garygregory-20&linkId=cadb800f39946ec62ea2b1af9fe6a2b8> <http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1617290459> JUnit in Action, Second Edition <https://www.amazon.com/gp/product/1935182021/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182021&linkCode=as2&tag=garygregory-20&linkId=31ecd1f6b6d1eaf8886ac902a24de418%22> <http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182021> Spring Batch in Action <https://www.amazon.com/gp/product/1935182951/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182951&linkCode=%7B%7BlinkCode%7D%7D&tag=garygregory-20&linkId=%7B%7Blink_id%7D%7D%22%3ESpring+Batch+in+Action> <http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182951> Blog: http://garygregory.wordpress.com Home: http://garygregory.com/ Tweet! http://twitter.com/GaryGregory
