-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Item993: made the log system more rational. Logging is now done via a…
…n abstract interface that is independent of the underlying log implementation. Logs can be reviewed. Log date stamps have been changed to time stamps, accurate to the second. git-svn-id: http://svn.foswiki.org/trunk@2621 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
- Loading branch information
CrawfordCurrie
authored and
CrawfordCurrie
committed
Feb 23, 2009
1 parent
71035a5
commit 9c32b6a
Showing
19 changed files
with
349 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
# tests for Foswiki::Logger | ||
|
||
package Logger; | ||
use base qw( FoswikiTestCase ); | ||
|
||
use strict; | ||
use File::Temp; | ||
use Foswiki::Logger::PlainFile; | ||
|
||
# NOTE: Test logs are created in the test web so they get torn down when the | ||
# web is torn down in the superclass. | ||
|
||
sub set_up { | ||
my $this = shift; | ||
$this->SUPER::set_up(); | ||
|
||
$Foswiki::cfg{DebugFileName} = "DebugFileName$$"; | ||
$Foswiki::cfg{WarningFileName} = "WarningFileName$$"; | ||
$Foswiki::cfg{LogFileName} = "LogFileName$$"; | ||
} | ||
|
||
sub tear_down { | ||
my $this = shift; | ||
|
||
unlink($Foswiki::cfg{DebugFileName}); | ||
unlink($Foswiki::cfg{WarningFileName}); | ||
unlink($Foswiki::cfg{LogFileName}); | ||
$this->SUPER::tear_down(); | ||
} | ||
|
||
sub fixture_groups { | ||
my %algs; | ||
foreach my $dir (@INC) { | ||
if ( opendir( D, "$dir/Foswiki/Logger" ) ) { | ||
foreach my $alg ( readdir D ) { | ||
next unless $alg =~ /^(\w+)\.pm$/; | ||
$algs{$1} = 1; | ||
} | ||
closedir(D); | ||
} | ||
} | ||
my @groups; | ||
foreach my $alg ( keys %algs ) { | ||
my $fn = $alg . 'Logger'; | ||
push( @groups, $fn ); | ||
next if ( defined(&$fn) ); | ||
my $class = "Foswiki::Logger::$alg"; | ||
eval <<HERE; | ||
sub $fn { | ||
my \$this = shift; | ||
require $class; | ||
\$this->{logger} = new $class(); | ||
} | ||
HERE | ||
die $@ if $@; | ||
} | ||
|
||
return \@groups; | ||
} | ||
|
||
sub verify_simpleWriteAndReplay { | ||
my $this = shift; | ||
my $time = time; | ||
# Verify the three levels used by Foswiki; debug, info and warning | ||
foreach my $level qw(debug info warning) { | ||
$this->{logger}->log( | ||
$level, | ||
$level, "Green", "Eggs", "and", "Ham"); | ||
} | ||
foreach my $level qw(debug info warning) { | ||
my $it = $this->{logger}->eachEventSince($time, $level); | ||
$this->assert($it->hasNext(), $level); | ||
my $data = $it->next(); | ||
my $t = shift( @$data ); | ||
$this->assert($t >= $time, "$t $time"); | ||
$this->assert_str_equals( | ||
"$level.Green.Eggs.and.Ham", join('.', @$data)); | ||
$this->assert(!$it->hasNext()); | ||
} | ||
} | ||
|
||
sub verify_eachEventSinceOnEmptyLog { | ||
my $this = shift; | ||
foreach my $level qw(debug info warning) { | ||
my $it = $this->{logger}->eachEventSince(0, $level); | ||
$this->assert(!$it->hasNext()); | ||
} | ||
} | ||
|
||
my $plainFileTestTime; | ||
sub PlainFileTestTime { | ||
return $plainFileTestTime; | ||
} | ||
|
||
# Test specific to PlainFile logger | ||
sub test_eachEventSinceOnSeveralLogs { | ||
my $this = shift; | ||
my $logger = new Foswiki::Logger::PlainFile(); | ||
my $cache = \&Foswiki::Logger::PlainFile::_time; | ||
no warnings 'redefine'; | ||
*Foswiki::Logger::PlainFile::_time = \&PlainFileTestTime; | ||
$plainFileTestTime = 3600; # 1am on 1st Jan 1970 | ||
$logger->log('info', "Seal"); | ||
my $firstTime = time - 2*32*24*60*60; | ||
$plainFileTestTime = $firstTime; # 2 months ago | ||
$logger->log('info', "Dolphin"); | ||
$plainFileTestTime += 32*24*60*60; # 1 month ago | ||
$logger->log('info', "Whale"); | ||
$plainFileTestTime = time; # today | ||
$logger->log('info', "Porpoise"); | ||
|
||
my $it = $logger->eachEventSince(0, 'info'); | ||
my $data; | ||
$this->assert($it->hasNext()); $data = $it->next(); | ||
$this->assert_equals(3600, $data->[0]); | ||
$this->assert_str_equals("Seal", $data->[1]); | ||
$this->assert($it->hasNext()); $data = $it->next(); | ||
$this->assert_equals($firstTime, $data->[0]); | ||
$this->assert_str_equals("Dolphin", $data->[1]); | ||
$this->assert($it->hasNext()); $data = $it->next(); | ||
$this->assert_str_equals("Whale", $data->[1]); | ||
$this->assert($it->hasNext()); $data = $it->next(); | ||
$this->assert_equals($plainFileTestTime, $data->[0]); | ||
$this->assert_str_equals("Porpoise", $data->[1]); | ||
$this->assert(!$it->hasNext()); | ||
|
||
# Check the date filter | ||
$it = $logger->eachEventSince($firstTime, 'info'); | ||
$this->assert($it->hasNext()); $data = $it->next(); | ||
$this->assert_equals($firstTime, $data->[0]); | ||
$this->assert_str_equals("Dolphin", $data->[1]); | ||
$this->assert($it->hasNext()); $data = $it->next(); | ||
$this->assert_str_equals("Whale", $data->[1]); | ||
$this->assert($it->hasNext()); $data = $it->next(); | ||
$this->assert_equals($plainFileTestTime, $data->[0]); | ||
$this->assert_str_equals("Porpoise", $data->[1]); | ||
$this->assert(!$it->hasNext()); | ||
|
||
*Foswiki::Logger::PlainFile::_time = $cache; | ||
use warnings 'redefine'; | ||
} | ||
|
||
sub test_filter { | ||
# with PlainFile, warning up are all crammed into one logfile | ||
my $this = shift; | ||
my $logger = new Foswiki::Logger::PlainFile(); | ||
$logger->log('warning', "Shark"); | ||
$logger->log('error', "Dolphin"); | ||
$logger->log('critical', "Injury"); | ||
$logger->log('warning', "Bite"); | ||
$logger->log('alert', "Ram"); | ||
$logger->log('warning', "Hurts"); | ||
$logger->log('critical', "Doctors"); | ||
|
||
my $it = $logger->eachEventSince(0, 'warning'); | ||
my $data; | ||
$this->assert($it->hasNext()); $data = $it->next(); | ||
$this->assert_str_equals("Shark", $data->[1]); | ||
$this->assert($it->hasNext()); $data = $it->next(); | ||
$this->assert_str_equals("Bite", $data->[1]); | ||
$this->assert($it->hasNext()); $data = $it->next(); | ||
$this->assert_str_equals("Hurts", $data->[1]); | ||
$this->assert(!$it->hasNext()); | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# See bottom of file for license and copyright information | ||
package Foswiki::Logger; | ||
|
||
use strict; | ||
|
||
use Assert; | ||
|
||
=begin TML | ||
---+ package Foswiki::Logger | ||
Object that interfaces to whatever records Foswiki log files. | ||
This is a pure virtual base class which must be implemented by a | ||
subclass in the Logger subdirectory and selected by $Foswiki::cfg{Log}{Implementation} | ||
Note that the implementation has to provide a way for the log to be replayed. | ||
Unfortunately this means that the simpler CPAN loggers are not suitable, | ||
nor is | ||
=cut | ||
|
||
=begin TML | ||
---++ ObjectMethod log($level, @fields) | ||
Adds a log message to a log. | ||
* =$level= - level of the event - one of =debug=, =info=, | ||
=warning=, =error=, =critical=, =alert=, =emergency=. | ||
* =@fields= - an arbitrary list of fields to output to the log. | ||
These fields are recoverable when the log is enumerated using the | ||
=eachEventSince= method. | ||
The levels are chosen to be compatible with Log::Dispatch. | ||
=cut | ||
|
||
sub log { | ||
ASSERT('Pure virtual method called') if DEBUG; | ||
} | ||
|
||
=begin TML | ||
---++ ObjectMethod eachEventSince($time, $level) -> $iterator | ||
* =$time= - a time in the past | ||
* =$level= - log level to return events for. | ||
Get an iterator over the list of all the events at the given level | ||
between =$time= and now. | ||
Events are returned in *oldest-first* order. | ||
Each event is returned as a reference to an array. The first element | ||
of this array is always the date of the event (seconds since the epoch). | ||
Subsequent elements are the fields passed to =log=. | ||
Note that a log implementation may choose to collapse several log levels | ||
into a single log. In this case, all messages in the same set as the | ||
requested level will be returned if any of the collapsed levels is selected. | ||
=cut | ||
|
||
sub eachEventSince { | ||
ASSERT('Pure virtual method called') if DEBUG; | ||
} | ||
|
||
1; | ||
__END__ | ||
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/ | ||
# | ||
# Copyright (C) 2008-2009 Foswiki Contributors. Foswiki Contributors | ||
# are listed in the AUTHORS file in the root of this distribution. | ||
# NOTE: Please extend that file, not this notice. | ||
# | ||
# This program 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 2 | ||
# of the License, or (at your option) any later version. For | ||
# more details read LICENSE in the root of this distribution. | ||
# | ||
# This program 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. | ||
# | ||
# As per the GPL, removal of this notice is prohibited. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.