The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

DBI::Log - Log all DBI queries

SYNOPSIS

use DBI::Log;

DESCRIPTION

You can use this module to log all queries that are made with DBI. You just need to include it in your script and it will work automatically. By default, it will send output to STDERR, which is useful for command line scripts and for CGI scripts since STDERR will appear in the error log.

If you want to log elsewhere, set the file option to a different location.

use DBI::Log file => "~/querylog.sql";

Each query in the log is prepended with the date and the place in the code where it was run from. You can add a full stack trace by setting the trace option.

use DBI::Log trace => 1;

If you want timing information about how long the queries took to run add the timing option.

use DBI::Log timing => 1;

If you want to exclude function calls from within a certain package appearing in the stack trace, you can use the exclude option like this:

use DBI::Log exclude => ["DBIx::Class"];

It will exclude any package starting with that name, for example DBIx::Class::ResultSet DBI::Log is excluded by default.

The log is formatted as SQL, so if you look at it in an editor, it might be highlighted. This is what the output may look like:

-- Fri Sep 11 17:31:18 2015
-- execute t/test.t 18
CREATE TABLE foo (a INT, b INT)

-- Fri Sep 11 17:31:18 2015
-- do t/test.t 21
INSERT INTO foo VALUES ('1', '2')

-- Fri Sep 11 17:31:18 2015
-- selectcol_arrayref t/test.t 24
SELECT * FROM foo

-- Fri Sep 11 17:31:18 2015
-- do t/test.t 27
-- (eval) t/test.t 27
INSERT INTO bar VALUES ('1', '2')

There is a built-in way to log with DBI, which can be enabled with DBI->trace(1), but the output is not easy to read through.

This module integrates placeholder values into the query, so the log will contain valid queries.

METACPAN

https://metacpan.org/pod/DBI::Log

REPOSITORY

https://github.com/zorgnax/dbilog

AUTHOR

Jacob Gelbman, <gelbman@gmail.com>

CONTRIBUTORS

Árpád Szász, <arpad.szasz@plenum.ro> Pavel Serikov David Precious

COPYRIGHT AND LICENSE

Copyright (C) 2015 by Jacob Gelbman

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.18.2 or, at your option, any later version of Perl 5 you may have available.