tags:

views:

119

answers:

3

Hey folks.

Re-thinked IDEA!

So, I've been thinking over the weekend and couldn't let this one go. I've decided to re-try the idea of getting log4j to work.

I've been doing some coding and think I got it to work. Except, I do not really understand how to insert into the logger. I've created an JDBC-appender and the SQL looks like this:

    <layout class="org.apache.log4j.PatternLayout"> 
        <param name="ConversionPattern" 
          value="INSERT INTO audit (timestamp, user, operation, source, resourceRange, additionalInfo) VALUES ('%d{yyyy-MM-dd H:mm:s,SSS}','%m', '%m', '%m', '%m', '%m')"
        /> 
    </layout> 

Is this correct? All values has the VARCHAR datatype.

But, the real question for me is how do I log this in the Java-code?
Can't really understand what values to insert to get the correct values to insert the database.

 logger.log(?);

EDIT Problem RESOLVED. Apparently I've mixed up how the original JDBC-appender for Log4j works. It is not doable to get several inserts to the database through a log message. Instead, I used "jdbcplus" made by a German1. Thank you all for the help./Edit

A: 

I would advise against logging with java, but use mysql's built in logging: http://dev.mysql.com/doc/refman/5.0/en/server-logs.html

If you need to use data from the log in your program, open the file programmaticaly and read in from there when you need to generate stats etc. This will save you a huge amount of overhead.

fredley
he wants to log application-level events from his Java program, this has nothing to do with logging at the database server-level
matt b
A: 

LogBack offers logging to a database out of the box.

Sylar
+2  A: 

Just to make sure; you can not use log4j and use JDBCAppender or DBAppender? ( http://stackoverflow.com/questions/1364322/log-to-a-database-using-log4j )

EDIT:

My suggestion would be to have a named appender that logs to the database.

By turning off additivity (log4j.additivity.database=false), it won't "push" log statements to the root logger, keeping that data out of the default log file/console. The named logger can be retrieved by Logger.getLogger("database"); from different places in the source code.

EDIT 2:

I think it would've been better to let the original question stand, and ask a new one, but here goes. :)

The appender must contain information about the database connection, either as a reference to the connection: <param name="connector" value="name.of.existing.ConnectionHandler" /> or by specifying url, username and password.

Setting up the logger, the name is the important bit for getting the logger. By setting the loggers additivity to fale, you stop it from adding this logging to the default logger specified by <root> in the config:

<appender name="JDBC" class="org.apache.log4j.jdbcplus.JDBCAppender">
    [...]
</appender>
<logger name="myLogger" additivity="false">
    <appender-ref ref="JDBC"/>
    [...]
</logger>

Now you can reference the logger in java code with:

private static final Logger usageLog = Logger.getLog("myLogger");

from any java file. Then you use the logger like always, with usageLog.debug("Debug message"); or something.

EDIT 3:

See PatternLayout javaDoc for which flags to use in the ConversionPattern.

Tormod
Unfortunately because of various reasons, that is not an option but I'm aware of that it is normally a great option. Thanks for your answer!
Fredrik_jakob
@Fredrik_jakob, why is it not an option? Perhaps there is a misconception about using JDBCAppender/DBAppender/log4j which we can clear up for you. It is likely far simpler to re-use a proven library and code than attempting to roll your own.
matt b
I followed your idea and posted a new question about Log4j.I kind of managed to create a Java-logging-thingy, but I can't say I'm satisfied so I'm prepared to do this idea instead and try counter its problems.
Fredrik_jakob
Edited to answer your edited question
Tormod
Thank you! I got the jdgbc-appender to work and it inserts information to the database. the problem is that I cant figure out how to get different information into the database? because I have %m as VALUES in the SQL-query all columns will be filled with 'abc' if I do this:logger.log("abc");.
Fredrik_jakob