Created
February 25, 2016 20:11
-
-
Save jberger/09e35415480c0795d121 to your computer and use it in GitHub Desktop.
Log a stack trace on an error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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