my dog learned polymorphism*
The moose likes Other Application Frameworks and the fly likes Generating Logs & storing them in DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Generating Logs & storing them in DB" Watch "Generating Logs & storing them in DB" New topic
Author

Generating Logs & storing them in DB

Ameya Thakur
Ranch Hand

Joined: Feb 04, 2004
Posts: 43
Hi !!!

I am trying to generate the logs using log4j i.e by using logging-log4j-1.2.14.jar

I am able to generate the logs with the following log4.properties file


log4j.rootLogger=debug,stdout ,R
log4j.level=ALL

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=cor.log

log4j.appender.R.MaxBackupIndex=10

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.R.layout.ConversionPattern=%5p [%d] (%F:%L) - %m%n


Now I wanted to add the generated logs files in the Database for which I am using the following config-JDBC.properties file

log4j.rootLogger=debug, R

# Since we plan to log to a database, we tell log4j that we will use the
# JDBCAppender.

log4j.appender.R=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.R.URL=jdbc:microsoft:sqlserver://TEST_SQL:1433;CCLogging;
log4j.appender.R.user=corconn
log4j.appender.R.password=2hopa
log4j.appender.R.sql=INSERT INTO JDBCTEST (Message) VALUES ('%d - %c - %p - %m')
log4j.appender.R.layout=org.apache.log4j.PatternLayout


now my JdbcTest Java file goes here which contains the logging information


package com.cs.test;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.db.DBAppender;
import org.apache.log4j.db.DriverManagerConnectionSource;


import org.apache.log4j.jdbc.JDBCAppender;
import org.apache.log4j.db.DriverManagerConnectionSource;

public class JdbcTest {
static Logger logger = Logger.getLogger(JdbcTest.class);
public JdbcTest() {
}

public static void main(String[] args) {
PropertyConfigurator.configure("config-JDBC.properties");
DriverManagerConnectionSource connectionSource = new DriverManagerConnectionSource();
connectionSource.setDriverClass("com.microsoft.jdbc.sqlserver.SQLServerDriver");
connectionSource.setUrl("jdbc:microsoft:sqlserver://TEST_SQL:1433;databaseName=CCLogging");
connectionSource.setUser("corconn");
connectionSource.setPassword("2hopa");
connectionSource.activateOptions();
DBAppender dbAppender = new DBAppender();
dbAppender.setLocationInfo(true);
dbAppender.setConnectionSource(connectionSource);
dbAppender.activateOptions();
logger.addAppender(dbAppender);
JdbcTest jdbcTest = new JdbcTest();
jdbcTest.doit2();
}
public void doit2() {
logger.debug("Debug message.");
logger.info("Info message.");
logger.warn("Warn message.");
logger.error("Error message.");
logger.fatal("Fatal message.");
}
}


After executing the above java file I am getting the following error..

0 [main] INFO org.apache.log4j.db.dialect.Util - Could not call supportsGetGeneratedKeys method. This may be recoverable
java.lang.NoSuchMethodError: org.apache.log4j.AppenderSkeleton: method <init>(Z)V not found
at org.apache.log4j.db.DBAppender.<init>(DBAppender.java:160)
at com.cs.test.JdbcTest.main(JdbcTest.java:36)
Exception in thread "main"


So could please any one help me resolve this problem.

Thanks and Regards
Ameya Thakur
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
The error makes it sound like you've got more than one version of log4j.

Beyond that, why are you configuring your db appender in the properties file and programmatically?

Is it using the properties file? Try adding -Dlog4j.debug to the command line or add log4j.debug=true to the properties file. It will put out some debug about how it is (or isn't) being configured.

And if you are looking for previously answered questions, try the Other Open Source Projects forum

Other Open Source Projects
Jakarta Projects (like Log4j etc.), SourceForge and other Open Source Projects (Ant has its own forum)

[ November 29, 2006: Message edited by: Carol Enderlin ]
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
I see you already found the open source forum...

CarefullyChooseOneForum

You waste people's time when you post in multiple forums.
Ameya Thakur
Ranch Hand

Joined: Feb 04, 2004
Posts: 43
Hi Carol Thanks for the response.

Now I am not getting that error anymore.. i was trying to implement log4J, since I did not had hands on ,I was trying a lot probably.

Now I am able to insert the logs in the table with some modifications which goes as follows

You were right i was using both the version of Log4j ie

log4j-all-1.3alpha-8.jar and
log4j-1.2.14.jar


The java code is as follows

public static void main(String[] args) {
PropertyConfigurator.configure("config-JDBC.properties");
DriverManagerConnectionSource connectionSource = new DriverManagerConnectionSource();
connectionSource.setDriverClass("com.microsoft.jdbc.sqlserver.SQLServerDriver");
connectionSource.setUrl("jdbc:microsoft:sqlserver://TEST_SQL:1433;databaseName=CCLogging;");
connectionSource.setUser("corconn");
connectionSource.setPassword("2hopa");
connectionSource.activateOptions();
JdbcTest jdbcTest = new JdbcTest();
jdbcTest.doit2();
}

public void doit2() {
logger.debug("Debug message.");
logger.info("Info message.");
logger.warn("Warn message.");
logger.error("Error message.");
logger.fatal("Fatal message.");
}



#############################################################

The contents of the config-JDBC.properties are as follows

log4j.rootLogger=debug, R,File
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.File=Test.log
log4j.appender.File.DatePattern='.'yyyy-MMM-dd
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern = %d | %5p | [%t] | %c:%M(%L) | %m %n

log4j.appender.R=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.R.URL=jdbc:microsoft:sqlserver://TEST_SQL:1433;databaseName=CCLogging;
log4j.appender.R.user=corconn
log4j.appender.R.password=2hopa
log4j.appender.R.sql=INSERT INTO logging_event (rendered_message) VALUES ('%d - %c - %p - %m')

log4j.appender.R.layout=org.apache.log4j.PatternLayout




Now a simple question over here why do we require to give the query in the property file as we would wanted it to pick up the dynamic values...

Regards
Ameya
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Are you saying it isn't replacing the values in the insert?

The wiki has an xml configuration example.

They put the sql into the layout's conversion pattern rather than the appender's sql value. Did you try that?
Ameya Thakur
Ranch Hand

Joined: Feb 04, 2004
Posts: 43
Hi !!
Thanks for the prompt response.
The values are getting inserted in the DB as

2006-11-30 11:02:11,130 - com.cs.test.JdbcTest - INFO - Info message.
ie which we configured in our property file
log4j.appender.R.sql=INSERT INTO logging_event (rendered_message) VALUES ('%d - %c - %p - %m')

now this is perfectly fine , now I wanted to add few more data ie some value which would be comming dynamically like some field value or anything which is dynamic, so how do i go about with it.

Thanks & Regards
Ameya
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Well, you haven't given me much to work with trying to figure out what you are really asking.

Something dynamic you want to do.

The simplest answer to the question I'll imagine you are asking is to say to concatenate into a String whatever you are wanting to log, and use that as the message.

The short manual briefly touches on using an ObjectRenderer if you have certain objects you want to "log". Note that the log/debug/etc take an Object and not just a String.

I suppose you could do something like use something likeMessageFormat
to do create the message for you.

Please clarify with an example if that is not at all what you were asking about.
Ameya Thakur
Ranch Hand

Joined: Feb 04, 2004
Posts: 43
Hi Carol!!

First let me thank you for helping me out.

What I meant was suppose there are some fields which get populated dynamically How can I get the details of those fields in my configuration file, is there any way in which i can do that.

Also how do I capture the method name in the logs??

When i use the following property in my log4j.properties file

log4j.appender.R.sql=INSERT INTO logging_event(rendered_message,log_date,level_string,caller_class) VALUES ('%m','%d{ISO8601}','%L','%C')



These values are quiet confusing ie '%m','%d','%L','%C'
Where are these values defined , only when I excetued , I came to know about these values
ie m stands for message and C stands for the class, Is there any way in which we can get all the details about these values, are they specified in some configuration file ??

Once again let me thank u for helping me out.

Regards,
Ameya
[ November 30, 2006: Message edited by: Ameya Thakur ]
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
OK, so I completely missed the point.

Originally posted by Ameya Thakur:
These values are quiet confusing ie '%m','%d','%L','%C'
Where are these values defined , only when I excetued , I came to know about these values
ie m stands for message and C stands for the class, Is there any way in which we can get all the details about these values, are they specified in some configuration file ??


The values are defined in thePatternLayout javadoc.

You asked about including method, so be sure to take note of the performance warnings:
The location information can be very useful. However, it's generation is extremely slow. It's use should be avoided unless execution speed is not an issue.
Ameya Thakur
Ranch Hand

Joined: Feb 04, 2004
Posts: 43
Hi Carol!!!
Thanks for the reponse ..I am currenlty working on the inputs given by you...appreciated your timely help.
Thanks a lot once again... I would keep you posted about any issues if I face .

Bye for now and keep rocking
Ameya
Ameya Thakur
Ranch Hand

Joined: Feb 04, 2004
Posts: 43
Hi Carol!!
I hope you got my previous note.

Now I have gone through the API's which are using our own internal logging mechanism.

Is there any chances that with overriding some of the classes of LOG4j there wont be much code changes??

For eg my Logger class contains the following methods...now can I add these methods in the log4j Logger class or something like that.

public void log(LogLevel,int aEventCategory,aEventText,Object );
public void log(LogLevel,int aEventCategory,aEventText,Object,Throwable);
public void log(LogLevel,int aEventCategory,aEventText,aEventDetail,Object)
public void log(LogLevel,aEventCategory,aEventText,aEventDetail,Object aObjectInstance,Throwable);

Have a nice weekend

Thanks in Advance
Bye Bye
Ameya
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
You should probably start a new post to ask about replacing your own logging system with something like log4j.
Ameya Thakur
Ranch Hand

Joined: Feb 04, 2004
Posts: 43
Hi Carol!!

I have opened a new topic

http://www.coderanch.com/t/63221/open-source/Replacing-Proprietary-Logging-system-log

Regards,
Ameya
 
Don't get me started about those stupid light bulbs.
 
subject: Generating Logs & storing them in DB