From 002fac9006168616d9c43a863722b007f473a0f8 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Mon, 17 Sep 2012 11:53:05 -0400 Subject: [PATCH] Bug 8190 - Followup - Add cached logger, output messages to template Content-Type: text/plain; charset="utf-8" * Add C4::Context->logger * Embed logged messageds to a comment in the template html, controlled by the system preference LogToHtmlComments\ * Add both new system preferences to sysprefs.sql --- C4/Auth.pm | 3 + C4/Context.pm | 28 +++++ Koha/Utils/Logger.pm | 108 ++++++++++++-------- installer/data/mysql/sysprefs.sql | 2 + installer/data/mysql/updatedatabase.pl | 5 +- .../prog/en/includes/doc-head-open.inc | 5 + .../prog/en/modules/admin/preferences/admin.pref | 7 ++ mainpage.pl | 2 + 8 files changed, 118 insertions(+), 42 deletions(-) diff --git a/C4/Auth.pm b/C4/Auth.pm index 59c9955..8b0a8a6 100644 --- a/C4/Auth.pm +++ b/C4/Auth.pm @@ -363,6 +363,8 @@ sub get_template_and_user { OPACLocalCoverImages => C4::Context->preference('OPACLocalCoverImages'), AllowMultipleCovers => C4::Context->preference('AllowMultipleCovers'), EnableBorrowerFiles => C4::Context->preference('EnableBorrowerFiles'), + LogToHtmlComments => C4::Context->preference('LogToHtmlComments'), + Logger => C4::Context->logger(), ); } else { @@ -461,6 +463,7 @@ sub get_template_and_user { $template->param(OpacPublic => '1') if ($user || C4::Context->preference("OpacPublic")); } + return ( $template, $borrowernumber, $cookie, $flags); } diff --git a/C4/Context.pm b/C4/Context.pm index 9ce6c74..4c556a3 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -20,6 +20,8 @@ use strict; use warnings; use vars qw($VERSION $AUTOLOAD $context @context_stack $servers $memcached $ismemcached); +use Koha::Utils::Logger; + BEGIN { if ($ENV{'HTTP_USER_AGENT'}) { require CGI::Carp; @@ -1130,6 +1132,32 @@ sub tz { return $context->{tz}; } +=head2 logger + + $logger = C4::Context->logger; + +Returns a Koha logger. If no logger has yet been instantiated, +this method creates one, and caches it. + +=cut + +sub logger +{ + my $self = shift; + my $sth; + + if ( defined( $context->{"logger"} ) ) { + return $context->{"logger"}; + } + + $context->{"logger"} = Koha::Utils::Logger->new( + { + level => C4::Context->preference("LogLevel") + } + ); + + return $context->{"logger"}; +} 1; diff --git a/Koha/Utils/Logger.pm b/Koha/Utils/Logger.pm index 37b24de..fea850f 100644 --- a/Koha/Utils/Logger.pm +++ b/Koha/Utils/Logger.pm @@ -48,32 +48,40 @@ our $log = undef; sub new { my ( $proto, $params ) = @_; - return $log if $log and ( not defined $params->{file} or not defined $log->{FILE_PATH} or $params->{file} eq $log->{FILE_PATH} ); - my $class = ref($proto) || $proto; - my $self = {}; + return $log + if $log + and ( not defined $params->{file} + or not defined $log->{FILE_PATH} + or $params->{file} eq $log->{FILE_PATH} ); + my $class = ref($proto) || $proto; + my $self = {}; my $LOG_PATH = defined $ENV{KOHA_LOG} ? $ENV{KOHA_LOG} : undef; - $self->{FILE_PATH} = defined $params->{file} ? $params->{file} : $LOG_PATH; - $self->{LEVEL} = defined $params->{level} ? $params->{level} : $INFO_LOG_LEVEL; + $self->{FILE_PATH} = defined $params->{file} ? $params->{file} : $LOG_PATH; + $self->{LEVEL} = + defined $params->{level} ? $params->{level} : $INFO_LOG_LEVEL; + $self->{LOGGED_MESSAGES} = []; + if ( not defined $self->{FILE_PATH} ) { - return bless($self, $class); + return bless( $self, $class ); } eval { - $self->{LOGGER} = Log::LogLite->new($self->{FILE_PATH}, $self->{LEVEL}); + $self->{LOGGER} = + Log::LogLite->new( $self->{FILE_PATH}, $self->{LEVEL} ); }; die "Log system is not correctly configured ($@)" if $@; return bless( $self, $class ); } sub write { - my ($self, $msg, $log_level, $dump, $cb) = @_; + my ( $self, $msg, $log_level, $dump, $cb ) = @_; if ( not $self->{LOGGER} ) { - if($log_level <= $self->{LEVEL}) { - print STDERR "[" . localtime() . "] " - . "$LEVEL_STR->{$log_level}: " - . $msg - . ( $cb ? " (" . $cb . ")" : "" ) - . "\n"; + if ( $log_level <= $self->{LEVEL} ) { + print STDERR "[" + . localtime() . "] " + . "$LEVEL_STR->{$log_level}: " + . $msg + . ( $cb ? " (" . $cb . ")" : "" ) . "\n"; } return; } @@ -82,64 +90,72 @@ sub write { $template .= "\n"; $self->{LOGGER}->template($template); $msg = "\n" . Dumper $msg if $dump; - $self->{LOGGER}->write($msg, $log_level); + $self->{LOGGER}->write( $msg, $log_level ); + +if ( $log_level <= $self->{LEVEL} ) { + my $message = "[" + . localtime() . "] " + . "$LEVEL_STR->{$log_level}: " + . $msg + . ( $cb ? " (" . $cb . ")" : "" ) . "\n"; + push( @{ $self->{LOGGED_MESSAGES} }, $message ); +} } sub unusable { - my ($self, $msg, $dump) = @_; + my ( $self, $msg, $dump ) = @_; my $cb = $self->called_by(); - $self->write($msg, $UNUSABLE_LOG_LEVEL, $dump, $cb); + $self->write( $msg, $UNUSABLE_LOG_LEVEL, $dump, $cb ); } sub critical { - my ($self, $msg, $dump) = @_; + my ( $self, $msg, $dump ) = @_; my $cb = $self->called_by(); - $self->write($msg, $CRITICAL_LOG_LEVEL, $dump, $cb); + $self->write( $msg, $CRITICAL_LOG_LEVEL, $dump, $cb ); } sub error { - my ($self, $msg, $dump) = @_; + my ( $self, $msg, $dump ) = @_; my $cb = $self->called_by(); - $self->write($msg, $ERROR_LOG_LEVEL, $dump, $cb); + $self->write( $msg, $ERROR_LOG_LEVEL, $dump, $cb ); } sub warning { - my ($self, $msg, $dump) = @_; + my ( $self, $msg, $dump ) = @_; my $cb = $self->called_by(); - $self->write($msg, $WARNING_LOG_LEVEL, $dump, $cb); + $self->write( $msg, $WARNING_LOG_LEVEL, $dump, $cb ); } sub log { - my ($self, $msg, $dump) = @_; - $self->write($msg, $NORMAL_LOG_LEVEL, $dump); + my ( $self, $msg, $dump ) = @_; + $self->write( $msg, $NORMAL_LOG_LEVEL, $dump ); } sub normal { - my ($self, $msg, $dump) = @_; - $self->write($msg, $NORMAL_LOG_LEVEL, $dump); + my ( $self, $msg, $dump ) = @_; + $self->write( $msg, $NORMAL_LOG_LEVEL, $dump ); } sub info { - my ($self, $msg, $dump) = @_; - $self->write($msg, $INFO_LOG_LEVEL, $dump); + my ( $self, $msg, $dump ) = @_; + $self->write( $msg, $INFO_LOG_LEVEL, $dump ); } sub debug { - my ($self, $msg, $dump) = @_; - $self->write($msg, $DEBUG_LOG_LEVEL, $dump); + my ( $self, $msg, $dump ) = @_; + $self->write( $msg, $DEBUG_LOG_LEVEL, $dump ); } sub level { my $self = shift; return $self->{LOGGER} - ? $self->{LOGGER}->level(@_) - : ($self->{LEVEL} = @_ ? shift : $self->{LEVEL}); + ? $self->{LOGGER}->level(@_) + : ( $self->{LEVEL} = @_ ? shift : $self->{LEVEL} ); } - sub called_by { - my $self = shift; + my $self = shift; my $depth = 2; my $args; my $pack; @@ -154,15 +170,18 @@ sub called_by { my $bitmask; my @subr; my $str = ""; + while (1) { - ($pack, $file, $line, $subr, $has_args, $wantarray, $evaltext, - $is_require, $hints, $bitmask) = caller($depth); - unless (defined($subr)) { + ( + $pack, $file, $line, $subr, $has_args, + $wantarray, $evaltext, $is_require, $hints, $bitmask + ) = caller($depth); + unless ( defined($subr) ) { last; } $depth++; - $line = (3) ? "$file:".$line."-->" : ""; - push(@subr, $line.$subr); + $line = (3) ? "$file:" . $line . "-->" : ""; + push( @subr, $line . $subr ); } @subr = reverse(@subr); foreach my $sr (@subr) { @@ -171,6 +190,13 @@ sub called_by { } $str =~ s/ > $/: /; return $str; -} # of called_by +} # of called_by + +sub get_messages { + my $self = shift; + + return $self->{LOGGED_MESSAGES} +} + 1; diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql index 33efb78..9ba1aa9 100644 --- a/installer/data/mysql/sysprefs.sql +++ b/installer/data/mysql/sysprefs.sql @@ -374,3 +374,5 @@ INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES(' INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('AgeRestrictionOverride',0,'Allow staff to check out an item with age restriction.',NULL,'YesNo'); INSERT INTO systempreferences (variable,value,explanation,type) VALUES('DidYouMeanFromAuthorities','0','Suggest searches based on authority file.','YesNo'); INSERT INTO systempreferences (variable,value,options,explanation,type) VALUES ('IncludeSeeFromInSearches','0','','Include see-from references in searches.','YesNo'); +INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('LogLevel','5','Set the level of logs. 1=Unusable, 2=Critical, 3=Error, 4=Warning, 5=Normal, 6=Info, 7=Debug','1|2|3|4|5|6|7','Choice'); +INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('LogToHtmlComments','0','Embed the logs into the html as a comment.','','YesNo'); \ No newline at end of file diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 6303beb..6844dc4 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -5792,7 +5792,10 @@ if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) { $dbh->do(qq{ INSERT IGNORE INTO systempreferences (variable,value,explanation,options,type) VALUES ('LogLevel','5','Set the level of logs. 1=Unusable, 2=Critical, 3=Error, 4=Warning, 5=Normal, 6=Info, 7=Debug','1|2|3|4|5|6|7','Choice'); }); - print "Upgrade to $DBversion done (Add system preference LogLevel)\n"; + $dbh->do(qq{ + INSERT IGNORE INTO systempreferences (variable,value,explanation,options,type) VALUES ('LogToHtmlComments','0','Embed the logs into the html as a comment.','','YesNo'); + }); + print "Upgrade to $DBversion done (Add system preferences LogLevel, LogToHtmlComments)\n"; SetVersion($DBversion); } diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/doc-head-open.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/doc-head-open.inc index b8aa2ad..d869767 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/doc-head-open.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/doc-head-open.inc @@ -2,3 +2,8 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> [% IF ( bidi ) %][% ELSE %][% END %] +[%- IF LogToHtmlComments %] + +[% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref index 3b259e3..3d677f1 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref @@ -125,3 +125,10 @@ Administration: 6: 6- Info 7: 7- Debug - for logs + - + - pref: LogToHtmlComments + default: 0 + choices: + yes: Embed + no: "Don't embed" + - log as a comment in the html. diff --git a/mainpage.pl b/mainpage.pl index f834985..820cbe7 100755 --- a/mainpage.pl +++ b/mainpage.pl @@ -60,4 +60,6 @@ $template->param( pendingsuggestions => $pendingsuggestions ); +C4::Context->logger->error( "an error string"); +C4::Context->logger->normal( "a normal string"); output_html_with_http_headers $query, $cookie, $template->output; -- 1.7.2.5