Friday, July 20, 2007

Catalyst debug trick

well, we always meet this when we develop a Catalyst application:
* we want -Debug and StackTrace or DBIC::Schema::Profiler when we develop the Catalyst App.
* we don't want -Debug like in the production server because it costs much.

Sometimes u may want a config name like debug_mode to control whether we want or not.
Here comes my solution:
__PACKAGE__->setup();

__PACKAGE__->log->levels('error', 'fatal'); # for real server
if( __PACKAGE__->config->{debug_mode} ) {

__PACKAGE__->log->enable('debug', 'info', 'warn'); # for developer server
{
# these code are copied from Catalyst.pm setup_log
no strict 'refs';
my $class = __PACKAGE__;
*{"$class\::debug"} = sub { 1 };
}

my @extra_plugins = qw/ StackTrace DBIC::Schema::Profiler /;
__PACKAGE__->setup_plugins( [ @extra_plugins ] );
}
Full code please check Foorum.pm

now u can set debug_mode: 1 in _local.yml when u develop and forget it in production server.

Explanation:
* why we write code after __PACKAGE__->setup(); is because we want __PACKAGE__->config->{debug_mode}. config is setup in ->setup().
* *{"$class\::debug"} = sub { 1 }; means $c->debug is on. code from Catalyst.pm
* __PACKAGE__->setup_plugins( [ @extra_plugins ] ); will setup extra_plugins whenever u want.

@Enjoy;

Labels: ,

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home