4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs dbdef );
6 use FS::UID qw( dbh driver_name );
10 use FS::upgrade_journal;
15 FS::log - Object methods for log records
21 $record = new FS::log \%hash;
22 $record = new FS::log { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
34 An FS::log object represents a log entry. FS::log inherits from
35 FS::Record. The following fields are currently supported:
39 =item lognum - primary key
41 =item _date - Unix timestamp
43 =item agentnum - L<FS::agent> to which the log pertains. If it involves a
44 specific customer, package, service, invoice, or other agent-specific object,
45 this will be set to that agentnum.
47 =item tablename - table name to which the log pertains, if any.
49 =item tablenum - foreign key to that table.
51 =item level - log level: 'debug', 'info', 'notice', 'warning', 'error',
52 'critical', 'alert', 'emergency'.
54 =item message - contents of the log entry
64 Creates a new log entry. Use FS::Log instead of calling this directly,
71 =item insert [ CONTEXT... ]
73 Adds this record to the database. If there is an error, returns the error,
74 otherwise returns false.
76 CONTEXT may be a list of context tags to attach to this record.
78 Will send emails according to the conditions in L<FS::log_email>.
83 # not using process_o2m for this, because we don't have a web interface
86 my $error = $self->SUPER::insert;
87 return $error if $error;
90 my $context_height = @_;
91 foreach ( @_ ) { # ordered from least to most specific
92 my $context = FS::log_context->new({
93 'lognum' => $self->lognum,
96 $error = $context->insert;
97 return $error if $error;
98 $contexts->{$_} = $context_height--;
101 foreach my $log_email (
107 'value' => $self->level,
112 # shouldn't be a lot of log_email records, so not packing these checks into the qsearch
113 next if $log_email->context && !$contexts->{$log_email->context};
114 next if $log_email->context_height && ($contexts->{$log_email->context} > $log_email->context_height);
115 my $msg_template = qsearchs('msg_template',{ 'msgnum' => $log_email->msgnum });
116 unless ($msg_template) {
117 warn "Could not send email when logging, could not load message template for logemailnum " . $log_email->logemailnum;
120 my $emailerror = $msg_template->send(
121 'msgtype' => 'admin',
122 'to' => $log_email->to_addr,
124 'loglevel' => $FS::Log::LEVELS{$self->level} || 'unknown',
125 'logcontext' => join(', ', keys( %$contexts )) || 'unknown',
126 'logmessage' => $self->message,
129 warn "Could not send email when logging: $emailerror" if $emailerror;
134 # these methods can be inherited from FS::Record
136 sub delete { die "Log entries can't be modified." };
138 sub replace { die "Log entries can't be modified." };
142 Checks all fields to make sure this is a valid example. If there is
143 an error, returns the error, otherwise returns false. Called by the insert
152 $self->ut_numbern('lognum')
153 || $self->ut_number('_date')
154 || $self->ut_numbern('agentnum')
155 || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
156 || $self->ut_textn('tablename')
157 || $self->ut_numbern('tablenum')
158 || $self->ut_number('level')
159 || $self->ut_anything('message')
161 return $error if $error;
163 if ( my $tablename = $self->tablename ) {
164 my $dbdef_table = dbdef->table($tablename)
165 or return "tablename '$tablename' does not exist";
166 $error = $self->ut_foreign_key('tablenum',
168 $dbdef_table->primary_key);
169 return $error if $error;
177 Returns the context for this log entry, as an array, from least to most
184 map { $_->context } qsearch({
185 table => 'log_context',
186 hashref => { lognum => $self->lognum },
187 order_by => 'ORDER BY logcontextnum ASC',
199 Returns a qsearch hash expression to search for parameters specified in
200 HASHREF. Valid parameters are:
206 =item date - arrayref of start and end date
208 =item level - either a specific level, or an arrayref of min and max level
210 =item context - a context string that the log entry must have. This may
211 change in the future to allow searching for combinations of context strings.
213 =item object - any database object, to find log entries related to it.
215 =item tablename, tablenum - alternate way of specifying 'object'.
217 =item custnum - a customer number, to find log entries related to the customer
218 or any of their subordinate objects (invoices, packages, etc.).
220 =item message - a text string to search in messages. The search will be
221 a case-insensitive LIKE with % appended at both ends.
227 # used for custnum search: all tables with custnums
230 sub _setup_table_stubs {
256 my $pkey = dbdef->table($table)->primary_key;
258 "log.tablename = '$table' AND ".
259 "EXISTS(SELECT 1 FROM $table WHERE log.tablenum = $table.$pkey AND ".
260 "$table.custnum = "; # needs a closing )
264 "(log.tablename LIKE 'svc_%' OR log.tablename = 'cust_svc') AND ".
265 "EXISTS(SELECT 1 FROM cust_svc JOIN cust_pkg USING (svcnum) WHERE ".
266 "cust_pkg.custnum = "; # needs a closing )
270 my ($class, $params) = @_;
277 if ( $params->{'agentnum'} =~ /^(\d+)$/ ) {
286 if ( $params->{'custnum'} =~ /^(\d+)$/ ) {
287 _setup_table_stubs() unless @table_stubs;
289 my @orwhere = map { "( $_ $custnum) )" } @table_stubs;
290 push @where, join(' OR ', @orwhere);
297 if ( ref $params->{'level'} eq 'ARRAY' ) {
298 my ($min, $max) = @{ $params->{'level'} };
299 if ( $min =~ /^\d+$/ ) {
300 push @where, "log.level >= $min";
302 if ( $max =~ /^\d+$/ ) {
303 push @where, "log.level <= $max";
305 } elsif ( $params->{'level'} =~ /^(\d+)$/ ) {
306 push @where, "log.level = $1";
313 if ( ref $params->{'date'} eq 'ARRAY' ) {
314 my ($beg, $end) = @{ $params->{'date'} };
315 if ( $beg =~ /^\d+$/ ) {
316 push @where, "log._date >= $beg";
318 if ( $end =~ /^\d+$/ ) {
319 push @where, "log._date <= $end";
327 if ( $params->{'object'} and $params->{'object'}->isa('FS::Record') ) {
328 my $table = $params->{'object'}->table;
329 my $pkey = dbdef->table($table)->primary_key;
330 my $tablenum = $params->{'object'}->get($pkey);
331 if ( $table and $tablenum ) {
332 push @where, "log.tablename = '$table'", "log.tablenum = $tablenum";
334 } elsif ( $params->{'tablename'} =~ /^(\w+)$/ ) {
336 if ( $params->{'tablenum'} =~ /^(\d+)$/ ) {
337 push @where, "log.tablename = '$table'", "log.tablenum = $1";
345 if ( $params->{'message'} ) { # can be anything, really, so escape it
346 my $quoted_message = dbh->quote('%' . $params->{'message'} . '%');
347 my $op = (driver_name eq 'Pg' ? 'ILIKE' : 'LIKE');
348 push @where, "log.message $op $quoted_message";
355 if ( $params->{'context'} ) {
356 my $quoted = dbh->quote($params->{'context'});
357 if ( $params->{'context_height'} =~ /^\d+$/ ) {
358 my $subq = 'SELECT context FROM log_context WHERE log.lognum = log_context.lognum'.
359 ' ORDER BY logcontextnum DESC LIMIT '.$params->{'context_height'};
361 "EXISTS(SELECT 1 FROM ($subq) AS log_context_x WHERE log_context_x.context = $quoted)";
364 "EXISTS(SELECT 1 FROM log_context WHERE log.lognum = log_context.lognum ".
365 "AND log_context.context = $quoted)";
369 # agent virtualization
370 my $access_user = $FS::CurrentUser::CurrentUser;
371 push @where, $access_user->agentnums_sql(
373 viewall_right => 'Configuration',
379 $extra_sql .= 'WHERE ' . join(' AND ', @where) if @where;
380 my $count_query = 'SELECT COUNT(*) FROM log '.$extra_sql;
385 'extra_sql' => $extra_sql,
386 'count_query' => $count_query,
387 'order_by' => 'ORDER BY _date ASC',
388 #addl_from, not needed
393 my ($class, %opts) = @_;
395 return if FS::upgrade_journal->is_done('log__remap_levels');
397 tie my %levelmap, 'Tie::IxHash',
398 2 => 1, #notice -> info
399 6 => 5, #alert -> critical
400 7 => 5, #emergency -> critical
403 # this method should never autocommit
404 # should have been set in upgrade, but just in case...
405 local $FS::UID::AutoCommit = 0;
407 # in practice, only debug/info/warning/error appear to have been used,
408 # so this probably won't do anything, but just in case
409 foreach my $old (keys %levelmap) {
410 # FS::log has no replace method
411 my $sql = 'UPDATE log SET level=' . dbh->quote($levelmap{$old}) . ' WHERE level=' . dbh->quote($old);
412 warn $sql unless $opts{'quiet'};
413 my $sth = dbh->prepare($sql) or die dbh->errstr;
414 $sth->execute() or die $sth->errstr;
418 foreach my $log_email (
419 qsearch('log_email',{ 'min_level' => 2 }),
420 qsearch('log_email',{ 'min_level' => 6 }),
421 qsearch('log_email',{ 'min_level' => 7 }),
423 $log_email->min_level($levelmap{$log_email->min_level});
424 my $error = $log_email->replace;
431 FS::upgrade_journal->set_done('log__remap_levels');
441 L<FS::Record>, schema.html from the base documentation.