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 $DBI::Log::path variable to a different location.

    $DBI::Log::path = "~/querylog.sql";

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 taking 0 seconds
    -- execute t/test.t 18
    CREATE TABLE foo (a INT, b INT)

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

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

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

Each query in the log is prepended with the date, the time it took to run, and a stack trace. You can disable the stack trace by setting $DBI::Log::trace to a false value.

You can set $DBI::Log::array to a true value and then all queries will end up in @DBI::Log::queries.

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>

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.