View | Details | Raw Unified | Return to bug 25159
Collapse All | Expand All

(-)a/C4/Log.pm (-7 / +16 lines)
Lines 25-33 use strict; Link Here
25
use warnings;
25
use warnings;
26
26
27
use Data::Dumper qw( Dumper );
27
use Data::Dumper qw( Dumper );
28
use JSON qw( to_json );
29
use Scalar::Util qw( blessed );
30
use File::Basename qw( basename );
28
use File::Basename qw( basename );
29
use JSON qw( to_json encode_json );
30
use Scalar::Util qw( blessed );
31
use Struct::Diff qw( diff );
31
32
32
use C4::Context;
33
use C4::Context;
33
use Koha::Logger;
34
use Koha::Logger;
Lines 59-65 The functions in this module perform various functions in order to log all the o Link Here
59
60
60
=item logaction
61
=item logaction
61
62
62
  &logaction($modulename, $actionname, $objectnumber, $infos, $interface);
63
  &logaction($modulename, $actionname, $objectnumber, $infos, $interface, $hashref_of_original);
63
64
64
Adds a record into action_logs table to report the different changes upon the database.
65
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
Each log entry includes the number of the user currently logged in.  For batch
Lines 70-76 number is set to 0, which is the same as the superlibrarian's number. Link Here
70
71
71
#'
72
#'
72
sub logaction {
73
sub logaction {
73
    my ($modulename, $actionname, $objectnumber, $infos, $interface)=@_;
74
    my ($modulename, $actionname, $objectnumber, $infos, $interface, $original )=@_;
75
76
    my $updated;
74
77
75
    # Get ID of logged in user.  if called from a batch job,
78
    # Get ID of logged in user.  if called from a batch job,
76
    # no user session exists and C4::Context->userenv() returns
79
    # no user session exists and C4::Context->userenv() returns
Lines 82-94 sub logaction { Link Here
82
85
83
    if( blessed($infos) && $infos->isa('Koha::Object') ) {
86
    if( blessed($infos) && $infos->isa('Koha::Object') ) {
84
        $infos = $infos->get_from_storage if $infos->in_storage;
87
        $infos = $infos->get_from_storage if $infos->in_storage;
88
        $updated = $infos->unblessed;
85
        local $Data::Dumper::Sortkeys = 1;
89
        local $Data::Dumper::Sortkeys = 1;
86
90
87
        if ( $infos->isa('Koha::Item') && $modulename eq 'CATALOGUING' && $actionname eq 'MODIFY' ) {
91
        if ( $infos->isa('Koha::Item') && $modulename eq 'CATALOGUING' && $actionname eq 'MODIFY' ) {
88
            $infos = "item " . Dumper( $infos->unblessed );
92
            $infos = "item " . Dumper( $updated );
89
        } else {
93
        } else {
90
            $infos = Dumper( $infos->unblessed );
94
            $infos = Dumper( $updated );
91
        }
95
        }
96
    } else {
97
        $updated = $infos;
92
    }
98
    }
93
99
94
    my $script = ( $interface eq 'cron' or $interface eq 'commandline' )
100
    my $script = ( $interface eq 'cron' or $interface eq 'commandline' )
Lines 112-117 sub logaction { Link Here
112
    }
118
    }
113
    my $trace = @trace ? to_json( \@trace, { utf8 => 1, pretty => 0 } ) : undef;
119
    my $trace = @trace ? to_json( \@trace, { utf8 => 1, pretty => 0 } ) : undef;
114
120
121
    my $diff = ( $original && ref $updated eq 'HASH' ) ? encode_json( diff( $original, $updated, noU => 1 ) ) : undef;
122
115
    Koha::ActionLog->new(
123
    Koha::ActionLog->new(
116
        {
124
        {
117
            timestamp => \'NOW()',
125
            timestamp => \'NOW()',
Lines 123-128 sub logaction { Link Here
123
            interface => $interface,
131
            interface => $interface,
124
            script    => $script,
132
            script    => $script,
125
            trace     => $trace,
133
            trace     => $trace,
134
            diff      => $diff,
126
        }
135
        }
127
    )->store();
136
    )->store();
128
137
Lines 134-140 sub logaction { Link Here
134
    );
143
    );
135
    $logger->debug(
144
    $logger->debug(
136
        sub {
145
        sub {
137
            "ACTION LOG: " . to_json(
146
            "ACTION LOG: " . encode_json(
138
                {
147
                {
139
                    user   => $usernumber,
148
                    user   => $usernumber,
140
                    module => $modulename,
149
                    module => $modulename,
(-)a/cpanfile (+1 lines)
Lines 96-101 requires 'POSIX', '1.09'; Link Here
96
requires 'Plack::Middleware::LogWarn', '0.001002';
96
requires 'Plack::Middleware::LogWarn', '0.001002';
97
requires 'Plack::Middleware::ReverseProxy', '0.14';
97
requires 'Plack::Middleware::ReverseProxy', '0.14';
98
requires 'Readonly', '2.00';
98
requires 'Readonly', '2.00';
99
requires 'Struct::Diff', '0.98';
99
requires 'Schedule::At', '1.06';
100
requires 'Schedule::At', '1.06';
100
requires 'Search::Elasticsearch', '6.80';
101
requires 'Search::Elasticsearch', '6.80';
101
requires 'Sereal::Decoder', '3.0';
102
requires 'Sereal::Decoder', '3.0';
(-)a/t/db_dependent/Log.t (-2 / +16 lines)
Lines 16-22 Link Here
16
16
17
use Modern::Perl;
17
use Modern::Perl;
18
use Data::Dumper qw( Dumper );
18
use Data::Dumper qw( Dumper );
19
use Test::More tests => 5;
19
use Test::More tests => 6;
20
20
21
use C4::Context;
21
use C4::Context;
22
use C4::Log qw( logaction cronlogaction );
22
use C4::Log qw( logaction cronlogaction );
Lines 27-32 use Koha::ActionLogs; Link Here
27
use t::lib::Mocks qw/mock_preference/; # to mock CronjobLog
27
use t::lib::Mocks qw/mock_preference/; # to mock CronjobLog
28
use t::lib::TestBuilder;
28
use t::lib::TestBuilder;
29
29
30
use JSON qw( decode_json );
31
30
# Make sure we can rollback.
32
# Make sure we can rollback.
31
our $schema  = Koha::Database->new->schema;
33
our $schema  = Koha::Database->new->schema;
32
$schema->storage->txn_begin;
34
$schema->storage->txn_begin;
Lines 194-197 subtest 'Reduce log size by unblessing Koha objects' => sub { Link Here
194
    like( $logs->next->info, qr/^t::lib::TestBuilder/, 'Dumped TestBuilder object' );
196
    like( $logs->next->info, qr/^t::lib::TestBuilder/, 'Dumped TestBuilder object' );
195
};
197
};
196
198
199
subtest 'Test storing diff of objects' => sub {
200
    plan tests => 1;
201
202
    my $builder = t::lib::TestBuilder->new;
203
    my $item = $builder->build_sample_item;
204
    my $original = $item->unblessed();
205
    $item->barcode('_MY_TEST_BARCODE_')->store();
206
    logaction( 'MY_MODULE', 'TEST01', $item->itemnumber, $item, 'opac', $original );
207
    my $logs = Koha::ActionLogs->search({ module => 'MY_MODULE', action => 'TEST01', object => $item->itemnumber })->next;
208
    my $diff = decode_json( $logs->diff );
209
    is( $diff->{D}->{barcode}->{N}, '_MY_TEST_BARCODE_', 'Diff of changes logged successfully' );
210
};
211
197
$schema->storage->txn_rollback;
212
$schema->storage->txn_rollback;
198
- 

Return to bug 25159