Description:
I need a stack writer to log messages for the user, which I can afterwards output in the template. Please consider including the attached patch. It contains the implementation together with tests.
Environment:
Operating System:
PHP Version: (please be specific, like '4.4.3' or '5.1.5')
Database and version:
Browser (and version):
Attachments
0001-implemented-stack-writer.patch (8.2 kb) added by Thomas Koch
From 3cad86a84d466f0848490994d246e41f882f8ff9 Mon Sep 17 00:00:00 2001
From: Thomas Koch <thomas.koch@ymc.ch>
Date: Wed, 10 Dec 2008 14:46:34 +0100
Subject: [PATCH] implemented stack writer
---
src/log_autoload.php | 2 +
src/structs/log_entry.php | 83 +++++++++++++++++++++++++++++++++
src/writers/writer_stack.php | 50 ++++++++++++++++++++
tests/suite.php | 2 +
tests/writers/writer_stack_test.php | 87 +++++++++++++++++++++++++++++++++++
5 files changed, 224 insertions(+), 0 deletions(-)
create mode 100644 src/structs/log_entry.php
create mode 100644 src/writers/writer_stack.php
create mode 100644 tests/writers/writer_stack_test.php
diff --git a/src/log_autoload.php b/src/log_autoload.php
index ab0801e..35a52c9 100644
--- a/src/log_autoload.php
+++ b/src/log_autoload.php
@@ -23,5 +23,7 @@ return array(
'ezcLogMessage' => 'EventLog/log_message.php',
'ezcLogSyslogWriter' => 'EventLog/writers/writer_syslog.php',
'ezcLogUnixFileWriter' => 'EventLog/writers/writer_unix_file.php',
+ 'ezcLogStackWriter' => 'EventLog/writers/writer_stack.php',
+ 'ezcLogEntry' => 'EventLog/structs/log_entry.php',
);
?>
diff --git a/src/structs/log_entry.php b/src/structs/log_entry.php
new file mode 100644
index 0000000..9c119e8
--- /dev/null
+++ b/src/structs/log_entry.php
@@ -0,0 +1,83 @@
+<?php
+/**
+ * File containing the ezcLogEntry class.
+ *
+ * @package EventLog
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * The ezcLogEntry class provides a structure to represent a log entry with all data passed to
+ * ezcLog::log().
+ *
+ * This class is used for entries hold by the ezcLogStackWriter.
+ *
+ * @package EventLog
+ * @version //autogentag//
+ * @mainclass
+ */
+class ezcLogEntry extends ezcBaseStruct
+{
+ /**
+ * The textual log message.
+ *
+ * @var string
+ */
+ public $message;
+
+ /**
+ * Severity of the log message.
+ *
+ * @var int
+ */
+ public $severity;
+
+ /**
+ * The source of the log message.
+ *
+ * @var string
+ */
+ public $source;
+
+ /**
+ * The category of the log message.
+ *
+ * @var string
+ */
+ public $category;
+
+ /**
+ * Optional informations
+ *
+ * @var array
+ */
+ public $optional;
+
+ /**
+ * The timestamp of the moment when this object was created
+ *
+ * @var int
+ */
+ public $timestamp;
+
+ /**
+ * Empty constructor
+ *
+ * @param int $severity
+ * @param array(string) $source
+ * @param array(string) $category
+ */
+ public function __construct( $message, $severity, $source, $category, $optional = array() )
+ {
+ $this->message = $message;
+ $this->severity = $severity;
+ $this->source = $source;
+ $this->category = $category;
+ $this->optional = $optional;
+ $this->timestamp = time();
+ }
+}
+?>
+
diff --git a/src/writers/writer_stack.php b/src/writers/writer_stack.php
new file mode 100644
index 0000000..60dbdfa
--- /dev/null
+++ b/src/writers/writer_stack.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * File containing the ezcLogStackWriter class.
+ *
+ * @package EventLog
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * The ezcLogStackWriter class saves all received log entries internally.
+ *
+ * The main purpose is to get all received log message at once, for example to output them in the
+ * webpage.
+ *
+ * @package EventLog
+ * @version //autogentag//
+ */
+class ezcLogStackWriter implements ezcLogWriter, IteratorAggregate
+{
+ /**
+ * Stores all entries received by this writer.
+ *
+ * @var array( int => ezcLogEntry )
+ */
+ protected $entries = array();
+
+ /**
+ *
+ * @inherit
+ */
+ public function writeLogMessage( $message, $severity, $source, $category, $optional = array())
+ {
+ $this->entries[] = new ezcLogEntry( $message, $severity, $source, $category, $optional );
+ }
+
+ /**
+ * Implements IteratorAggreagate, returns iterator for all entries.
+ *
+ * @see entries
+ * @return ArrayObject
+ */
+ public function getIterator()
+ {
+ return new ArrayObject($this->entries);
+ }
+}
+?>
+
diff --git a/tests/suite.php b/tests/suite.php
index 09e0e52..4020383 100644
--- a/tests/suite.php
+++ b/tests/suite.php
@@ -16,6 +16,7 @@ require_once( "context_test.php");
require_once( "writers/writer_file_test.php");
require_once( "writers/writer_unix_file_test.php");
require_once( "writers/writer_syslog_test.php");
+require_once( "writers/writer_stack_test.php");
/**
* @package EventLog
@@ -34,6 +35,7 @@ class ezcEventLogSuite extends PHPUnit_Framework_TestSuite
$this->addTest( ezcLogFileWriterTest::suite() );
$this->addTest( ezcLogUnixFileWriterTest::suite() );
$this->addTest( ezcLogSyslogWriterTest::suite() );
+ $this->addTest( ezcLogStackWriterTest::suite() );
$this->addTest( ezcLogMessageTest::suite() );
$this->addTest( ezcLogTest::suite() );
}
diff --git a/tests/writers/writer_stack_test.php b/tests/writers/writer_stack_test.php
new file mode 100644
index 0000000..173ce4d
--- /dev/null
+++ b/tests/writers/writer_stack_test.php
@@ -0,0 +1,87 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @version //autogentag//
+ * @filesource
+ * @package EventLog
+ * @subpackage Tests
+ */
+
+/**
+ * @package EventLog
+ * @subpackage Tests
+ */
+class ezcLogStackWriterTest extends ezcTestCase
+{
+ protected $writer;
+
+ protected $messages = array(
+ array("message" => "Alien alert",
+ "severity" => "critical",
+ "source" => "UFO report",
+ "category" => "fake warning",
+ 'optional' => array( 1,2,3 )
+ ),
+ array("message" => "Alien greeting",
+ "severity" => "uncritical",
+ "source" => "Alien News",
+ "category" => "real warning",
+ 'optional' => 'hi'
+ ),
+ );
+
+ protected function setUp()
+ {
+ $this->writer = new ezcLogStackWriter;
+ }
+
+ public function testWriteOneLogMessage()
+ {
+ $m = $this->messages[0];
+
+ $this->writer->writeLogMessage( $m["message"], $m["severity"], $m["source"], $m["category"], $m['optional']);
+
+ foreach( $this->writer as $entry )
+ {
+ $this->assertEquals( $entry->message, $m['message'] );
+ $this->assertEquals( $entry->source, $m['source'] );
+ $this->assertEquals( $entry->severity, $m['severity'] );
+ $this->assertEquals( $entry->category, $m['category'] );
+ $this->assertEquals( $entry->optional, $m['optional'] );
+
+ return;
+ }
+ // should return in the foreach
+ $this->fail();
+ }
+
+ public function testWriteMultipleLogMessages()
+ {
+ foreach( $this->messages as $m )
+ {
+ $this->writer->writeLogMessage( $m["message"], $m["severity"], $m["source"], $m["category"], $m['optional']);
+ }
+
+ $i = 0;
+ foreach( $this->writer as $entry )
+ {
+ $m = $this->messages[$i];
+ ++$i;
+
+ $this->assertEquals( $entry->message, $m['message'] );
+ $this->assertEquals( $entry->source, $m['source'] );
+ $this->assertEquals( $entry->severity, $m['severity'] );
+ $this->assertEquals( $entry->category, $m['category'] );
+ $this->assertEquals( $entry->optional, $m['optional'] );
+ }
+ $this->assertEquals( count( $this->messages ), $i );
+ }
+
+ public static function suite()
+ {
+ return new PHPUnit_Framework_TestSuite(__CLASS__);
+ }
+}
+?>
+
--
1.5.6.5
Attachments (1)
Links
No links for this issue.
Links (0)
History
History (5)