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