You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by AF Techie <af...@yahoo.com> on 2002/05/03 19:04:51 UTC

JDBCAppender - using different table def and SQL

Hi,

I'm attempting to get the JDBCAppender to work, but a monkey-wrench has
been thrown into the works:

I can't simply log a single message string.  Requirements dictate that
several fields in the audit table must be filled.

I think I've discovered how to set SQL statement, but how can I set the
SQL parameters for these table columns?  What's the best way to do this
whole thing?

Thanks,
AJ


__________________________________________________
Do You Yahoo!?
Yahoo! Health - your guide to health and wellness
http://health.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: JDBCAppender - using different table def and SQL

Posted by Jeffrey Winter <j....@niteo.com>.
So I assume from your question that you understand that the sql string you
pass into setSql is just a regular conversion pattern and that all the
substitution rules apply, correct?  (e.g., "insert into error_log (location,
description) values('%c', '%m')", will substitute the message text for %m
etc.)

If you are saying you need more than this, there are basically 2 roots you
can go:

1.    The sort-of-hacky route:

    place all the data you need into the regular message string with your
own special delimiters
    override, JDBCAppender.getLogStatement() and reformat the converted SQL
string as you wish: i.e.:

    protected String getLogStatement(LoggingEvent event) {

        String rawMessage = (String) event.getMessage();

        // extract your data into it's constituent parts

        String rawSql = getLayout().format(event);

        // rebuild the sql using your data

        return sql

    }

2.    Extend Log4J with your own extention of LoggingEvent and
PatternParser.

There is an example of how to do this here:

http://www.ingrid.org/jajakarta/log4j/jakarta-log4j-1.1.3/docs/deepExtension
.html

One note though, this example only shows how to add "static" parameters,
i.e., parameters that are supplied to the static logger instance at create
time.  I wanted to do what you seem to be asking for, which was for a richer
set of parameters passed to the error logger itself for each log entry.  It
doesn't take much work to make that possible too.

For example, in our JDBCAppender, we needed to supply a public key and a
serial number with each log entry, so to our Logger extension we added
methods that take those parameters, build up our own LogMessage (just a bean
that holds data) and pass that to the regular methods:

  /**
   * Logs an error message by first constructing a LogMessage from the
supplied parameters
   * the calling the base error(Object message) method.
   */
  public void error(PublicKey key, SerialNumber ssn, String message) {

    LogMessage logMessage = new LogMessage(id, message);

    error(logMessage);

  }

Once all this is in place, you can also put most of the configuration stuff
into an external DOMConfigurator file so your JDBCAppender extension doesn't
need to do much more than supply a Connection.

Send me a private email and I can send you some more detailed examples if
you get stuck.

- Jeff








----- Original Message -----
From: "AF Techie" <af...@yahoo.com>
To: <lo...@jakarta.apache.org>
Sent: Friday, May 03, 2002 1:04 PM
Subject: JDBCAppender - using different table def and SQL


> Hi,
>
> I'm attempting to get the JDBCAppender to work, but a monkey-wrench has
> been thrown into the works:
>
> I can't simply log a single message string.  Requirements dictate that
> several fields in the audit table must be filled.
>
> I think I've discovered how to set SQL statement, but how can I set the
> SQL parameters for these table columns?  What's the best way to do this
> whole thing?
>
> Thanks,
> AJ
>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Health - your guide to health and wellness
> http://health.yahoo.com
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>