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