Log-Log4perl-Appender-Raven

 view release on metacpan or  search on metacpan

lib/Log/Log4perl/Appender/Raven.pm  view on Meta::CPAN

package Log::Log4perl::Appender::Raven;
$Log::Log4perl::Appender::Raven::VERSION = '0.006';
use Moose;

use Carp;
use Data::Dumper;
use Digest::MD5;
use Sentry::Raven;
use Log::Log4perl;
use Devel::StackTrace;
use Safe;
use Scope::Guard;
use Text::Template;

## Configuration
has 'sentry_dsn' => ( is => 'ro', isa => 'Maybe[Str]' );
has 'sentry_timeout' => ( is => 'ro' , isa => 'Int' ,required => 1 , default => 1 );
has 'sentry_culprit_template' => ( is => 'ro', isa => 'Str', required => 1 , default => '{$function}');
has 'infect_die' => ( is => 'ro' , isa => 'Bool', default => 0 );
# STATIC CONTEXT
has 'context' => ( is => 'ro' , isa => 'HashRef', default => sub{ {}; });
# STATIC TAGS. They will go in the global context.
has 'tags' => ( is => 'ro' ,isa => 'HashRef', default => sub{ {}; });
# Log4Perl MDC key to look for tags
has 'mdc_tags' => ( is => 'ro' , isa => 'Maybe[Str]' , default => 'sentry_tags' );
# Log4perl MDC key to look for extra
has 'mdc_extra' => ( is => 'ro', isa => 'Maybe[Str]' , default => 'sentry_extra' );
# Log4perl MDC key to look for user data.
has 'mdc_user'  => ( is => 'ro' ,isa => 'Maybe[Str]' , default => 'sentry_user' );
# Log4perl MDC key to look for http data.
has 'mdc_http' => ( is => 'ro' , isa => 'Maybe[Str]' , default => 'sentry_http' );

## End of configuration

# Operation objects
has 'raven' => ( is => 'ro', isa => 'Sentry::Raven', lazy_build => 1);
has 'culprit_text_template' => ( is => 'ro', isa => 'Text::Template' , lazy_build => 1);
has 'safe' => ( is => 'ro' , isa => 'Safe', lazy_build => 1);


my %L4P2SENTRY = ('ALL' => 'info',
                  'TRACE' => 'debug',
                  'DEBUG' => 'debug',
                  'INFO' => 'info',
                  'WARN' => 'warning',
                  'ERROR' => 'error',
                  'FATAL' => 'fatal');

sub BUILD{
    my ($self) = @_;
    if( $self->infect_die() ){
        warn q|INFECTING SIG __DIE__ with Log4perl trickery. Ideally you should not count on that.

See perldoc Log::Log4perl::Appender::Raven, section 'CODE WIHTOUT LOG4PERL'

|;

        # Infect die. This is based on http://log4perl.sourceforge.net/releases/Log-Log4perl/docs/html/Log/Log4perl/FAQ.html#73200
        $SIG{__DIE__} = sub{

            ## Are we called from within log4perl at all.
            {
                my $frame_up = 0;
                while( my @caller = caller($frame_up++) ){
                    if( $caller[0] =~ /^Log::Log4perl/ ){
                        return;
                    }
                }
            }


            ## warn "CALLING die Handler";
            my $method = 'fatal';

            my $level_up = 1;

            # In an eval, nothing is fatal:
            if( $^S ){
                $method = 'error';
            }

            my ($package, $filename, $line,
                $subroutine, @discard )  = caller(0);



( run in 1.124 second using v1.01-cache-2.11-cpan-ceb78f64989 )