jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes Log 4j JDBC appender Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Log 4j JDBC appender" Watch "Log 4j JDBC appender" New topic

Log 4j JDBC appender

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 614

I am trying to use the log4j JDBC appender with mySQL. I am getting below error message:
log4j:ERROR Could not create an Appender. Reported error follows.
java.lang.ClassNotFoundException: org.apache.log4j.jdbcplus.JDBCAppender
at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClass
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at weblogic.utils.classloaders.GenericClassLoader.loadClass(GenericClass
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.Class.forName0(Native Method)

I have put the log4j jar file in the classpath. My log4j configuration is as below:
<appender name="JDBC" class="org.apache.log4j.jdbcplus.JDBCAppender">
<param name="url" value="jdbc:mysql://localhost:3306/mysql" />
<param name="username" value="root" />
<param name="password" value="root" />
<param name="sql" value="INSERT INTO LOGTEST (id, prio, cat, thread, msg) VALUES (@INC@, '@PRIO@', '@CAT@', '@THREAD@', '@MSG@')" />
<param name="buffer" value="1" />
<param name="commit" value="true" />
<param name="dbclass" value="com.mysql.jdbc.Driver" />
<param name="quoteReplace" value="true" />
<param name="throwableMaxChars" value="3000" />
<param name="layoutPartsDelimiter" value="#-#" />
<layout class="org.apache.log4j.PatternLayout">
<!-- conversion pattern with 4 parts separated by #-#, second part is empty -->
<param name="ConversionPattern" value="[%t] %m#-##-#%d{dd.MM.yyyy}#-#%d{HH:mm:ss}" />

<!--<appender-ref ref="def"/>-->
<!--<appender-ref ref="RollingFile" />-->
<appender-ref ref="JDBC" />

Can someone please help me out what am I doing wrong?

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 614
now I am getting below exception:
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:532)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at org.apache.log4j.jdbc.JDBCAppender.getConnection(JDBCAppender.java:19

There was some mistake in the earlier configuration. I corrected that so am getting above exception.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Is the MySQL JDBC driver in your classpath? That's the error you would get if it wasn't.
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 614
The driver is in the classpath, but the configuration had lower case driver name. I changed so do not get that error any more but now it seems I have problem with my SQL statement.

The statement runs with no problem from mySQL console but when I run it through the config file I get below error:

log4j:ERROR Failed to excute sql
java.sql.SQLException: You have an error in your SQL syntax; check the manual th
at corresponds to your MySQL server version for the right syntax to use near '[E
xecuteThread: '1' for queue: 'weblogic.kernel.System'] Entering net.mycompany.'
at line 1

The SQLstatement is given below:
<param name="sql" value="INSERT INTO test (message) VALUES ('%d - %c - %p - %m'); "/>

Can someone please let me know what is wrong in above SQL?

[ April 25, 2006: Message edited by: trupti nigam ]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Moved to JDBC forum.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

What's wrong with that SQL? Well, the error message says that it includes this:
[ExecuteThread: '1' for queue: 'weblogic.kernel.System'] Entering net.mycompany.
I think that when the appender replaces %d and so on by the replacement values, if the replacement values contain quotes then the SQL gets fouled up.
I agree. Here's the link: http://aspose.com/file-tools
subject: Log 4j JDBC appender
It's not a secret anymore!