The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DBIx::QueryLog - Logging queries for DBI

SYNOPSIS

use DBIx::QueryLog;
my $row = $dbh->selectrow_hashref('SELECT * FROM people WHERE user_id = ?', undef, qw/1986/);
# => SELECT * FROM people WHERE user_id = '1986';

DESCRIPTION

DBIx::QueryLog logs each execution time and the actual query.

Currently, it works with DBD::mysql, DBD::Pg and DBD::SQLite.

CLASS METHODS

TIPS

Localization

If you want to log only in a specific scope:

use DBIx::QueryLog (); # or require DBIx::QueryLog;

DBIx::QueryLog->begin; # or DBIx::QueryLog->enable
my $row = $dbh->do(...);
DBIx::QueryLog->end;   # or DBIx::QueryLog->disable

DBIx::QueryLog logs only between begin and end.

LOG_LEVEL

When you set a logger, you might also want to change a log level.

$DBIx::QueryLog::LOG_LEVEL = 'info'; # default 'debug'

OUTPUT

If you want to change where to output:

open my $fh, '>', 'dbix_query.log';
$DBIx::QueryLog::OUTPUT = $fh;

You can also specify a code reference:

$DBIx::QueryLog::OUTPUT = sub {
    my %params = @_;

    my $format = << 'FORMAT';
localtime  : %s       # ISO-8601 without timezone
level      : %s       # log level ($DBIx::QueryLog::LOG_LEVEL)
time       : %f       # elasped time
data_source: $s       # data_source
sql        : %s       # executed query
bind_params: %s       # bind parameters
pkg        : %s       # caller package
file       : %s       # caller file
line       : %d       # caller line
FORMAT

    printf $format,
        @params{qw/localtime level pkg time data_source sql/},
        join(', ', @{$params{bind_params}}),
        @params{qw/file line/};

    printf "AutoCommit?: %d\n", $params->{dbh}->{AutoCommit} ? 1 : 0;
};

You can also use this if you want to use a logger that doesn't have a log method like the one of >.

$DBIx::QueryLog::OUTPUT = sub {
    my %params = @_;
    my $logger = Log::Any->get_logger;
    $logger->debug("$params{message}");
};

Note that this only works when <logger> is not set.

Default $OUTPUT is STDERR.

AUTHOR

xaicron

THANKS TO

tokuhirom

yibe

kamipo

tomi-ru

riywo

makamaka

BUG REPORTING

Plese use github issues: https://github.com/xaicron/p5-DBIx-QueryLog/issues.

COPYRIGHT

Copyright 2010 - xaicron

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

DBI