Bug 30984: Action logs should log the cronjob script name that generated the given log

When something is changed by a cronjob, and that entity is logged via action logs, we can know what changed, and that it was via a cronjob, but we cannot necessarily know which cronjob made that change. The closest we can come is to find the action logs for the cronjob module which ran before the change which is by no means reliable assuming the CronLog is even enabled.

We should add a new column to action logs to store the name of the script ran for any action logs where the interface is "cron".

Test plan:
1) Apply this patch
2) Run updatedatabase.pl
3) Enable all the Log/Logging sysprefs
4) Run some cronjobs that will generate action logs
5) Note the new action_logs column "script" contains the filename of the
   cronjob that caused the change.

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
(cherry picked from commit 18fd0c89cf)

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Kyle Hall 2022-06-17 06:59:11 -04:00 committed by Lucas Gass
parent ecc6a21ad2
commit 5767add2cc
3 changed files with 26 additions and 2 deletions

View file

@ -27,6 +27,7 @@ use warnings;
use Data::Dumper qw( Dumper );
use JSON qw( to_json );
use Scalar::Util qw( blessed );
use File::Basename qw( basename );
use C4::Context;
use Koha::Logger;
@ -89,9 +90,14 @@ sub logaction {
}
}
my $script =
$interface eq 'cron' ? basename($0)
: $interface eq 'commandline' ? basename($0)
: undef;
my $dbh = C4::Context->dbh;
my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info,interface) values (now(),?,?,?,?,?,?)");
$sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos,$interface);
my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info,interface,script) values (now(),?,?,?,?,?,?,?)");
$sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos,$interface,$script);
$sth->finish;
my $logger = Koha::Logger->get(

View file

@ -0,0 +1,17 @@
use Modern::Perl;
return {
bug_number => "30984",
description => "Log the cron script that generated an action log if there is one",
up => sub {
my ($args) = @_;
my ($dbh, $out) = @$args{qw(dbh out)};
if( !column_exists( 'action_logs', 'script' ) ) {
$dbh->do(q{
ALTER TABLE action_logs
ADD COLUMN script mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'the name of the cron script that caused this change'
AFTER interface
});
}
},
};

View file

@ -175,6 +175,7 @@ CREATE TABLE `action_logs` (
`object` int(11) DEFAULT NULL COMMENT 'the object that the action was taken against (could be a borrowernumber, itemnumber, etc)',
`info` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'information about the action (usually includes SQL statement)',
`interface` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'the context this action was taken in',
`script` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'the name of the cron script that caused this change',
PRIMARY KEY (`action_id`),
KEY `timestamp_idx` (`timestamp`),
KEY `user_idx` (`user`),