aspose file tools*
The moose likes Java in General and the fly likes Logging into database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Logging into database" Watch "Logging into database" New topic
Author

Logging into database

Ayyappan Selvaraj
Ranch Hand

Joined: Sep 20, 2004
Posts: 63
I dont find logging into database takes place. I have included this in the log4j.properties

log4j.appender.R=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.R.URL=myurl
log4j.appender.R.user=myuserid
log4j.appender.R.password=mypassword
log4j.appender.R.sql=INSERT INTO LOGTEST (Date, Logger, Priority, Message) VALUES ('%d - %c - %p - %m')

And I have a table LOGTEST in my database. And in my classes I have the code sinppet

protected final Log log = LogFactory.getLog(getClass());

I find that the logging takes place to a file but not to the database...
Can anyone help me to find where the problem could be pls....
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

What does the rest of your log4j.properties file look like? If it's logging to a file instead of to the database, then there's probably a mistake in the log4j.properties file so that it uses the wrong appender (a file appender instead of the database appender).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ayyappan Selvaraj
Ranch Hand

Joined: Sep 20, 2004
Posts: 63
This is what I have in rest of the log4j..

log4j.rootCategory=INFO,stdout,mail
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[@APPNAME@] %p [%t] %c{1}.%M(%L) | %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=@APPNAME@.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=0
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

I have the same for mail appender...
Ayyappan Selvaraj
Ranch Hand

Joined: Sep 20, 2004
Posts: 63
I get this error now...I guess the driver "org.apache.log4j.jdbc.JDBCAppender" could not be registered

[lis] DEBUG [main] StartupListener.contextInitialized(31) | initializing context...
log4j:ERROR Failed to excute sql
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:197)
at org.apache.log4j.jdbc.JDBCAppender.execute(JDBCAppender.java:160)
at org.apache.log4j.jdbc.JDBCAppender.flushBuffer(JDBCAppender.java:235)
at org.apache.log4j.jdbc.JDBCAppender.append(JDBCAppender.java:131)
at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:221)
at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:57)
at org.apache.log4j.Category.callAppenders(Category.java:194)
at org.apache.log4j.Category.forcedLog(Category.java:379)
at org.apache.log4j.Category.log(Category.java:844)
at org.apache.commons.logging.impl.Log4JLogger.debug(Log4JLogger.java:110)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Ok, that's one thing, but there's more. Like I already suspected, you are not using the JDBC appender at all. Look at the first line in your log4j.properties:

log4j.rootCategory=INFO,stdout,mail

You are using the "stdout" (which prints to the console) and the "mail" (which is not defined in your log4j.properties file?!) appenders. You are not using the appender named "R" (which is what you called the JDBC appender in your properties file). Change the first line to something like this:

log4j.rootCategory=INFO,stdout,R

*edit*: Wait a minute. You are not really posting the contents of your log4j.properties file. In your first post, it was the JDBC appender which was called "R", but in the other post this is a RollingFileAppender.

Please be more accurate, otherwise it will be difficult to help you.

You have to make sure that you specify a correct JDBC connection URL and that the JDBC driver is in the classpath.
[ August 03, 2006: Message edited by: Jesper Young ]
Ayyappan Selvaraj
Ranch Hand

Joined: Sep 20, 2004
Posts: 63
yes sorry for that.. I have fixed all that and now I find that Im not able to insert details into the table properly.

log4j.appender.db.sql=INSERT INTO LOGTEST (Date, Logger, Priority, Message) VALUES (%d,%c,%p,%m)

But I get SQL syntax error


this is my log4.props
# This is the configuring for logging to various Appenders in the Application Server
log4j.rootCategory=INFO,stdout,R,mail,db


###Logging to console(transient logging)

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=[@APPNAME@] %p [%t] %c{1}.%M(%L) | %m%n


### Logging to a Log file(transient logging)

log4j.appender.R=org.apache.log4j.RollingFileAppender
# You can change this to be an absolute path or even an environment variable
# If you're using an environment variable, you will have to set JAVA_OPTS
# to contain this variables - for example in the catalina.sh or catalina.bat
# file
log4j.appender.R.File=@APPNAME@.log
log4j.appender.R.MaxFileSize=100KB
# Don't keep a backup file
log4j.appender.R.MaxBackupIndex=0
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n



### Configuration for receiving e-mails when ERROR messages occur.

log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.To=@ERROR-MAILTO@
log4j.appender.mail.From=@ERROR-SERVER@
log4j.appender.mail.SMTPHost=@ERROR-MAILHOST@
log4j.appender.mail.Threshold=ERROR
log4j.appender.mail.BufferSize=1
log4j.appender.mail.Subject=[@ERROR-MAILHOST@] @APPNAME@ Application Error
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d %-5p %c %x - %m%n



### Configuration for logging ERROR messages to the database occur.

log4j.appender.db=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.db.Driver=net.sourceforge.jtds.jdbc.Driver
log4j.appender.db.URL=url
log4j.appender.db.user=id
log4j.appender.db.password=password
log4j.appender.db.Threshold=DEBUG
log4j.appender.db.sql=INSERT INTO LOGTEST (Date) VALUES (%d)
log4j.appender.db.layout=org.apache.log4j.PatternLayout


# If programmed properly the most messages would be at DEBUG
# and the least at FATAL.
log4j.logger.com.cpt.lis=DEBUG
log4j.logger.com.cpt.lis.webapp.filter=ERROR
log4j.logger.com.cpt.lis.webapp.listener.UserCounterListener=WARN
log4j.logger.com.cpt.lis.util=WARN

log4j.logger.com.cpt.lisclinicallab=DEBUG
log4j.logger.com.cpt.lisclinicallab.webapp.action=DEBUG
log4j.logger.com.cpt.lisclinicallab.webapp.filter=ERROR
log4j.logger.com.cpt.lisclinicallab.webapp.listener.UserCounterListener=WARN
log4j.logger.com.cpt.lisclinicallab.util=WARN

log4j.logger.com.cpt.liscommon=DEBUG
log4j.logger.com.cpt.liscommon.webapp.filter=ERROR
log4j.logger.com.cpt.liscommon.webapp.listener.UserCounterListener=WARN
log4j.logger.com.cpt.liscommon.util=WARN

log4j.logger.com.cpt.lisbloodbank=DEBUG

# Control logging for other open source packages
log4j.logger.com.opensymphony.oscache=ERROR
log4j.logger.net.sf.navigator=ERROR
log4j.logger.net.sf.acegisecurity=WARN
log4j.logger.net.sf.acegisecurity.intercept.event.LoggerListener=WARN
log4j.logger.org.apache.commons=ERROR
log4j.logger.org.apache.struts=WARN
log4j.logger.org.displaytag=ERROR
log4j.logger.org.springframework=WARN
log4j.logger.com.ibatis.db=WARN
log4j.logger.org.apache.velocity=WARN

# Don't show debug logs for WebTest
log4j.logger.com.canoo.webtest=WARN

# All hibernate log output of "info" level or higher goes to stdout.
# For more verbose logging, change the "info" to "debug" on the last line.
log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN
log4j.logger.org.hibernate=WARN

# Changing the log level to DEBUG will result in Hibernate generated
# SQL to be logged.
log4j.logger.org.hibernate.SQL=ERROR

# Changing the log level to DEBUG will result in the PreparedStatement
# bound variable values to be logged.
log4j.logger.org.hibernate.type=ERROR
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Originally posted by Ayyappan Selvaraj:
But I get SQL syntax error

Copy and paste the exact error message please...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Logging into database
 
Similar Threads
Newby Question on Loging in WLS6.1SP4
log4j Revoking the JDBCAppender after the Database Connection has been established
Logging (java.util.logging)
log4j problem - global logger?
adding custom log levels to config file