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

(-)a/debian/scripts/koha-log4perl (-1 / +182 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Getopt::Long;
19
use Modern::Perl;
20
21
use Log::Log4perl;
22
use Log::Log4perl::Config::PropertyConfigurator;
23
24
use File::Slurp qw(append_file);
25
26
Getopt::Long::Configure("bundling");
27
28
my %opts;
29
my $res = GetOptions( \%opts, "help|h", );
30
31
if ( !$res || $opts{help} ) {
32
    show_help( !$res );
33
    exit( !$res );
34
}
35
36
if ( !@ARGV ) {
37
    show_help( 1, "An instance name must be supplied." );
38
    exit(1);
39
}
40
my $instance = shift @ARGV;
41
if ( !-e "/etc/koha/sites/$instance" ) {
42
    show_help( 1, "The instance doesn't exist: $instance" );
43
    exit(1);
44
}
45
46
#NOTE: It would be nice to put the below code in a module, but then
47
#this script would need to know about PERL5LIB paths...
48
49
#NOTE: Add new components here and in the get_snippet() function
50
my @components = (
51
    'z3950',
52
    'api',
53
    'sip',
54
    'plack-opac',
55
    'plack-api',
56
    'plack-intranet',
57
);
58
59
validate_log4perl({
60
    instance => $instance,
61
    components => \@components,
62
});
63
64
sub get_snippet {
65
    my ($args) = @_;
66
    my $instance = $args->{instance};
67
    my $component = $args->{component};
68
    my %snippets = ();
69
$snippets{z3950} = qq#
70
log4perl.logger.z3950 = WARN, Z3950
71
log4perl.appender.Z3950=Log::Log4perl::Appender::File
72
log4perl.appender.Z3950.filename=/var/log/koha/$instance/z3950-error.log
73
log4perl.appender.Z3950.mode=append
74
log4perl.appender.Z3950.layout=PatternLayout
75
log4perl.appender.Z3950.layout.ConversionPattern=[%d] [%p] %m %l%n
76
log4perl.appender.Z3950.utf8=1
77
#;
78
$snippets{api} = qq#
79
log4perl.logger.api = WARN, API
80
log4perl.appender.API=Log::Log4perl::Appender::File
81
log4perl.appender.API.filename=/var/log/koha/$instance/api-error.log
82
log4perl.appender.API.mode=append
83
log4perl.appender.API.layout=PatternLayout
84
log4perl.appender.API.layout.ConversionPattern=[%d] [%p] %m %l%n
85
log4perl.appender.API.utf8=1
86
#;
87
$snippets{sip} = qq#
88
log4perl.logger.sip = DEBUG, SIP
89
log4perl.appender.SIP=Log::Log4perl::Appender::File
90
log4perl.appender.SIP.filename=/var/log/koha/$instance/sip.log
91
log4perl.appender.SIP.mode=append
92
log4perl.appender.SIP.layout=PatternLayout
93
log4perl.appender.SIP.layout.ConversionPattern=[%d] [%P] [%p] %X{accountid}@%X{peeraddr}: %m %l%n
94
log4perl.appender.SIP.utf8=1
95
#;
96
$snippets{'plack-opac'} = qq#
97
log4perl.logger.plack-opac = WARN, PLACKOPAC
98
log4perl.appender.PLACKOPAC=Log::Log4perl::Appender::File
99
log4perl.appender.PLACKOPAC.filename=/var/log/koha/$instance/plack-opac-error.log
100
log4perl.appender.PLACKOPAC.mode=append
101
log4perl.appender.PLACKOPAC.layout=PatternLayout
102
log4perl.appender.PLACKOPAC.layout.ConversionPattern=[%d] [%p] %m%n
103
log4perl.appender.PLACKOPAC.utf8=1
104
#;
105
$snippets{'plack-api'} = qq#
106
log4perl.logger.plack-api = WARN, PLACKAPI
107
log4perl.appender.PLACKAPI=Log::Log4perl::Appender::File
108
log4perl.appender.PLACKAPI.filename=/var/log/koha/$instance/plack-api-error.log
109
log4perl.appender.PLACKAPI.mode=append
110
log4perl.appender.PLACKAPI.layout=PatternLayout
111
log4perl.appender.PLACKAPI.layout.ConversionPattern=[%d] [%p] %m%n
112
log4perl.appender.PLACKAPI.utf8=1
113
#;
114
$snippets{'plack-intranet'} = qq#
115
log4perl.logger.plack-intranet = WARN, PLACKINTRANET
116
log4perl.appender.PLACKINTRANET=Log::Log4perl::Appender::File
117
log4perl.appender.PLACKINTRANET.filename=/var/log/koha/$instance/plack-intranet-error.log
118
log4perl.appender.PLACKINTRANET.mode=append
119
log4perl.appender.PLACKINTRANET.layout=PatternLayout
120
log4perl.appender.PLACKINTRANET.layout.ConversionPattern=[%d] [%p] %m%n
121
log4perl.appender.PLACKINTRANET.utf8=1
122
#;
123
    return $snippets{$component};
124
}
125
126
sub validate_log4perl {
127
    my ($args) = @_;
128
    my $instance = $args->{instance};
129
    my $components = $args->{components};
130
    my $filename = sprintf('/etc/koha/sites/%s/log4perl.conf',$instance);
131
    if ( -f $filename ){
132
        my $conf = Log::Log4perl::Config::PropertyConfigurator->new();
133
        $conf->file($filename);
134
        $conf->parse(); # will die() on error
135
        my $update = '';
136
        foreach my $component (@$components){
137
            #NOTE: If a component is commented out in config, it won't return a value here
138
        	my $value = $conf->value("log4perl.logger.$component");
139
			if ( ! $value){
140
                warn "Missing component $component\n";
141
                my $snippet = get_snippet({
142
                    instance => $instance,
143
                    component => $component,
144
                });
145
                if ($snippet){
146
                    $update .= $snippet;
147
                }
148
			}
149
        }
150
        if ($update){
151
            my $res = append_file($filename,$update);
152
            if ($res){
153
                warn "Updated file $filename\n";
154
            }
155
            else {
156
                warn "Failed to update file $filename\n";
157
            }
158
        }
159
    }
160
    else {
161
        warn "$filename is not an accessible file\n";
162
        exit 1;
163
    }
164
}
165
166
#FIXME: The following stolen from koha-shell
167
sub show_help {
168
    my ( $err, $msg ) = @_;
169
170
    my $fh = $err ? *STDERR : *STDOUT;
171
    say $fh "Error: " . $msg if $msg;
172
    print $fh $_ while <DATA>;
173
}
174
175
__DATA__
176
koha-log4perl -- tool for working with log4perl for a Koha instance
177
178
Usage: koha-log4perl [options] [instance name]
179
180
Options:
181
    -h, --help              show this help and quit
182

Return to bug 32791