Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Issue using log4j to log the stack trace to MySQL DB

843810Jan 23 2009 — edited Jan 26 2009
Hello

I am trying to setup a logging appender for log4j that logs to a MySQL database. I have everything working except for one thing. I want to be able to log the stack trace but I can not seem to get that to work.

When I use this logger in my code in my local environment:
catch( Exception e ) {
	logger.error( "Exception trying to retrieve a value", e );
}
and I use a File appender, the exception message and the stack trace are both printed.

Here is the layout I was using
<param name="ConversionPattern" value="%m%n"/>
But when I try to do the same thing but instead of using the File Appender I use my DB appender, all that gets logged is the message (not the stack trace). Actually the stack trace is displayed as 2 boxes (that may be a null indicator for the DB utility I am using).

Is it possible to log the stack trace to a MySQL database? The field data type for the stack trace is LONGBLOG.

Here are my appenders just in case I am missing something:
<appender name="ROLLING_FILE" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="/logger.log"/>
	<param name="Append" value="true"/>
	<param name="MaxFileSize" value="10MB"/>
	<param name="MaxBackupIndex" value="4"/>
	<layout class="org.apache.log4j.PatternLayout">
		<param name="ConversionPattern" value="[%d{MM/dd/yyyy HH:mm:ss,SSS}] %-5p - (%F:%L) - %m%n"/>
	</layout>
</appender>

<appender name="MySqlDB" class="org.apache.log4j.jdbc.JDBCAppender">
     	<param name="URL" value="jdbc:mysql://localhost/test"/>
     	<param name="driver" value="com.mysql.jdbc.Driver"/>
     	<param name="user" value="USERNAME"/>
     	<param name="password" value="PASSWORD"/>
     	<layout class="org.apache.log4j.PatternLayout">
     		<param name="ConversionPattern" value="INSERT INTO tbllog (logDate, logLevel, location, stackTrace) VALUES ( '%d','%p','%C;%L', '%m%n' )"/>
     	</layout>
</appender>
Any feedback is appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 23 2009
Added on Jan 23 2009
6 comments
1,420 views