Thanks a lot for pointing out the usage of sub{...} in this context. This does the trick! I needed to do some minor tweaking of your code though as both the inline document <<"_SQL_" and the comment line were causing problems.
As a reference for others that might read/find this conversation here's the complete log4perl.conf file that did work (again, no error checking implemented!):
log4perl.rootLogger = TRACE, app_screen, app_db
# configuration for screen appender
log4perl.appender.app_screen = Log::Log4perl::Appender::Screen
log4perl.appender.app_screen.layout = Log::Log4perl::Layout::PatternLa
+yout
log4perl.appender.app_screen.layout.ConversionPattern = [%p] %m{indent
+}%n
# configuration for database logging
log4perl.appender.app_db = Log::Log4perl::Appender::DBI
log4perl.appender.app_db.datasource = sub { \
require DBI; \
my $connect_info = "dbi:SQLite:uri=file:log4perl.sqlite"; \
my $dbh = DBI->connect($connect_info); \
$dbh->do(' \
CREATE TABLE IF NOT EXISTS log ( \
priority, \
message, \
my_key \
)' \
); \
$connect_info; \
}
log4perl.appender.app_db.sql = \
insert into log \
(priority, my_key, message) \
values (?, ?, ? ) \
log4perl.appender.app_db.params.1 = %p
log4perl.appender.app_db.params.2 = %X{MDC_key}
log4perl.appender.app_db.usePreparedStm = 1
log4perl.appender.app_db.warp_message = 0
log4perl.appender.app_db.attrs.f_encoding = utf8
log4perl.appender.app_db.layout = Log::Log4perl::Layout::NoopLayout
You are certainly right that in general the database should be set up beforehand and for server-type DB engines this probably can be assumed to be the case. My use-case is to retrofit some existing applications with DB logging and SQLite is the simplest choice for this scenario. Thus putting all the DB setup stuff into the log4perl configuration is the simplest way for a self-contained log4perl setup without any change to the actual application codebase.
|