X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Flog.pm;h=1d4df730a541f180b05e2f56f8de00b329e4bb0a;hb=88e9a56677d343392416c262f976f069157b06cb;hp=a4ad214d020fe23099ff0c5436c9567e8135ed7a;hpb=431c9ca4fab151862bd24322bf8a1f9252fb38fc;p=freeside.git diff --git a/FS/FS/log.pm b/FS/FS/log.pm index a4ad214d0..1d4df730a 100644 --- a/FS/FS/log.pm +++ b/FS/FS/log.pm @@ -5,6 +5,7 @@ use base qw( FS::Record ); use FS::Record qw( qsearch qsearchs dbdef ); use FS::UID qw( dbh driver_name ); use FS::log_context; +use FS::log_email; =head1 NAME @@ -71,6 +72,8 @@ otherwise returns false. CONTEXT may be a list of context tags to attach to this record. +Will send emails according to the conditions in L. + =cut sub insert { @@ -78,18 +81,51 @@ sub insert { my $self = shift; my $error = $self->SUPER::insert; return $error if $error; - foreach ( @_ ) { + my $contexts = {}; # for quick checks when sending emails + my $context_height = @_; # also for email check + foreach ( @_ ) { # ordered from least to most specific my $context = FS::log_context->new({ 'lognum' => $self->lognum, 'context' => $_ }); $error = $context->insert; return $error if $error; + $contexts->{$_} = $context_height--; + } + foreach my $log_email ( + qsearch('log_email', + { + 'disabled' => '', + 'min_level' => { + 'op' => '<=', + 'value' => $self->level, + }, + } + ) + ) { + # shouldn't be a lot of log_email records, so not packing these checks into the qsearch + next if $log_email->context && !$contexts->{$log_email->context}; + next if $log_email->context_height && ($contexts->{$log_email->context} > $log_email->context_height); + my $msg_template = qsearchs('msg_template',{ 'msgnum' => $log_email->msgnum }); + unless ($msg_template) { + warn "Could not send email when logging, could not load message template for logemailnum " . $log_email->logemailnum; + next; + } + my $emailerror = $msg_template->send( + 'msgtype' => 'admin', + 'to' => $log_email->to_addr, + 'substitutions' => { + 'loglevel' => $FS::Log::LEVELS[$self->level], # which has hopefully been loaded... + 'logcontext' => $log_email->context, # use the one that triggered the email + 'logmessage' => $self->message, + }, + ); + warn "Could not send email when logging: $emailerror" if $emailerror; } ''; } -# the insert method can be inherited from FS::Record +# these methods can be inherited from FS::Record sub delete { die "Log entries can't be modified." }; @@ -114,7 +150,7 @@ sub check { || $self->ut_textn('tablename') || $self->ut_numbern('tablenum') || $self->ut_number('level') - || $self->ut_text('message') + || $self->ut_anything('message') ; return $error if $error; @@ -312,9 +348,16 @@ sub search { if ( $params->{'context'} ) { my $quoted = dbh->quote($params->{'context'}); - push @where, - "EXISTS(SELECT 1 FROM log_context WHERE log.lognum = log_context.lognum ". - "AND log_context.context = $quoted)"; + if ( $params->{'context_height'} =~ /^\d+$/ ) { + my $subq = 'SELECT context FROM log_context WHERE log.lognum = log_context.lognum'. + ' ORDER BY logcontextnum DESC LIMIT '.$params->{'context_height'}; + push @where, + "EXISTS(SELECT 1 FROM ($subq) AS log_context_x WHERE log_context_x.context = $quoted)"; + } else { + push @where, + "EXISTS(SELECT 1 FROM log_context WHERE log.lognum = log_context.lognum ". + "AND log_context.context = $quoted)"; + } } # agent virtualization