Logging your SQL statements with Log::Any

In the accidental quest of conquering the logging world with Log::Any, presenting Log::Any::For::DBI. I needed something like DBIx::Log4perl but for Log::Any, so I wrote one. Internally it's much simpler than DBIx::Log4perl currently and only logs method calls like connect(), prepare(), do(), selectrow_hashref(). Suggest or send a patch if you want more features. The module builds upon the more general Log::Any::For::Class.

To quickly demonstrate it, we'll use something like Log::Any::App to show logs to the terminal:

$ TRACE=1 perl -MLog::Any::App -MDBI -MLog::Any::For::DBI \
  -e'$dbh=DBI->connect("dbi:SQLite:dbname=/tmp/tmp.db", "", ""); 
[1] -> DBI::connect(['dbi:SQLite:dbname=/tmp/tmp.db','','********'])
[5] <- DBI::connect() = [bless( {}, 'DBI::db' )]
[5] -> DBI::db::do(['CREATE TABLE IF NOT EXISTS t (i INTEGER)'])
[5] -> DBI::db::prepare(['CREATE TABLE IF NOT EXISTS t (i INTEGER)',undef])
[5] <- DBI::db::prepare() = [bless( {}, 'DBI::st' )]
[5] -> DBI::st::execute([])
[5] <- DBI::st::execute() = ['0E0']
[5] <- DBI::db::do()

It even masks the connection password for you.

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays).