Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: Log4perl: Create missing SQLite file and log to it

by mbloecker (Novice)
on May 10, 2019 at 09:23 UTC ( [id://1233553]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Log4perl: Create missing SQLite file and log to it
in thread Log4perl: Create missing SQLite file and log to it

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1233553]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-16 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found