From c5a8d074886c700138c03dd7cedbb7ea3b7885b2 Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 2 Feb 2023 03:13:24 +0000 Subject: [PATCH] Bug 32791: Add Debian tool for adding missing config to log4perl files This change adds a tool for adding missing config to log4perl files for individual Koha instances created using Debian packages Test plan: 0. Apply patch 1. Comment out a component in /etc/koha/sites/kohadev/log4perl.conf or delete some of the configuration 2. Run the following: ./debian/scripts/koha-log4perl kohadev 3. Note warning saying config has been updated 4. Review config to see that it was updated correctly 5. Run the following: ./debian/scripts/koha-log4perl kohadev 6. Note that no config changes are made --- debian/scripts/koha-log4perl | 182 +++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100755 debian/scripts/koha-log4perl diff --git a/debian/scripts/koha-log4perl b/debian/scripts/koha-log4perl new file mode 100755 index 0000000000..c32c3ba5d6 --- /dev/null +++ b/debian/scripts/koha-log4perl @@ -0,0 +1,182 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Getopt::Long; +use Modern::Perl; + +use Log::Log4perl; +use Log::Log4perl::Config::PropertyConfigurator; + +use File::Slurp qw(append_file); + +Getopt::Long::Configure("bundling"); + +my %opts; +my $res = GetOptions( \%opts, "help|h", ); + +if ( !$res || $opts{help} ) { + show_help( !$res ); + exit( !$res ); +} + +if ( !@ARGV ) { + show_help( 1, "An instance name must be supplied." ); + exit(1); +} +my $instance = shift @ARGV; +if ( !-e "/etc/koha/sites/$instance" ) { + show_help( 1, "The instance doesn't exist: $instance" ); + exit(1); +} + +#NOTE: It would be nice to put the below code in a module, but then +#this script would need to know about PERL5LIB paths... + +#NOTE: Add new components here and in the get_snippet() function +my @components = ( + 'z3950', + 'api', + 'sip', + 'plack-opac', + 'plack-api', + 'plack-intranet', +); + +validate_log4perl({ + instance => $instance, + components => \@components, +}); + +sub get_snippet { + my ($args) = @_; + my $instance = $args->{instance}; + my $component = $args->{component}; + my %snippets = (); +$snippets{z3950} = qq# +log4perl.logger.z3950 = WARN, Z3950 +log4perl.appender.Z3950=Log::Log4perl::Appender::File +log4perl.appender.Z3950.filename=/var/log/koha/$instance/z3950-error.log +log4perl.appender.Z3950.mode=append +log4perl.appender.Z3950.layout=PatternLayout +log4perl.appender.Z3950.layout.ConversionPattern=[%d] [%p] %m %l%n +log4perl.appender.Z3950.utf8=1 +#; +$snippets{api} = qq# +log4perl.logger.api = WARN, API +log4perl.appender.API=Log::Log4perl::Appender::File +log4perl.appender.API.filename=/var/log/koha/$instance/api-error.log +log4perl.appender.API.mode=append +log4perl.appender.API.layout=PatternLayout +log4perl.appender.API.layout.ConversionPattern=[%d] [%p] %m %l%n +log4perl.appender.API.utf8=1 +#; +$snippets{sip} = qq# +log4perl.logger.sip = DEBUG, SIP +log4perl.appender.SIP=Log::Log4perl::Appender::File +log4perl.appender.SIP.filename=/var/log/koha/$instance/sip.log +log4perl.appender.SIP.mode=append +log4perl.appender.SIP.layout=PatternLayout +log4perl.appender.SIP.layout.ConversionPattern=[%d] [%P] [%p] %X{accountid}@%X{peeraddr}: %m %l%n +log4perl.appender.SIP.utf8=1 +#; +$snippets{'plack-opac'} = qq# +log4perl.logger.plack-opac = WARN, PLACKOPAC +log4perl.appender.PLACKOPAC=Log::Log4perl::Appender::File +log4perl.appender.PLACKOPAC.filename=/var/log/koha/$instance/plack-opac-error.log +log4perl.appender.PLACKOPAC.mode=append +log4perl.appender.PLACKOPAC.layout=PatternLayout +log4perl.appender.PLACKOPAC.layout.ConversionPattern=[%d] [%p] %m%n +log4perl.appender.PLACKOPAC.utf8=1 +#; +$snippets{'plack-api'} = qq# +log4perl.logger.plack-api = WARN, PLACKAPI +log4perl.appender.PLACKAPI=Log::Log4perl::Appender::File +log4perl.appender.PLACKAPI.filename=/var/log/koha/$instance/plack-api-error.log +log4perl.appender.PLACKAPI.mode=append +log4perl.appender.PLACKAPI.layout=PatternLayout +log4perl.appender.PLACKAPI.layout.ConversionPattern=[%d] [%p] %m%n +log4perl.appender.PLACKAPI.utf8=1 +#; +$snippets{'plack-intranet'} = qq# +log4perl.logger.plack-intranet = WARN, PLACKINTRANET +log4perl.appender.PLACKINTRANET=Log::Log4perl::Appender::File +log4perl.appender.PLACKINTRANET.filename=/var/log/koha/$instance/plack-intranet-error.log +log4perl.appender.PLACKINTRANET.mode=append +log4perl.appender.PLACKINTRANET.layout=PatternLayout +log4perl.appender.PLACKINTRANET.layout.ConversionPattern=[%d] [%p] %m%n +log4perl.appender.PLACKINTRANET.utf8=1 +#; + return $snippets{$component}; +} + +sub validate_log4perl { + my ($args) = @_; + my $instance = $args->{instance}; + my $components = $args->{components}; + my $filename = sprintf('/etc/koha/sites/%s/log4perl.conf',$instance); + if ( -f $filename ){ + my $conf = Log::Log4perl::Config::PropertyConfigurator->new(); + $conf->file($filename); + $conf->parse(); # will die() on error + my $update = ''; + foreach my $component (@$components){ + #NOTE: If a component is commented out in config, it won't return a value here + my $value = $conf->value("log4perl.logger.$component"); + if ( ! $value){ + warn "Missing component $component\n"; + my $snippet = get_snippet({ + instance => $instance, + component => $component, + }); + if ($snippet){ + $update .= $snippet; + } + } + } + if ($update){ + my $res = append_file($filename,$update); + if ($res){ + warn "Updated file $filename\n"; + } + else { + warn "Failed to update file $filename\n"; + } + } + } + else { + warn "$filename is not an accessible file\n"; + exit 1; + } +} + +#FIXME: The following stolen from koha-shell +sub show_help { + my ( $err, $msg ) = @_; + + my $fh = $err ? *STDERR : *STDOUT; + say $fh "Error: " . $msg if $msg; + print $fh $_ while ; +} + +__DATA__ +koha-log4perl -- tool for working with log4perl for a Koha instance + +Usage: koha-log4perl [options] [instance name] + +Options: + -h, --help show this help and quit + -- 2.30.2