|
From: Kevin G. <ke...@go...> - 2002-12-09 19:58:01
|
I needed to add logging to a database, here's a proposed solution.
Log4j has a JDBCAppender with the warning: "This version of JDBCAppender
is very likely to be completely replaced in the future." The API is
kind of different, you call append() until the buffer is full, at which
point the an sth is created and executed on the buffer. So the API
isn't interchangeable with other appenders.
So I've implemented a DBI appender that's completely controllable from
the config file, looking something like this:
log4j.category = WARN, DBAppndr
log4j.appender.DBAppndr = Log::Log4perl::Appender::DBI
log4j.appender.DBAppndr.datasource = DBI:CSV:f_dir=t/tmp
log4j.appender.DBAppndr.username = bobjones
log4j.appender.DBAppndr.password = 12345
log4j.appender.DBAppndr.sqlStatement = \
insert into log4perltest \
(level, message, shortmessage) \
values (?,?,?)
log4j.appender.DBAppndr.params.1 = %p
log4j.appender.DBAppndr.params.2 = %m
log4j.appender.DBAppndr.params.3 = %5.5m
...
The code is based on Log::Dispatch::DBI by Tatsuhiko Miyagawa. His
module gets behavior changes via subclassing, but by the time I was done
I had overridden everything but DESTROY, so I just dropped the
dependency on his module.
Below is the only change to existing code, the module and a unit test
are attached. Any comments or suggestions?
============================================================
--- lib/Log/Log4perl/Appender.pm 18 Nov 2002 20:03:39 -0000 1.17
+++ lib/Log/Log4perl/Appender.pm 9 Dec 2002 19:51:31 -0000
@@ -120,7 +120,10 @@
$level,
3 +
$Log::Log4perl::caller_depth,
);
- $self->{appender}->log(%$p);
+ $self->{appender}->log(%$p,
+ #these are used by our Appender::DBI
+ log4p_category => $category,
+ log4p_level => $level,);
return 1;
}
--
Happy Trails . . .
Kevin M. Goess
(and Anne and Frank)
904 Carmel Ave.
Albany, CA 94706
(510) 525-5217
|