3 #package to deal with Logging Actions in DB
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2011 MJ Ray and software.coop
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
27 use Data::Dumper qw( Dumper );
28 use JSON qw( to_json );
29 use Scalar::Util qw( blessed );
30 use File::Basename qw( basename );
36 use vars qw(@ISA @EXPORT);
41 @EXPORT = qw(logaction cronlogaction);
46 C4::Log - Koha Log Facility functions
54 The functions in this module perform various functions in order to log all the operations done on the Database, including deleting and undeleting books, adding/editing members, etc.
62 &logaction($modulename, $actionname, $objectnumber, $infos, $interface);
64 Adds a record into action_logs table to report the different changes upon the database.
65 Each log entry includes the number of the user currently logged in. For batch
66 jobs, which operate without authenticating a user and setting up a session, the user
67 number is set to 0, which is the same as the superlibrarian's number.
73 my ($modulename, $actionname, $objectnumber, $infos, $interface)=@_;
75 # Get ID of logged in user. if called from a batch job,
76 # no user session exists and C4::Context->userenv() returns
78 my $userenv = C4::Context->userenv();
79 my $usernumber = (ref($userenv) eq 'HASH') ? $userenv->{'number'} : 0;
81 $interface //= C4::Context->interface;
83 if( blessed($infos) && $infos->isa('Koha::Object') ) {
84 $infos = $infos->get_from_storage if $infos->in_storage;
85 local $Data::Dumper::Sortkeys = 1;
87 if ( $infos->isa('Koha::Item') && $modulename eq 'CATALOGUING' && $actionname eq 'MODIFY' ) {
88 $infos = "item " . Dumper( $infos->unblessed );
90 $infos = Dumper( $infos->unblessed );
94 my $script = ( $interface eq 'cron' or $interface eq 'commandline' )
99 my $depth = C4::Context->preference('ActionLogsTraceDepth') || 0;
100 for ( my $i = 0 ; $i < $depth ; $i++ ) {
101 my ( $package, $filename, $line, $subroutine ) = caller($i);
102 last unless defined $line;
107 filename => $filename,
109 subroutine => $subroutine,
113 my $trace = @trace ? to_json( \@trace, { utf8 => 1, pretty => 0 } ) : undef;
115 Koha::ActionLog->new(
117 timestamp => \'NOW()',
119 module => $modulename,
120 action => $actionname,
121 object => $objectnumber,
123 interface => $interface,
129 my $logger = Koha::Logger->get(
131 interface => $interface,
132 category => "ActionLogs.$modulename.$actionname"
137 "ACTION LOG: " . to_json(
140 module => $modulename,
141 action => $actionname,
142 object => $objectnumber,
152 &cronlogaction($infos);
154 Convenience routine to add a record into action_logs table from a cron job.
155 Logs the path and name of the calling script plus the information privided by param $infos.
162 my $info = $params->{info};
163 my $action = $params->{action};
165 my $loginfo = (caller(0))[1];
166 $loginfo .= ' ' . $info if $info;
167 logaction( 'CRONJOBS', $action, $$, $loginfo ) if C4::Context->preference('CronjobLog');
177 Koha Development Team <http://koha-community.org/>