• Stars
    star
    13
  • Rank 1,467,650 (Top 30 %)
  • Language
    Perl
  • License
    Other
  • Created over 13 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Logging queries for DBI

Build Status

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

  • threshold

    If set, only queries that take more time than this threshold will be logged (default is undef)

      DBIx::QueryLog->threshold(0.1); # sec
    

    You can also specify this with DBIX_QUERYLOG_THRESHOLD environment variable.

  • probability

    If set, the logger logs only once per a defined value. (default is undef)

      DBIx::QueryLog->probability(100); # about 1/100
    

    You can also specify this with DBIX_QUERYLOG_PROBABILITY environment variable.

  • logger

    Sets a logger class (e.g. Log::Dispach)

    The logger class must have a `log` method, which should work like the one of Log::Dispatch (but see also OUTPUT section below).

      DBIx::QueryLog->logger($logger);
    
  • skip_bind

    If set, DBIx::QueryLog runs faster, but placeholders are not processed.

      DBIx::QueryLog->skip_bind(1);
      my $row = $dbh->do(...);
      # => 'SELECT * FROM people WHERE user_id = ?' : [1986]
    

    You can also specify this with DBIX_QUERYLOG_SKIP_BIND environment variable.

  • color

    If set, log messages will be colored with Term::ANSIColor.

      DBIx::QueryLog->color('green');
    

    You can also specify this with DBIX_QUERYLOG_COLOR environment variable.

  • useqq

    If set, DBIx::QueryLog uses $Data::Dumper::Useqq.

      DBIx::QueryLog->useqq(1);
    

    You can also specify this with DBIX_QUERYLOG_USEQQ environment variable.

  • compact

    If set, log messages will be compact.

      DBIx::QueryLog->compact(1);
      #  FROM: SELECT          *  FROM      foo WHERE bar = 'baz'
      #  TO  : SELECT * FROM foo WHERE bar = 'baz'
    

    You can also specify this with DBIX_QUERYLOG_COMPACT environment variable.

  • explain

    EXPERIMENTAL

    If set, DBIx::QueryLog logs the result of a EXPLAIN statement.

      DBIx::QueryLog->explain(1);
      my $row = $dbh->do(...);
      # => SELECT * FROM peaple WHERE user_id = '1986'
      #  .----------------------------------------------------------------------------------------------.
      #  | id | select_type | table  | type  | possible_keys | key     | key_len | ref   | rows | Extra |
      #  +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+
      #  |  1 | SIMPLE      | peaple | const | PRIMARY       | PRIMARY |       4 | const |    1 |       |
      #  '----+-------------+--------+-------+---------------+---------+---------+-------+------+-------'
    

    You can also specify this with DBIX_QUERYLOG_EXPLAIN environment variable.

  • show_data_source

    if set, DBI data_source will be added to the log messages.

      $dbh->do('SELECT * FROM sqlite_master');
      # [2012-03-09T00:58:23] [main] [0.000953] SELECT * FROM sqlite_master at foo.pl line 34
    
      DBIx::QueryLog->show_data_source(1);
      $dbh->do('SELECT * FROM sqlite_master');
      # [2012-03-09T00:58:23] [main] [0.000953] [SQLite:dbname=/tmp/TrSATdY3cc] SELECT * FROM sqlite_master at foo.pl line 56
    

    You can also specify this with DBIX_QUERYLOG_SHOW_DATASOURCE environment variable.

  • guard

    Returns a guard object.

      use DBIx::QueryLog ();
      {
          my $guard = DBIx::QueryLog->guard;
          # ... do something
      }
    

    The following code does the same:

      use DBIx::QueryLog ();
    
      DBIx::QueryLog->enable;
      # ... do something
      DBIx::QueryLog->disable;
    
  • ignore_trace

    Returns a guard object and disables tracing while the object is alive.

      use DBIx::QueryLog;
    
      # enabled
      $dbh->do(...);
    
      {
          my $guard = DBIx::QueryLog->ignore_trace;
          # disable
          $dbh->do(...);
      }
    
      # enabled
      $dbh->do(...)
    
  • is_enabled

    Returns if DBIx::QueryLog is enabled or not.

      use DBIx::QueryLog ();
    
      say DBIx::QueryLog->is_enabled;
    
      DBIx::QueryLog->disable;
    

    See also Localization section.

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 <Log::Dispatch>.

$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 <xaicron {at} cpan.org>

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

More Repositories

1

mysqlenv

mysql binary manager
Perl
67
star
2

p5-www-youtube-download

YouTube video download interface.
HTML
38
star
3

pm-uninstall

Uninstall modules
Perl
32
star
4

p5-JSON-WebToken

JSON Web Token (JWT) implementation for Perl
Perl
18
star
5

p5-win32-unicode

perl unicode-friendly wrapper for win32api.
Perl
13
star
6

p5-WWW-Google-Cloud-Messaging

Google Cloud Messaging (GCM) Client Library
Perl
10
star
7

Kagura

minimalistic web application framework
Perl
9
star
8

dotfiles

It's my dotfiles!
Vim Script
7
star
9

p5-Data-Validator-Recursive

recursive data friendly Data::Validator
Perl
7
star
10

p5-Getopt-Compact-WithCmd

sub-command friendly, like Getopt::Compact
Perl
6
star
11

p5-Test-Flatten

subtest output to a flatten
Perl
6
star
12

p5-Data-WeightedRoundRobin

Serve data in a Weighted RoundRobin manner.
Perl
5
star
13

p5-Module-Install-TestTarget

Assembles Custom Test Targets For `make`
Perl
5
star
14

p5-Data-Encoder

Generic interface for perl encoder or serializer.
Perl
5
star
15

p5-App-envfile

runs another program with environment modified according to envfile
Perl
4
star
16

p5-IO-Prompt-Simple

provide a simple user input
Perl
4
star
17

p5-send-gmail-simple

Very simple Gmail sending interface.
Perl
4
star
18

p5-SQL-Format

Yet another yet another SQL builder
Perl
3
star
19

p5-Test-Level

Perl
3
star
20

p5-WWW-FCM-HTTP

HTTP Client for Firebase Cloud Messaging
Perl
3
star
21

OreZen

oreore presentation tool
Perl
3
star
22

p5-ZenPAN

yet yet another DarkPAN repository manager.
Perl
3
star
23

LRU-Cache

BPStudy #29
Perl
2
star
24

p5-Net-APNs-HTTP2

APNs Provider API Client for Perl5
Perl
2
star
25

p5-Net-APNs-Extended

Client library for APNs
Perl
2
star
26

lingr2irc

IRC proxy for Lingr
Perl
2
star
27

Text-Wiki-Lite

Perl
2
star
28

p5-Acme-Saikyo

ใผใใŒใ‹ใ‚“ใŒใˆใŸใ•ใ„ใใ‚‡ใ†ใฎใ€ŒใผใใŒใ‹ใ‚“ใŒใˆใŸใ•ใ„ใใ‚‡ใ†ใฎโ—‹โ—‹ใ€ใ‚’ใ—ใ‚…ใคใ‚Šใ‚‡ใใ™ใ‚‹ใ‚‚ใ˜ใ‚…ใƒผใ‚‹
Perl
2
star
29

p5-Test-Script-Shebang

check the perl script shebang
Perl
2
star
30

p5-WWW-Google-C2DM

Google C2DM Client
Perl
2
star
31

Mayoi

database test sample
Perl
2
star
32

p5-script-require

hook script-file
Perl
2
star
33

static.xaicron.dotcloud.com

Perl
2
star
34

Pod-Viewer

pod viewer for browser
Perl
1
star
35

Nadeko

ใŠๅ…„ใกใ‚ƒใ‚“ใฏๅคงไบบใ ใ‹ใ‚‰(ry
Java
1
star
36

bookmarklet

JavaScript
1
star
37

p5-cgi-param-filter

Filter for CGI param
Perl
1
star
38

tialog

Perl
1
star
39

p5-Object-Container-Flexible

Flexible Object::Container
Perl
1
star
40

p5-Task-BeLike-XAICRON

my favorite modules
Perl
1
star
41

p5-Micro-Container

Lite weight and inheritable object container
Perl
1
star
42

p5-Module-Install-TestPreload

preload codes for test
Perl
1
star
43

p5-WWW-Google-ClientLogin

Yet Another Google ClientLogin Client
Perl
1
star
44

p5-Sub-Fallback

fallback for each callbacks
Perl
1
star
45

isucon

Perl
1
star
46

p5-Acme-Crypt-LLDecade

encrypt / decrypt from Crypt LLDecade quiz
Perl
1
star
47

yapc-okinawa-2018

YAPC::Okinawa 2018 demo codes
Perl
1
star
48

p5-DateTimeX-CGI-Expires

Create DateTime Object from (CGI) expires
Perl
1
star
49

p5-CGI-Upload-Simple

CGI::Uploadใฎ็ฐกๅ˜็‰ˆ
Perl
1
star
50

p5-Acme-Shiin

Consonantalize Japanese.
Perl
1
star
51

p5-wget-progress

file download module
Perl
1
star
52

p5-Test-Successful

it's a joke
Perl
1
star
53

p5-WWW-Gumroad

gumroad api client
Perl
1
star
54

p5-Sub-Pipe-HTML

chain subs HTML methods
Perl
1
star
55

Catalyst-Plugin-Lazy-Encoding

auto decode request param and auto encode response body.
Perl
1
star
56

p5-WWW-Vimeo-Download-Lite

Download videos from Vimeo
Perl
1
star
57

WWW-Google-Auth-ClientLogin

Perl module to interact with Google's ClientLogin protocol
Perl
1
star
58

p5-template-plugin-yaml-encode

Encode supported YAML
Perl
1
star
59

jsonrpcstatus

Display JSON-RPC error status code information
Perl
1
star
60

Acme-CheckCharactorsOutsideRange

ใ‚นใ‚ฏใƒชใƒ—ใƒˆใซๆŒ‡ๅฎšใ—ใŸๆ–‡ๅญ—ใ‚ณใƒผใƒ‰ใฎ็ฏ„ๅ›ฒๅค–ใซใ‚ใ‚‹ๆ–‡ๅญ—ใŒๅซใพใ‚Œใฆใ„ใŸๅ ดๅˆใซๆญปใฌ
Perl
1
star