Bug 28692: Get from storage before log actions
[srvgit] / C4 / Log.pm
1 package C4::Log;
2
3 #package to deal with Logging Actions in DB
4
5
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2011 MJ Ray and software.coop
8 #
9 # This file is part of Koha.
10 #
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.
15 #
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.
20 #
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>.
23
24 use strict;
25 use warnings;
26
27 use JSON qw( to_json );
28
29 use C4::Context;
30 use Koha::Logger;
31
32 use vars qw(@ISA @EXPORT);
33
34 BEGIN {
35         require Exporter;
36         @ISA = qw(Exporter);
37         @EXPORT = qw(logaction cronlogaction);
38 }
39
40 =head1 NAME
41
42 C4::Log - Koha Log Facility functions
43
44 =head1 SYNOPSIS
45
46   use C4::Log;
47
48 =head1 DESCRIPTION
49
50 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.
51
52 =head1 FUNCTIONS
53
54 =over 2
55
56 =item logaction
57
58   &logaction($modulename, $actionname, $objectnumber, $infos, $interface);
59
60 Adds a record into action_logs table to report the different changes upon the database.
61 Each log entry includes the number of the user currently logged in.  For batch
62 jobs, which operate without authenticating a user and setting up a session, the user
63 number is set to 0, which is the same as the superlibrarian's number.
64
65 =cut
66
67 #'
68 sub logaction {
69     my ($modulename, $actionname, $objectnumber, $infos, $interface)=@_;
70
71     # Get ID of logged in user.  if called from a batch job,
72     # no user session exists and C4::Context->userenv() returns
73     # the scalar '0'.
74     my $userenv = C4::Context->userenv();
75     my $usernumber = (ref($userenv) eq 'HASH') ? $userenv->{'number'} : 0;
76     $usernumber ||= 0;
77     $interface //= C4::Context->interface;
78
79     if ( ref($infos) && ref($infos) !~ /HASH|ARRAY/ && $infos->isa('Koha::Object') ) {
80         $infos = $infos->get_from_storage if $infos->in_storage;
81         $infos = Dumper( $infos->unblessed );
82
83         if ( $infos->isa('Koha::Item') && $modulename eq 'CATALOGUING' && $actionname eq 'MODIFY' ) {
84             $infos = "item " . $infos;
85         }
86     }
87
88     my $dbh = C4::Context->dbh;
89     my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info,interface) values (now(),?,?,?,?,?,?)");
90     $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos,$interface);
91     $sth->finish;
92
93     my $logger = Koha::Logger->get(
94         {
95             interface => $interface,
96             category  => "ActionLogs.$modulename.$actionname"
97         }
98     );
99     $logger->debug(
100         sub {
101             "ACTION LOG: " . to_json(
102                 {
103                     user   => $usernumber,
104                     module => $modulename,
105                     action => $actionname,
106                     object => $objectnumber,
107                     info   => $infos
108                 }
109             );
110         }
111     );
112 }
113
114 =item cronlogaction
115
116   &cronlogaction($infos);
117
118 Convenience routine to add a record into action_logs table from a cron job.
119 Logs the path and name of the calling script plus the information privided by param $infos.
120
121 =cut
122
123 #'
124 sub cronlogaction {
125     my ($infos)=@_;
126     my $loginfo = (caller(0))[1];
127     $loginfo .= ' ' . $infos if $infos;
128     logaction( 'CRONJOBS', 'Run', undef, $loginfo ) if C4::Context->preference('CronjobLog');
129 }
130
131 1;
132 __END__
133
134 =back
135
136 =head1 AUTHOR
137
138 Koha Development Team <http://koha-community.org/>
139
140 =cut