Skip to content

Instantly share code, notes, and snippets.

@jberger
Created February 25, 2016 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/09e35415480c0795d121 to your computer and use it in GitHub Desktop.
Save jberger/09e35415480c0795d121 to your computer and use it in GitHub Desktop.
Log a stack trace on an error
package Mojolicious::Plugin::LogTrace;
use Mojo::Base 'Mojolicious::Plugin';
use Scalar::Util 'blessed';
sub register {
my ($plugin, $app, $config) = @_;
my $log = $config->{log} || $app->log;
$log->on(message => \&_trace);
}
sub _trace {
my ($log, $level, $e) = @_;
return unless $level eq 'error';
return unless $e && blessed($e) && $e->isa('Mojo::Exception');
$log->append(sprintf "[trace] %s at %s:%s\n", @{$_}[3,1,2]) for @{$e->frames};
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment