Bugzilla – Attachment 12287 Details for
Bug 8190
Add a logging module to Koha
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 8190 - Followup - Add cached logger, output messages to template
Bug-8190---Followup---Add-cached-logger-output-mes.patch (text/plain), 12.29 KB, created by
Kyle M Hall (khall)
on 2012-09-17 17:54:35 UTC
(
hide
)
Description:
Bug 8190 - Followup - Add cached logger, output messages to template
Filename:
MIME Type:
Creator:
Kyle M Hall (khall)
Created:
2012-09-17 17:54:35 UTC
Size:
12.29 KB
patch
obsolete
>From 002fac9006168616d9c43a863722b007f473a0f8 Mon Sep 17 00:00:00 2001 >From: Kyle M Hall <kyle@bywatersolutions.com> >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 ) %]<html lang="[% lang %]" xml:lang="[% lang %]" dir="[% bidi %]" xmlns="http://www.w3.org/1999/xhtml">[% ELSE %]<html lang="[% lang %]" xml:lang="[% lang %]" xmlns="http://www.w3.org/1999/xhtml">[% END %] > <head> >+[%- IF LogToHtmlComments %] >+<!-- LOG MESSAGES >+[% FOREACH message IN Logger.get_messages() %][% message %][% END %] >+--> >+[% 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
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 8190
:
9921
|
10996
|
12257
|
12264
|
12265
|
12270
|
12282
|
12283
|
12284
|
12287
|
12743
|
12744
|
12745
|
12746
|
13739
|
13740
|
13762
|
13763
|
13925
|
13926
|
14294
|
14296
|
14318
|
14319
|
14320
|
14321
|
14324
|
14388
|
14703
|
14704
|
14705
|
16426
|
16427
|
16428
|
16429
|
16430
|
18799
|
18800
|
18801
|
18802
|
18803
|
18819
|
18820
|
18870
|
18871
|
18872
|
18873
|
18874
|
18875
|
18876
|
18877
|
18880
|
18881
|
18882
|
18883
|
18884
|
18886
|
18887
|
18888
|
18890
|
34837
|
34838
|
34839
|
34840
|
34841
|
34842
|
34843
|
34844
|
34845