Rats, the attachment got stripped. Here's the code: /* vim: ts=4 sw=4 cindent */ import java.io.IOException; import java.io.PrintWriter;
import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingEnumeration; import javax.naming.NameClassPair; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/DataSourceExample") public class DataSourceExample extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet( HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException { Context ctx = null; try { PrintWriter out = response.getWriter(); response.setContentType("text/plain"); ctx = new InitialContext(); try { printCtx(ctx, out); } catch (NamingException e) { out.println(e); } } catch (NamingException e) { e.printStackTrace(); } finally { try { ctx.close(); } catch (NamingException e) { System.out.println("Exception in closing context."); } } } private void printCtx(Context ctx, PrintWriter out) throws NamingException { NamingEnumeration<NameClassPair> list = ctx.list(""); int rows = 0; for (;list.hasMore(); rows++) { NameClassPair pair = list.next(); out.print(pair.getName() + " : "); try { out.print(ctx.lookup(pair.getName())); } catch (Exception e) {} out.println(""); } out.println("Context contains " + rows + " items."); } } "Alban Hertroys" <alban.hertr...@apollovredestein.com> wrote on 2019-12-12 17:20:26: > Van: "Alban Hertroys" <alban.hertr...@apollovredestein.com> > Aan: "Log4J Users List" <log4j-user@logging.apache.org> > Datum: 2019-12-12 17:20 > Onderwerp: Betr: Re: JDBCAppender fails to pass connection URL string > > CAUTION : External email. Do not click links or open attachments > unless you recognize the sender and know the content is safe. > > "Ralph Goers" <ralph.go...@dslextreme.com> wrote on 2019-12-12 16:00:02: > > > http://logging.apache.org/log4j/2.x/manual/appenders.html#JDBCAppender < > > http://logging.apache.org/log4j/2.x/manual/appenders.html#JDBCAppender > > > does have sample configurations but there is not one with the > > configuration I would expect to use where you can specify the driver > > class, user name and password. > > > ...Hang on... Does that mean I'm using a feature from an untested code > path? > > I'm created a dumbed down servlet with the same configuration files and it > looks like the context from InitialContext() is empty - that certainly > would explain the null database driver and the empty connection string. > > > > I put the above under Tomcat with a small context-file in > ${CATALINA_HOME}/conf/Catalina/localhost and got the output: > > Context contains 0 items. > > So it would seem that either my context initialization in de attached Java > class is incorrect, or my context is set up incorrectly somehow. > > When adding code to get the DB connection from a DataSource from that > context to that class, I get the same exception as the one I get in my > attempts to set up log4j2. I suspect that the problem is there, but have > no clue where to look. > > Is it required to also set up the same JNDI resource in tomcat's > server.xml? I think I tried that, but I removed it as it didn't seem to > help and I don't like having definitions duplicated all over the place. > > Regards, > Alban. > > > > On Dec 12, 2019, at 7:55 AM, Ralph Goers <ralph.go...@dslextreme.com> > wrote: > > > > > > I just looked at the JDBC unit tests and surprisingly didn’t see > > any that use a configuration file. > > > > > > Ralph > > > > > >> On Dec 12, 2019, at 7:54 AM, Matt Sicker <boa...@gmail.com> wrote: > > >> > > >> Unit tests in the log4j project. All our components have examples in > unit > > >> tests. > > >> > > >> On Thu, Dec 12, 2019 at 04:26 Alban Hertroys < > > >> alban.hertr...@apollovredestein.com> wrote: > > >> > > >>> "Gary Gregory" <garydgreg...@gmail.com> wrote on 2019-12-11 > 22:01:54: > > >>> > > >>>> CAUTION : External email. Do not click links or open attachments > > >>>> unless you recognize the sender and know the content is safe. > > >>>> > > >>>> AFK, sorry for the top-post. Have you looked at our unit tests? > There > > >>> might > > >>>> be something there to help see what the usage pattern is for JNDI. > > >>> > > >>> You mean to say I should write some Java code to test the JNDI > connection > > >>> as defined in the Tomcat configuration? Or are the unit tests you > refer to > > >>> existing code that is available somewhere? > > >>> > > >>> Either way, I have no idea which unit tests you're referring to? I'm > also > > >>> not quite sure what I'm looking for here, I haven't done this before > and I > > >>> don't write Java code on a regular basis. Just created a few > snippets to > > >>> do small stuff here and there, such as this case where we're > wrapping a > > >>> servlet to have some logging of HTTP context information to a > database > > >>> table. > > >>> > > >>> There's no requirement here to do the logging through JNDI either; > we used > > >>> a direct connection through log4j.xml previously and that had some > issues > > >>> (mostly losing connection when the DB server got kicked). JNDI just > seemed > > >>> to be the easiest way to set up a connection pool for the > JDBCAppender, > > >>> but now it's starting to look like JNDI is anything but. > > >>> > > >>> Regards, > > >>> Alban. > > >>> > > >>> > > >>>> On Tue, Dec 10, 2019, 06:02 Alban Hertroys < > > >>>> alban.hertr...@apollovredestein.com> wrote: > > >>>> > > >>>>> Hi, > > >>>>> > > >>>>> We're having a lot of trouble getting the JDBCAppender to write to > our > > >>>>> PostgreSQL database. There's probably a configuration error > somewhere, > > >>> but > > >>>>> we haven't been able to find it. And I have been searching and > trying > > >>> for > > >>>>> several days now, with no progress at all. > > >>>>> Even though we specify a connection URL in the JDNI source (see > > >>> below), it > > >>>>> always ends with the JDBCAppender complaining that: > > >>>>> java.sql.SQLException: Cannot create JDBC driver of class '' > > >>> for > > >>>>> connect URL 'null' > > >>>>> > > >>>>> The Appender should write the data to our database at server > "foobar", > > >>>>> database "logging-tst". Using the same credentials in SquirrelSQL, > > >>> with > > >>>>> the same JDBC driver, I can connect to that database and query the > > >>> table > > >>>>> specified in the log4j.xml (schema: "logging", table: "usage"; > names > > >>> being > > >>>>> case-insensitive). > > >>>>> > > >>>>> Any pointers would be appreciated. > > >>>>> > > >>>>> Environment: > > >>>>> - Centos 7 > > >>>>> - Apache Tomcat 8.5.32 > > >>>>> - log4j-2.11 > > >>>>> > > >>>>> We created a servlet HTTP filter into a JAR, compiled against: > > >>>>> WEB-INF/lib: > > >>>>> log4j-api-2.11.0.jar log4j-core-2.11.0.jar > > >>>>> servlet-api.jar > > >>>>> > > >>>>> This servlet put()'s certain fields into the ThreadContext, which > we > > >>> then > > >>>>> attempt to access through %X{...} in the log4j2.xml, appending > those > > >>>>> fields as a > > >>>>> row to an existing table in our database. > > >>>>> We had this set-up working previously with log4j-1.something in an > MS > > >>> SQL > > >>>>> '05 > > >>>>> DB, all on Windows 2003 server (which we are migrating away from > due > > >>> to > > >>>>> performance issues). > > >>>>> > > >>>>> Files within the web application: > > >>>>> WEB-INF/lib/ > > >>>>> log4j-1.2-api-2.11.0.jar log4j-api-2.11.0.jar > > >>>>> log4j-core-2.11.0.jar log4j-jcl-2.11.0.jar log4j-jul-2.11.0.jar > > >>>>> log4j-slf4j-impl-2.11.0.jar log4j-web-2.11.0.jar > > >>>>> postgresql-jdbc.jar > > >>>>> > > >>>>> > > >>>>> META-INF/context.xml: > > >>>>> --------------------- > > >>>>> <?xml version="1.0" encoding="utf-8"?> > > >>>>> <context> > > >>>>> <resource > > >>>>> name="jdbc/loggingtst" > > >>>>> auth="Container" > > >>>>> type="javax.sql.DataSource" > > >>>>> username="*****" > > >>>>> password="*****" > > >>>>> driverClassName="org.postgresql.Driver" > > >>>>> url="jdbc:postgresql://foobar:5432/logging-tst" > > >>>>> maxTotal="20" > > >>>>> maxIdle="10" > > >>>>> /> > > >>>>> </context> > > >>>>> > > >>>>> > > >>>>> WEB-INF/classes/log4j2.xml: > > >>>>> --------------------------- > > >>>>> <?xml version="1.0" encoding="UTF-8"?> > > >>>>> <Configuration status="WARN"> > > >>>>> ... > > >>>>> <Appenders> > > >>>>> ... > > >>>>> <JDBC name="DBAppender" tableName="logging.usage"> > > >>>>> <DataSource jndiName="java:/comp/env/jdbc/loggingtst"/> > > >>>>> <Column name="servername" pattern="%X{servername}"/> > > >>>>> <Column name="fex" pattern="%X{focexec}"/> > > >>>>> <Column name="parameters" pattern="%X{parameters}"/> > > >>>>> <Column name="username" pattern="%X{username}"/> > > >>>>> <Column name="duration" pattern="%X{duration}"/> > > >>>>> <Column name="user_agent" pattern="%X{user-agent}"/> > > >>>>> </JDBC> > > >>>>> </Appenders> > > >>>>> <Loggers> > > >>>>> ... > > >>>>> <Logger name="LogFilter" level="info" additivity="false"> > > >>>>> <AppenderRef ref="DBAppender"/> > > >>>>> </Logger> > > >>>>> > > >>>>> <Root level="error"> > > >>>>> <AppenderRef ref="sysout" /> > > >>>>> </Root> > > >>>>> </Loggers> > > >>>>> > > >>>>> </Configuration> > > >>>>> > > >>>>> > > >>>>> WEB-INF/web.xml: > > >>>>> ---------------- > > >>>>> ... > > >>>>> <resource-ref> > > >>>>> <description>PostgreSQL logging table</description> > > >>>>> <res-ref-name>jdbc/loggingtst</res-ref-name> > > >>>>> <res-type>javax.sql.DataSource</res-type> > > >>>>> <res-auth>Container</res-auth> > > >>>>> </resource-ref> > > >>>>> ... > > >>>>> > > >>>>> With this, catalina.out contains: > > >>>>> > > >>>>> 2019-12-09 17:38:16,460 localhost-startStop-1 ERROR > > >>> JdbcDatabaseManager > > >>>>> JdbcManager{name=FexAppender, bufferSize=0, > tableName=logging.usage, > > >>>>> columnConfigs=[{ name=servername, layout=%X{servername}, > literal=null, > > >>>>> timestamp=false }, { name=fex, layout=%X{focexec}, literal=null, > > >>>>> timestamp=false }, { name=parameters, layout=%X{parameters}, > > >>> literal=null, > > >>>>> timestamp=false }, { name=username, layout=%X{username}, > literal=null, > > >>>>> timestamp=false }, { name=duration, layout=%X{duration}, > literal=null, > > >>>>> timestamp=false }, { name=user_agent, layout=%X{user-agent}, > > >>> literal=null, > > >>>>> timestamp=false }], columnMappings=[]} Could not perform database > > >>> startup > > >>>>> operations: java.sql.SQLException: Cannot create JDBC driver of > class > > >>> '' > > >>>>> for connect URL 'null' java.sql.SQLException: Cannot create JDBC > > >>> driver of > > >>>>> class '' for connect URL 'null' > > >>>>> at > > >>>>> > > >>>>> > > >>>> > org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory > > >>>> (BasicDataSource.java:2186) > > >>>>> at > > >>>>> > > >>>>> org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource > > >>>> (BasicDataSource.java:2066) > > >>>>> at > > >>>>> > > >>>>> org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection > > >>>> (BasicDataSource.java:1525) > > >>>>> at > > >>>>> > > >>>>> > > >>>> > > >>> > > >>> > > > org.apache.logging.log4j.core.appender.db.jdbc.DataSourceConnectionSource.getConnection > > >>>> (DataSourceConnectionSource.java:51) > > >>>>> at > > >>>>> > > >>>>> > > >>>> > > >>> > > >>> > > > org.apache.logging.log4j.core.appender.db.jdbc.JdbcDatabaseManager.startupInternal > > >>>> (JdbcDatabaseManager.java:85) > > >>>>> at > > >>>>> > > >>>>> > > >>>> > > >>> > org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager.startup > > >>>> (AbstractDatabaseManager.java:81) > > >>>>> at > > >>>>> > > >>>>> > > >>>> > org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender.start > > >>>> (AbstractDatabaseAppender.java:106) > > >>>>> at > > >>>>> > > >>>>> org.apache.logging.log4j.core.config.AbstractConfiguration.start > > >>>> (AbstractConfiguration.java:265) > > >>>>> at > > >>>>> > > >>>>> org.apache.logging.log4j.core.LoggerContext.setConfiguration > > >>>> (LoggerContext.java:547) > > >>>>> at > > >>>>> > > >>> > org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:263) > > >>>>> at > > >>>>> > > >>>>> org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext > > >>>> (Log4jContextFactory.java:240) > > >>>>> at > > >>>>> > > >>>>> org.apache.logging.log4j.core.config.Configurator.initialize > > >>>> (Configurator.java:158) > > >>>>> at > > >>>>> > > >>>>> > > >>>> > org.apache.logging.log4j.web.Log4jWebInitializerImpl.initializeNonJndi > > >>>> (Log4jWebInitializerImpl.java:168) > > >>>>> at > > >>>>> > > >>>>> org.apache.logging.log4j.web.Log4jWebInitializerImpl.start > > >>>> (Log4jWebInitializerImpl.java:110) > > >>>>> at > > >>>>> > > >>>>> > > >>>> > org.apache.logging.log4j.web.Log4jServletContainerInitializer.onStartup > > >>>> (Log4jServletContainerInitializer.java:57) > > >>>>> at > > >>>>> > > >>>>> org.apache.catalina.core.StandardContext.startInternal > > >>>> (StandardContext.java:5245) > > >>>>> at > > >>>>> > org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) > > >>>>> at > > >>>>> > > >>>>> org.apache.catalina.core.ContainerBase.addChildInternal > > >>>> (ContainerBase.java:754) > > >>>>> at > > >>>>> > > >>> > org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730) > > >>>>> at > > >>>>> > org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) > > >>>>> at > > >>>>> > > >>>>> > > >>> > > >>> org.apache.catalina.startup.HostConfig.deployDescriptor > > (HostConfig.java:629) > > >>>>> at > > >>>>> > > >>>>> org.apache.catalina.startup.HostConfig$DeployDescriptor.run > > >>>> (HostConfig.java:1839) > > >>>>> at > > >>>>> > > >>> > java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) > > >>>>> at java.util.concurrent.FutureTask.run(FutureTask.java:266) > > >>>>> at > > >>>>> > > >>>>> java.util.concurrent.ThreadPoolExecutor.runWorker > > >>>> (ThreadPoolExecutor.java:1149) > > >>>>> at > > >>>>> > > >>>>> java.util.concurrent.ThreadPoolExecutor$Worker.run > > >>>> (ThreadPoolExecutor.java:624) > > >>>>> at java.lang.Thread.run(Thread.java:748) > > >>>>> Caused by: java.lang.NullPointerException > > >>>>> at org.postgresql.Driver.parseURL(Driver.java:551) > > >>>>> at org.postgresql.Driver.acceptsURL(Driver.java:472) > > >>>>> at java.sql.DriverManager.getDriver(DriverManager.java:299) > > >>>>> at > > >>>>> > > >>>>> > > >>>> > org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory > > >>>> (BasicDataSource.java:2171) > > >>>>> ... 26 more > > >>>>> > > >>>>> (Ignore below company statement) > > >>>>> > > >>>>> > > >>>>> > > >>>>> Alban Hertroys > > >>>>> D: +31 (0)53 4 888 888 | T: +31 (0)53 4888 888 | E: > > >>>>> alban.hertr...@apollovredestein.com > > >>>>> Apollo Vredestein B.V.| Ir. E.L.C. Schiffstraat 370, 7547 RD > Enschede, > > >>> The > > >>>>> Netherlands > > >>>>> Chamber of Commerce number: 34223268 > > >>>>> > > >>>>> > > >>>>> > > >>>>> > > >>>>> The information contained in this e-mail is intended solely for > the > > >>> use of > > >>>>> the > > >>>>> individual or entity to whom it is addressed. If you are not the > > >>> intended > > >>>>> recipient, you are hereby notified that any disclosure, copying, > > >>>>> distribution > > >>>>> or action in relation to the contents of this information is > strictly > > >>>>> prohibited and may be unlawful and request you to delete this > message > > >>> and > > >>>>> any > > >>>>> attachments and advise the sender by return e-mail. The > > >>> confidentiality of > > >>>>> this > > >>>>> message is not warranted. Apollo Vredestein and its subsidiaries > rule > > >>> out > > >>>>> any > > >>>>> and every liability resulting from this or any other electronic > > >>>>> transmission > > >>>>> > > >>>>> > > >>>>> > > >>>>> > > >>>>> > > >>>>> Please consider the environment before printing this > e-mail > > >>>>> > > >>>>> > > >>>>> > --------------------------------------------------------------------- > > >>>>> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org > > >>>>> For additional commands, e-mail: > log4j-user-h...@logging.apache.org > > >>>>> > > >>>>> > > >>> > > >>> > > >>> Alban Hertroys > > >>> D: +31 (0)53 4 888 888 | T: +31 (0)53 4888 888 | E: > > >>> alban.hertr...@apollovredestein.com > > >>> Apollo Vredestein B.V.| Ir. E.L.C. Schiffstraat 370, 7547 RD > Enschede, The > > >>> Netherlands > > >>> Chamber of Commerce number: 34223268 > > >>> > > >>> > > >>> > > >>> > > >>> The information contained in this e-mail is intended solely for the > use of > > >>> the > > >>> individual or entity to whom it is addressed. If you are not the > intended > > >>> recipient, you are hereby notified that any disclosure, copying, > > >>> distribution > > >>> or action in relation to the contents of this information is > strictly > > >>> prohibited and may be unlawful and request you to delete this > message and > > >>> any > > >>> attachments and advise the sender by return e-mail. The > confidentiality of > > >>> this > > >>> message is not warranted. Apollo Vredestein and its subsidiaries > rule out > > >>> any > > >>> and every liability resulting from this or any other electronic > > >>> transmission > > >>> > > >>> > > >>> > > >>> > > >>> > > >>> Please consider the environment before printing this e-mail > > >>> > > >>> > > >>> > --------------------------------------------------------------------- > > >>> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org > > >>> For additional commands, e-mail: log4j-user-h...@logging.apache.org > > >>> > > >>> -- > > >> Matt Sicker <boa...@gmail.com> > > > > > > > > > > > > --------------------------------------------------------------------- > > > To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org > > > For additional commands, e-mail: log4j-user-h...@logging.apache.org > > > > > > > > > > > Alban Hertroys > D: +31 (0)53 4 888 888 | T: +31 (0)53 4888 888 | E: > alban.hertr...@apollovredestein.com > Apollo Vredestein B.V.| Ir. E.L.C. Schiffstraat 370, 7547 RD Enschede, The > Netherlands > Chamber of Commerce number: 34223268 > > > > > The information contained in this e-mail is intended solely for the > use of the > individual or entity to whom it is addressed. If you are not the intended > recipient, you are hereby notified that any disclosure, copying, distribution > or action in relation to the contents of this information is strictly > prohibited and may be unlawful and request you to delete this message and any > attachments and advise the sender by return e-mail. The > confidentiality of this > message is not warranted. Apollo Vredestein and its subsidiaries rule out any > and every liability resulting from this or any other electronic transmission > > > > > > Please consider the environment before printing this e-mail > > --------------------------------------------------------------------- > To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org > For additional commands, e-mail: log4j-user-h...@logging.apache.org Alban Hertroys D: +31 (0)53 4 888 888 | T: +31 (0)53 4888 888 | E: alban.hertr...@apollovredestein.com Apollo Vredestein B.V.| Ir. E.L.C. Schiffstraat 370, 7547 RD Enschede, The Netherlands Chamber of Commerce number: 34223268 The information contained in this e-mail is intended solely for the use of the individual or entity to whom it is addressed. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or action in relation to the contents of this information is strictly prohibited and may be unlawful and request you to delete this message and any attachments and advise the sender by return e-mail. The confidentiality of this message is not warranted. Apollo Vredestein and its subsidiaries rule out any and every liability resulting from this or any other electronic transmission Please consider the environment before printing this e-mail