2

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(?);
Tunaki
  • 132,869
  • 46
  • 340
  • 423
user373455
  • 12,675
  • 4
  • 32
  • 46
  • 4
    Replacing log4j logging in a big application by a self-developed logging framework? Boy, what did you do wrong this time? – Andreas Dolk Aug 12 '10 at 11:34
  • This logging is "simply" for auditing what types of functionality used. Still use log4j in other types of logging. So it's not that big, might been a bad explanation :). – user373455 Aug 12 '10 at 11:39

2 Answers2

3

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 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.

Community
  • 1
  • 1
Tormod
  • 454
  • 3
  • 7
  • 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! – user373455 Aug 12 '10 at 11:44
  • 1
    @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 Aug 12 '10 at 13:08
  • 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. – user373455 Aug 15 '10 at 14:10
  • 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");. – user373455 Aug 16 '10 at 10:12
0

LogBack offers logging to a database out of the box.

Sylar
  • 2,273
  • 2
  • 18
  • 26