1DBIx::QueryLog(3pm)   User Contributed Perl Documentation  DBIx::QueryLog(3pm)
2
3
4

NAME

6       DBIx::QueryLog - Logging queries for DBI
7

SYNOPSIS

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

DESCRIPTION

14       DBIx::QueryLog logs each execution time and the actual query.
15
16       Currently, it works with DBD::mysql, DBD::Pg and DBD::SQLite.
17

CLASS METHODS

19       threshold
20           If set, only queries that take more time than this threshold will
21           be logged (default is undef)
22
23             DBIx::QueryLog->threshold(0.1); # sec
24
25           You can also specify this with "DBIX_QUERYLOG_THRESHOLD"
26           environment variable.
27
28       probability
29           If set, the logger logs only once per a defined value. (default is
30           undef)
31
32             DBIx::QueryLog->probability(100); # about 1/100
33
34           You can also specify this with "DBIX_QUERYLOG_PROBABILITY"
35           environment variable.
36
37       logger
38           Sets a logger class (e.g. Log::Dispach)
39
40           The logger class must have a `log` method, which should work like
41           the one of Log::Dispatch (but see also OUTPUT section below).
42
43             DBIx::QueryLog->logger($logger);
44
45       skip_bind
46           If set, DBIx::QueryLog runs faster, but placeholders are not
47           processed.
48
49             DBIx::QueryLog->skip_bind(1);
50             my $row = $dbh->do(...);
51             # => 'SELECT * FROM people WHERE user_id = ?' : [1986]
52
53           You can also specify this with "DBIX_QUERYLOG_SKIP_BIND"
54           environment variable.
55
56       color
57           If set, log messages will be colored with Term::ANSIColor.
58
59             DBIx::QueryLog->color('green');
60
61           You can also specify this with "DBIX_QUERYLOG_COLOR" environment
62           variable.
63
64       useqq
65           If set, DBIx::QueryLog uses $Data::Dumper::Useqq.
66
67             DBIx::QueryLog->useqq(1);
68
69           You can also specify this with "DBIX_QUERYLOG_USEQQ" environment
70           variable.
71
72       compact
73           If set, log messages will be compact.
74
75             DBIx::QueryLog->compact(1);
76             #  FROM: SELECT          *  FROM      foo WHERE bar = 'baz'
77             #  TO  : SELECT * FROM foo WHERE bar = 'baz'
78
79           You can also specify this with "DBIX_QUERYLOG_COMPACT" environment
80           variable.
81
82       explain
83           EXPERIMENTAL
84
85           If set, DBIx::QueryLog logs the result of a "EXPLAIN" statement.
86
87             DBIx::QueryLog->explain(1);
88             my $row = $dbh->do(...);
89             # => SELECT * FROM peaple WHERE user_id = '1986'
90             #  .----------------------------------------------------------------------------------------------.
91             #  | id | select_type | table  | type  | possible_keys | key     | key_len | ref   | rows | Extra |
92             #  +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+
93             #  |  1 | SIMPLE      | peaple | const | PRIMARY       | PRIMARY |       4 | const |    1 |       |
94             #  '----+-------------+--------+-------+---------------+---------+---------+-------+------+-------'
95
96           You can also specify this with "DBIX_QUERYLOG_EXPLAIN" environment
97           variable.
98
99       show_data_source
100           if set, DBI data_source will be added to the log messages.
101
102             $dbh->do('SELECT * FROM sqlite_master');
103             # [2012-03-09T00:58:23] [main] [0.000953] SELECT * FROM sqlite_master at foo.pl line 34
104
105             DBIx::QueryLog->show_data_source(1);
106             $dbh->do('SELECT * FROM sqlite_master');
107             # [2012-03-09T00:58:23] [main] [0.000953] [SQLite:dbname=/tmp/TrSATdY3cc] SELECT * FROM sqlite_master at foo.pl line 56
108
109           You can also specify this with "DBIX_QUERYLOG_SHOW_DATASOURCE"
110           environment variable.
111
112       guard
113           Returns a guard object.
114
115             use DBIx::QueryLog ();
116             {
117                 my $guard = DBIx::QueryLog->guard;
118                 # ... do something
119             }
120
121           The following code does the same:
122
123             use DBIx::QueryLog ();
124
125             DBIx::QueryLog->enable;
126             # ... do something
127             DBIx::QueryLog->disable;
128
129       ignore_trace
130           Returns a guard object and disables tracing while the object is
131           alive.
132
133             use DBIx::QueryLog;
134
135             # enabled
136             $dbh->do(...);
137
138             {
139                 my $guard = DBIx::QueryLog->ignore_trace;
140                 # disable
141                 $dbh->do(...);
142             }
143
144             # enabled
145             $dbh->do(...)
146
147       is_enabled
148           Returns if DBIx::QueryLog is enabled or not.
149
150             use DBIx::QueryLog ();
151
152             say DBIx::QueryLog->is_enabled;
153
154             DBIx::QueryLog->disable;
155
156           See also Localization section.
157

TIPS

159   Localization
160       If you want to log only in a specific scope:
161
162         use DBIx::QueryLog (); # or require DBIx::QueryLog;
163
164         DBIx::QueryLog->begin; # or DBIx::QueryLog->enable
165         my $row = $dbh->do(...);
166         DBIx::QueryLog->end;   # or DBIx::QueryLog->disable
167
168       DBIx::QueryLog logs only between "begin" and "end".
169
170   LOG_LEVEL
171       When you set a "logger", you might also want to change a log level.
172
173         $DBIx::QueryLog::LOG_LEVEL = 'info'; # default 'debug'
174
175   OUTPUT
176       If you want to change where to output:
177
178         open my $fh, '>', 'dbix_query.log';
179         $DBIx::QueryLog::OUTPUT = $fh;
180
181       You can also specify a code reference:
182
183         $DBIx::QueryLog::OUTPUT = sub {
184             my %params = @_;
185
186             my $format = << 'FORMAT';
187         localtime  : %s       # ISO-8601 without timezone
188         level      : %s       # log level ($DBIx::QueryLog::LOG_LEVEL)
189         time       : %f       # elasped time
190         data_source: $s       # data_source
191         sql        : %s       # executed query
192         bind_params: %s       # bind parameters
193         pkg        : %s       # caller package
194         file       : %s       # caller file
195         line       : %d       # caller line
196         FORMAT
197
198             printf $format,
199                 @params{qw/localtime level pkg time data_source sql/},
200                 join(', ', @{$params{bind_params}}),
201                 @params{qw/file line/};
202
203             printf "AutoCommit?: %d\n", $params->{dbh}->{AutoCommit} ? 1 : 0;
204         };
205
206       You can also use this if you want to use a logger that doesn't have a
207       "log" method like the one of <Log::Dispatch>.
208
209         $DBIx::QueryLog::OUTPUT = sub {
210             my %params = @_;
211             my $logger = Log::Any->get_logger;
212             $logger->debug("$params{message}");
213         };
214
215       Note that this only works when "<logger"> is not set.
216
217       Default $OUTPUT is "STDERR".
218

AUTHOR

220       xaicron <xaicron {at} cpan.org>
221

THANKS TO

223       tokuhirom
224
225       yibe
226
227       kamipo
228
229       tomi-ru
230
231       riywo
232
233       makamaka
234

BUG REPORTING

236       Plese use github issues:
237       <https://github.com/xaicron/p5-DBIx-QueryLog/issues>.
238
240       Copyright 2010 - xaicron
241

LICENSE

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

SEE ALSO

247       DBI
248
249
250
251perl v5.36.0                      2022-07-22               DBIx::QueryLog(3pm)
Impressum