mbloecker has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to setup Log4perl to optionally log into a SQLite database. With the following code:
and this Log4perl configuration:use strict; use warnings; use Log::Log4perl; # initialize logging Log::Log4perl->init('log4perl.conf'); my $log = Log::Log4perl->get_logger(); # perform some logging including MDC $log->info("Logging initialized"); Log::Log4perl::MDC->put("MDC_key", "my MDC value"); $log->info("MDC used");
this works fine as long as the SQLite database has been setup beforehand. Namely the log table needs to be setup correctly.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 = dbi:SQLite:uri=file:log4perl.sql +ite 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
If the log4perl.sqlite file is not present when the above code is run an empty SQLite file is generated by DBD::SQLite during Log4perl initialization (as expected). But when the first logging statement is executed the perl code certainly errors out with
as the log table has not been set up in the SQLite database.Log4perl: DBI->prepare failed no such table: log insert into log (priority, my_key, message) values (?, ?, ? ) at C:\Us +ers\bloeckm\Perl\DB_logging\log4perl_dbi_demo.pl line 13.
My concern is that the SQLite file might (accidentally or intentionally) be deleted by the user or not be present in the first place. I was wondering if there was any way to implement/configure Log4perl in a "self-contained" way so that the SQLite file will be created properly if it does not (yet) exist. I have been trying to wrap my head around this but have not found a way to put the required CREATE TABLE IF NOT EXISTS SQL statements into the Log4perl configuration file. Is there any way to do that?
Or is there any way to programmatically detect that a DBI::SQLite appender has been configured and get the associated DB handle? With the respective DB handle it should be possible to achieve what I want using CREATE TABLE IF NOT EXISTS.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Log4perl: Create missing SQLite file and log to it
by thanos1983 (Parson) on May 09, 2019 at 13:29 UTC | |
by Marshall (Canon) on May 10, 2019 at 03:20 UTC | |
by mbloecker (Novice) on May 09, 2019 at 15:35 UTC | |
by Your Mother (Archbishop) on May 09, 2019 at 16:57 UTC | |
by mbloecker (Novice) on May 10, 2019 at 09:23 UTC |