DBD::Log - a logging mechanism for the DBI.
use strict; use IO::File; use DBD::mysql; use DBD::Log;
my $dbh = DBI->connect("DBI:mysql:database=test");
my $fh = new IO::File "file", O_WRONLY|O_APPEND; $dbh = DBD::Log->new( dbi => $dbh, logFH => $fh, logThis => [ 'update', 'select' ], );
my $sth = $dbh->prepare("UPDATE table SET field=?, other=?, foo=?"); $sth->execute('green', 'good', 'bar');
# this logs into 'file': # # 1105018817 UPDATE table SET field='green', other='good', foo='bar'
$dbh->dbiLogging(1); $sth = $dbh->prepare("SELECT * FROM the_other_table WHERE username LIKE ?"); $sth->execute('%-idiots'); $sth->execute('%-guests');
# this logs # # 1105018818 [prepare] SELECT * FROM the_other_table WHERE username LIKE ? # 1105018818 SELECT * FROM the_other_table WHERE username LIKE '%-idiots' # 1105018819 SELECT * FROM the_other_table WHERE username LIKE '%-guests'
Appends logging to the DBI interface, but limits to the executed sql-statements. Written to support all the DBD::Drivers out there, but some (like Oracle) might cause problems.
Do not expect to overload the DBI without any consequences.
DBI, DBD::Something, IO::File & Carp
logThis()array-ref of sql-commands (eg: insert, update, delete, etc) to log. If left empty logs; insert, update, delete, select, create & drop
If set to [ 'all' ] logs everything.
logFH()The filehandle used for logging. You must supply your own, since I just could not figure out if you like to append or overwrite.
dbiLogging()0 or 1.
If set to 1 will log all the actions/function-calls of/to the DBI interface as well.
dbi()the $dbh of your script goes in here. $dbh->{LongReadLen} should be set as $dbi->dbi->{LongReadLen}
The logs are tab-seperated and in the following format:
time ([$function]) statement @rest
CORE::time of the writedown of the line.
The called DBI function. Only when $self->dbiLogging is TRUE.
The compiled statement.
Any excess parameters to the function that DBD::Log could not parse.
I have not had the opportunity, nor the time, to test this package against all the DBD::Drivers out there. Things might break do to your specific needs.
Since the real DBI is stored in ->dbi, all those special flags are
stored there 2. To get to mysql_insert_id(), go fetch
$dbi->dbi->{mysql_insert_id}
the DBI manpage, the DBD::Log::Sth manpage
Hartog C. de Mik <hartog@2organize.com> Lead Developer
(c) 2004 - 2organize, all rights reserved.