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(?);
Just to make sure; you can not use log4j and use JDBCAppender or DBAppender?
( 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 byLogger.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 specifyingurl,usernameandpassword.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:Now you can reference the logger in java code with:
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.