yahermann
yahermann

Reputation: 1579

logging die and warn in Perl Dancer modules

Looking to have die and warn logged to file log (in production mode) if said die/warn is within my own module. Works fine if die/warn occurs within ./lib/main.pm, but not if they occur within MyModule, in which case no logginc occurs. Any ideas?

The pattern I'm using is:

./lib/main.pm:

 package main;
 use Dancer ':syntax';
 use MyModule;
 ...
 warn 'this will get logged. hurrah!';
 my $result = &MyModule::MyMethod();

./MyModule.pm:

 package MyModule;

 ...

 sub MyMethod {
   warn 'this wont get logged for some reason. any idea why not?';
   return 'result';
 }

apache configuration:

<VirtualHost *:80>
ServerName myapp.com
DocumentRoot /home/myapp/public

SetEnv DANCER_ENVIRONMENT "production"

<Directory />
SetHandler perl-script
PerlResponseHandler Plack::Handler::Apache2
PerlSetVar psgi_app /home/myapp/bin/app.pl
</Directory>

CustomLog /home/myapp/logs/access.log common
ErrorLog /home/myapp/logs/error.log

</VirtualHost>

And production.yml:

log: "warning"
logger: "file"
log_path: "/home/myapp/logs/"
warnings: 0
show_errors: 0
route_cache: 1

Upvotes: 2

Views: 659

Answers (1)

yahermann
yahermann

Reputation: 1579

I figured this out a long time ago, figured I'd answer my own question.

  1. Use 'warning', not warn.

  2. Each module needs to use Dancer ':syntax';

So, this will work:

package MyModule;
use Dancer ':syntax';
...

sub MyMethod {
  warning 'this will get logged with good reason. yippee!';
  return 'result';
  }

Upvotes: 3

Related Questions