Plack::Middleware::Debug::DBIC::QueryLog - Log DBIC Queries
Adds a debug panel and querylog object for logging DBIx::Class queries. Has support for Catalyst via a Catalyst::TraitFor::Model::DBIC::Schema::QueryLog compatible trait, Catalyst::TraitFor::Model::DBIC::Schema::QueryLog::AdoptPlack.
use Plack::Builder; my $app = ...; ## Build your Plack App builder { enable 'Debug::DBIC::QueryLog', querylog_args => {passthrough => 1}; $app; };
And in you Catalyst application, if you are also using Catalyst::TraitFor::Model::DBIC::Schema::QueryLog::AdoptPlack
package MyApp::Web::Model::Schema; use parent 'Catalyst::Model::DBIC::Schema'; __PACKAGE__->config({ schema_class => 'MyApp::Schema', traits => ['QueryLog::AdoptPlack'], ## .. rest of configuration }); 1;
DBIx::Class::QueryLog is a tool in the DBIx::Class software ecosystem which benchmarks queries. It lets you log the SQL that DBIx::Class is generating, along with bind variables and timestamps. You can then pass the querylog object to an analyzer (such as DBIx::Class::QueryLog::Analyzer) to generate sorted statistics for all the queries between certain log points.
Query logging in Catalyst is supported for DBIx::Class via a trait for Catalyst::Model::DBIC::Schema called Catalyst::TraitFor::Model::DBIC::Schema::QueryLog. This trait will log all the SQL used by DBIx::Class for a given request cycle. This is very useful since it can help you identify troublesome or bottlenecking queries.
However, Catalyst::TraitFor::Model::DBIC::Schema::QueryLog does not provide out of the box outputting of your analyzed query logs. Usually you need to add a bit of templating work to the bottom of your webpage footer, or dump the output to the logs. We'd like to provide a lower ceremony experience.
Additionally, it would be nice if we could provide this functionality for all Plack based applications, not just Catalyst. Ideally we'd play nice with Plack::Middleware::Debug so that the table of our querylog would appear as a neat Plack based Debug panel. This bit of middleware provides that function.
Basically we create a new instance of DBIx::Class::QueryLog and place it into $env->{'plack.middleware.debug.dbic.querylog'} so that it is accessible by all applications running inside of Plack. You need to 'tell' your application's instance of DBIx::Class to use this $env key and make sure you set DBIx::Class's debug object correctly:
$env->{'plack.middleware.debug.dbic.querylog'}
$env
my $querylog = $ctx->engine->env->{'plack.middleware.debug.dbic.querylog'}; $schema->storage->debugobj($querylog); $schema->storage->debug(1);
That way when you view the debug panel, we have SQL to review.
If you are using Catalyst and a modern Catalyst::Model::DBIC::Schema you can use the trait Catalyst::TraitFor::Model::DBIC::Schema::QueryLog::AdoptPlack, which is compatible with Catalyst::TraitFor::Model::DBIC::Schema::QueryLog. See the "SYNOPSIS" example for more details.
This debug panel defines the following options.
Takes a DBIx::Class::QueryLog object, which is used as the querylog for the application. If you don't provide this, we will build one automatically, using "querylog_args" if provided. Generally you will use this only if you are instantiating a querylog object outside your Plack based application, such as in an IOC container like Bread::Board.
Takes a HashRef which is passed to DBIx::Class::QueryLog at construction.
Plack::Middleware::Debug, Catalyst::TraitFor::Model::DBIC::Schema::QueryLog, Catalyst::Model::DBIC::Schema, Catalyst::TraitFor::Model::DBIC::Schema::QueryLog::AdoptPlack
John Napiorkowski, <jjnapiork@cpan.org>
<jjnapiork@cpan.org>
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Plack::Middleware::Debug::DBIC::QueryLog, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Plack::Middleware::Debug::DBIC::QueryLog
CPAN shell
perl -MCPAN -e shell install Plack::Middleware::Debug::DBIC::QueryLog
For more information on module installation, please visit the detailed CPAN module installation guide.