Author: fh
Date: Sun Jul 22 13:48:07 2007
New Revision: 5724

Log:
- Implemented #010434: unix syslog writer, also works with the windows event 
system


Added:
    trunk/EventLog/src/writers/writer_syslog.php   (with props)
    trunk/EventLog/tests/writers/data/
    trunk/EventLog/tests/writers/data/syslog_tests.php   (with props)
    trunk/EventLog/tests/writers/writer_syslog_test.php   (with props)
Modified:
    trunk/EventLog/ChangeLog
    trunk/EventLog/design/class_diagram.png
    trunk/EventLog/src/exceptions/writer_exception.php
    trunk/EventLog/src/interfaces/writer.php
    trunk/EventLog/src/log_autoload.php
    trunk/EventLog/tests/suite.php

Modified: trunk/EventLog/ChangeLog
==============================================================================
--- trunk/EventLog/ChangeLog [iso-8859-1] (original)
+++ trunk/EventLog/ChangeLog [iso-8859-1] Sun Jul 22 13:48:07 2007
@@ -1,3 +1,9 @@
+1.2 [RELEASEDATE]
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+- Implemented #010434: unix syslog writer, also works with the windows event 
system
+
+
 1.1 - Monday 02 July 2007
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 

Modified: trunk/EventLog/design/class_diagram.png
==============================================================================
Binary files - no diff available.

Modified: trunk/EventLog/src/exceptions/writer_exception.php
==============================================================================
--- trunk/EventLog/src/exceptions/writer_exception.php [iso-8859-1] (original)
+++ trunk/EventLog/src/exceptions/writer_exception.php [iso-8859-1] Sun Jul 22 
13:48:07 2007
@@ -1,6 +1,6 @@
 <?php
 /**
- * File containing the ezcLogWriterException class.
+x * File containing the ezcLogWriterException class.
  *
  * @package EventLog
  * @version //autogen//
@@ -14,12 +14,18 @@
  *
  * This exception is a container, containing any kind of exception.
  *
+ * @apichange Remove the wrapping of exceptions.
  * @package EventLog
  * @version //autogen//
  */
 class ezcLogWriterException extends ezcBaseException
 {
-    public $exception; 
+    /**
+     * The wrapped exception.
+     *
+     * @var Exception
+     */
+    public $exception;
 
     /**
      * Constructs a new ezcLogWriterException with the original exception $e.

Modified: trunk/EventLog/src/interfaces/writer.php
==============================================================================
--- trunk/EventLog/src/interfaces/writer.php [iso-8859-1] (original)
+++ trunk/EventLog/src/interfaces/writer.php [iso-8859-1] Sun Jul 22 13:48:07 
2007
@@ -34,7 +34,7 @@
      *
      * @param string $message
      * @param int $severity
-     *        ezcLog::DEBUG, ezcLog::SUCCES_AUDIT, ezcLog::FAIL_AUDIT, 
ezcLog::INFO, ezcLog::NOTICE, 
+     *        ezcLog::DEBUG, ezcLog::SUCCESS_AUDIT, ezcLog::FAILED_AUDIT, 
ezcLog::INFO, ezcLog::NOTICE, 
      *        ezcLog::WARNING, ezcLog::ERROR or ezcLog::FATAL.
      * $param string $source
      * @param string $category

Modified: trunk/EventLog/src/log_autoload.php
==============================================================================
--- trunk/EventLog/src/log_autoload.php [iso-8859-1] (original)
+++ trunk/EventLog/src/log_autoload.php [iso-8859-1] Sun Jul 22 13:48:07 2007
@@ -20,6 +20,7 @@
     'ezcLogFilterRule'      => 'EventLog/mapper/filter_rule.php',
     'ezcLogFilterSet'       => 'EventLog/mapper/filterset.php',
     'ezcLogMessage'         => 'EventLog/log_message.php',
+    'ezcLogSyslogWriter'    => 'EventLog/writers/writer_syslog.php',
     'ezcLogUnixFileWriter'  => 'EventLog/writers/writer_unix_file.php',
 );
 ?>

Added: trunk/EventLog/src/writers/writer_syslog.php
==============================================================================
--- trunk/EventLog/src/writers/writer_syslog.php (added)
+++ trunk/EventLog/src/writers/writer_syslog.php [iso-8859-1] Sun Jul 22 
13:48:07 2007
@@ -1,0 +1,173 @@
+<?php
+/**
+ * File containing the ezcLogSyslogWriter class.
+ *
+ * @package EventLog
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * The ezcLogSyslogWriter class provides functionality to write log messages 
to the
+ * UNIX syslog.
+ *
+ * The writer uses the [EMAIL PROTECTED] syslog() syslog} method and works on 
Windows as
+ * well. Please see the documentation of [EMAIL PROTECTED] syslog() syslog} 
for further information
+ * on how to set it up correctly.
+ *
+ * The EventLog severity levels are mapped to the syslog error levels.
+ * The mapping is as follows:
+ * - ezcLog::DEBUG: LOG_DEBUG
+ * - ezcLog::SUCCES_AUDIT: LOG_INFO
+ * - ezcLog::FAIL_AUDIT: LOG_INFO
+ * - ezcLog::INFO: LOG_INFO
+ * - ezcLog::NOTICE: LOG_NOTICE
+ * - ezcLog::WARNING: LOG_WARNING
+ * - ezcLog::ERROR: LOG_ERR
+ * - ezcLog::FATAL: LOG_CRIT
+ *
+ * @package EventLog
+ * @version //autogentag//
+ */
+class ezcLogSyslogWriter implements ezcLogWriter
+{
+
+    /**
+     * Constructs a new syslog writer with the identity $ident, options $option
+     * and the facility $facility.
+     *
+     * The identity will be prepended to each log message in the syslog.
+     *
+     * The $option argument is used to indicate what logging options will be 
used
+     * when generating a log message. See
+     * [EMAIL PROTECTED] syslog() syslog} for more information on valid values 
for $option.
+     * The default options are LOG_PID and LOG_ODELAY.
+     *
+     * The $facility argument is used to specify what type of program is 
logging
+     * the message. This allows you to specify (in your machine's syslog 
configuration)
+     * how messages coming from different facilities will be handled. See
+     * [EMAIL PROTECTED] syslog() syslog} for more information on valid values 
for $facility.
+     *
+     * @param string $ident
+     * @param int $option
+     * @param int $facility
+     */
+    public function __construct( $ident, $option = null, $facility = LOG_USER )
+    {
+        if( $option == null )
+        {
+            $option = LOG_PID|LOG_ODELAY;
+        }
+        openlog( $ident, $option, $facility );
+    }
+
+    /**
+     * Writes the message $message to the log.
+     *
+     * The writer can use the severity, source, and category to filter the
+     * incoming messages and determine the location where the messages should
+     * be written.
+     *
+     * The array $optional contains extra information that can be added to the 
log. For example:
+     * line numbers, file names, usernames, etc.
+     *
+     * @throws ezcLogWriterException
+     *         If the log writer was unable to write the log message
+     *
+     * @param string $message
+     * @param int $severity
+     *        ezcLog::DEBUG, ezcLog::SUCCESS_AUDIT, ezcLog::FAILED_AUDIT, 
ezcLog::INFO, ezcLog::NOTICE,
+     *        ezcLog::WARNING, ezcLog::ERROR or ezcLog::FATAL.
+     * $param string $source
+     * @param string $category
+     * @param array(string=>string) $optional
+     */
+    public function writeLogMessage( $message, $severity, $source, $category, 
$extraInfo = array() )
+    {
+        // generate the log message
+        $extra = "";
+        if ( sizeof( $extraInfo ) > 0 )
+        {
+            $extra =  " (" . $this->implodeWithKey( ", ", ": ", $extraInfo ) . 
")";
+        }
+
+        $logMsg = "[".ezcLog::translateSeverityName( $severity ) . "] ".
+                  ( $source == "" ? "" : "[$source] ") .
+                  ( $category == "" ? "" : "[$category] " ).
+                  "{$message}{$extra}";
+
+
+        // Map severity to syslog severity
+        $syslogSeverity = LOG_INFO;
+        switch( $severity )
+        {
+            case ezcLog::DEBUG:
+                $syslogSeverity = LOG_DEBUG;
+                break;
+            case ezcLog::SUCCESS_AUDIT:
+            case ezcLog::FAILED_AUDIT:
+            case ezcLog::INFO:
+                $syslogSeverity = LOG_INFO;
+                break;
+            case ezcLog::NOTICE:
+                $syslogSeverity = LOG_NOTICE;
+                break;
+            case ezcLog::WARNING:
+                $syslogSeverity = LOG_WARNING;
+                break;
+            case ezcLog::ERROR:
+                $syslogSeverity = LOG_ERR;
+                break;
+            case ezcLog::FATAL:
+                $syslogSeverity = LOG_CRIT;
+                break;
+            default:
+                $syslogSeverity = LOG_INFO;
+                break;
+        }
+
+        // write to syslog
+        $success = syslog( $syslogSeverity, $logMsg );
+        if( !$success )
+        {
+            throw new ezcLogWriterException( new Exception( "Couldn't not 
write to syslog" ) );
+        }
+    }
+
+    /**
+     * Returns a string from the hash $data.
+     *
+     * The string $splitEntry specifies the string that will be inserted 
between the pairs.
+     * The string $splitKeyVal specifies the string that will be inserted in 
each pair.
+     *
+     * Example:
+     * <code>
+     * $this->implodeWithKey( ", ", ": ", array( "Car" => "red", "Curtains" => 
"blue" );
+     * </code>
+     *
+     * Will create the following string:
+     * <pre>
+     * Car: red, Curtains: blue
+     * </pre>
+     *
+     * @param string $splitEntry
+     * @param string $splitKeyVal
+     * @param array(mixed=>mixed) $data
+     * @return string
+     */
+    protected function implodeWithKey( $splitEntry, $splitKeyVal, $data)
+    {
+        $total = "";
+        if ( is_array( $data ) )
+        {
+            foreach ( $data as $key => $val )
+            {
+                $total .=  $splitEntry . $key . $splitKeyVal . $val;
+            }
+        }
+
+        return substr( $total, strlen( $splitEntry ) );
+    }
+}
+?>

Propchange: trunk/EventLog/src/writers/writer_syslog.php
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: trunk/EventLog/tests/suite.php
==============================================================================
--- trunk/EventLog/tests/suite.php [iso-8859-1] (original)
+++ trunk/EventLog/tests/suite.php [iso-8859-1] Sun Jul 22 13:48:07 2007
@@ -15,26 +15,27 @@
 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");
 
 /**
  * @package EventLog
  * @subpackage Tests
- */    
+ */
 class ezcEventLogSuite extends PHPUnit_Framework_TestSuite
 {
     public function __construct()
     {
         parent::__construct();
         $this->setName("EventLog");
-        
+
         $this->addTest( ezcLogDelayedInitTest::suite() );
         $this->addTest( ezcLogFilterSetTest::suite() );
         $this->addTest( ezcLogContextTest::suite() );
         $this->addTest( ezcLogFileWriterTest::suite() );
         $this->addTest( ezcLogUnixFileWriterTest::suite() );
+        $this->addTest( ezcLogSyslogWriterTest::suite() );
         $this->addTest( ezcLogMessageTest::suite() );
         $this->addTest( ezcLogTest::suite() );
-
     }
 
     public static function suite()

Added: trunk/EventLog/tests/writers/data/syslog_tests.php
==============================================================================
--- trunk/EventLog/tests/writers/data/syslog_tests.php (added)
+++ trunk/EventLog/tests/writers/data/syslog_tests.php [iso-8859-1] Sun Jul 22 
13:48:07 2007
@@ -1,0 +1,37 @@
+<?php
+require dirname( __FILE__ ) . '/../../../../Base/src/base.php';
+function __autoload( $className )
+{
+    ezcBase::autoload( $className );
+}
+
+$writer = new ezcLogSyslogWriter( "ezctest", LOG_PERROR|LOG_PID|LOG_ODELAY );
+
+// extras
+$writer->writeLogMessage( "I was bowling.", ezcLog::DEBUG, "Donny", "quotes",
+                                array( "movie" => "The Big Lebowski" ) );
+
+// debug
+$writer->writeLogMessage( "The dude abides.", ezcLog::DEBUG, "Lebowski", 
"quotes" );
+
+// success audit
+$writer->writeLogMessage( "Don't be fatuous, Jeffrey.", ezcLog::SUCCESS_AUDIT, 
"Maude", "quotes" );
+
+// fail audit
+$writer->writeLogMessage( "Also, my rug was stolen.", ezcLog::FAILED_AUDIT, 
"Lebowski", "quotes" );
+
+// info
+$writer->writeLogMessage( "Obviously you're not a golfer.", ezcLog::INFO, 
"Lebowski", "quotes" );
+
+// notice
+$writer->writeLogMessage( "Forget it, Donny, you're out of your element!",
+                          ezcLog::NOTICE, "Walter", "quotes" );
+
+// warning
+$writer->writeLogMessage( "Donny you're out of your element! Dude, the 
Chinaman is not the issue here!",
+                          ezcLog::FAILED_AUDIT, "Walter", "quotes" );
+
+// fatal
+$writer->writeLogMessage( "Ok, Dude. Have it your way.", ezcLog::FATAL, "The 
stranger", "quotes" );
+
+?>

Propchange: trunk/EventLog/tests/writers/data/syslog_tests.php
------------------------------------------------------------------------------
    svn:eol-style = native

Added: trunk/EventLog/tests/writers/writer_syslog_test.php
==============================================================================
--- trunk/EventLog/tests/writers/writer_syslog_test.php (added)
+++ trunk/EventLog/tests/writers/writer_syslog_test.php [iso-8859-1] Sun Jul 22 
13:48:07 2007
@@ -1,0 +1,136 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005-2007 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 ezcLogSyslogWriterTest extends ezcTestCase
+{
+    protected $writer;
+    protected $stderr;
+
+    protected function setUp()
+    {
+        $this->writer = new ezcLogSyslogWriter( "ezctest", LOG_PID|LOG_ODELAY 
);
+    }
+
+    protected function tearDown()
+    {
+        closelog();
+    }
+
+    // External test. Runs through the tests below and returns the syslog 
output.
+    public function testExternalTest()
+    {
+        $dataDir = dirname( __FILE__ ) . "/data";
+        $phpPath = isset( $_SERVER["_"] ) ? $_SERVER["_"] : "/bin/env php";
+        $scriptFile = "{$dataDir}/syslog_tests.php";
+        $desc = array(
+            0 => array( "pipe", "r" ),  // stdin
+            1 => array( "pipe", "w" ),  // stdout
+            2 => array( "pipe", "w" )   // stderr
+        );
+        $proc = proc_open("'{$phpPath}' '{$scriptFile}'", $desc, $pipes );
+
+        fclose( $pipes[0] );
+        fclose( $pipes[1] );
+
+        $ret = '';
+
+        while (!feof( $pipes[2] ) )
+        {
+            $ret .= fgets( $pipes[2] );
+        }
+        $strings = explode( "\n", $ret );
+        // check each of the strings for correctness
+        $regExp = "/ezctest\S*: \[Debug\] \[Donny\] \[quotes\] I was bowling. 
\(movie: The Big Lebowski\)/";
+        $this->assertRegExp( $regExp, $strings[0] );
+
+        $regExp = "/ezctest\S*: \[Debug\] \[Lebowski\] \[quotes\] The dude 
abides./";
+        $this->assertRegExp( $regExp, $strings[1] );
+
+        $regExp = "/ezctest\S*: \[Success audit\] \[Maude\] \[quotes\] Don't 
be fatuous, Jeffrey./";
+        $this->assertRegExp( $regExp, $strings[2] );
+
+        $regExp = "/ezctest\S*: \[Failed audit\] \[Lebowski\] \[quotes\] Also, 
my rug was stolen./";
+        $this->assertRegExp( $regExp, $strings[3] );
+
+        $regExp = "/ezctest\S*: \[Info\] \[Lebowski\] \[quotes\] Obviously 
you're not a golfer./";
+        $this->assertRegExp( $regExp, $strings[4] );
+
+        $regExp = "/ezctest\S*: \[Notice\] \[Walter\] \[quotes\] Forget it, 
Donny, you're out of your element!/";
+        $this->assertRegExp( $regExp, $strings[5] );
+
+
+        $regExp = "/ezctest\S*: \[Failed audit\] \[Walter\] \[quotes\] Donny 
you're out of your element! Dude, the Chinaman is not the issue here!/";
+        $this->assertRegExp( $regExp, $strings[6] );
+
+        $regExp = "/ezctest\S*: \[Fatal\] \[The stranger\] \[quotes\] Ok, 
Dude. Have it your way./";
+        $this->assertRegExp( $regExp, $strings[7] );
+    }
+
+
+    /**
+     * These tests are the same as the ones run in the testExternalTests()
+     *
+     * They are run to check for any errors, and for code coverage. If you add 
a new test
+     * add it to syslog_tests.php as well. If you change any tests.. change it 
there as well.
+     */
+    public function testExtras()
+    {
+        $this->writer->writeLogMessage( "I was bowling.", ezcLog::DEBUG, 
"Donny", "quotes",
+                                        array( "movie" => "The Big Lebowski" ) 
);
+    }
+
+    public function testDebug()
+    {
+        $this->writer->writeLogMessage( "The dude abides.", ezcLog::DEBUG, 
"Lebowski", "quotes" );
+    }
+
+    public function testSuccessAudit()
+    {
+        $this->writer->writeLogMessage( "Don't be fatuous, Jeffrey.", 
ezcLog::SUCCESS_AUDIT, "Maude", "quotes" );
+    }
+
+    public function testFailAudit()
+    {
+        $this->writer->writeLogMessage( "Also, my rug was stolen.", 
ezcLog::FAILED_AUDIT, "Lebowski", "quotes" );
+    }
+
+    public function testInfo()
+    {
+        $this->writer->writeLogMessage( "Obviously you're not a golfer.", 
ezcLog::INFO, "Lebowski", "quotes" );
+    }
+
+    public function testNotice()
+    {
+        $this->writer->writeLogMessage( "Forget it, Donny, you're out of your 
element!",
+                                        ezcLog::NOTICE, "Walter", "quotes" );
+    }
+
+    public function testWarning()
+    {
+        $this->writer->writeLogMessage( "Donny you're out of your element! 
Dude, the Chinaman is not the issue here!",
+                                        ezcLog::FAILED_AUDIT, "Walter", 
"quotes" );
+    }
+
+    public function testFatal()
+    {
+        $this->writer->writeLogMessage( "Ok, Dude. Have it your way.", 
ezcLog::FATAL, "The stranger", "quotes" );
+    }
+
+    public static function suite()
+       {
+               return new 
PHPUnit_Framework_TestSuite("ezcLogSyslogWriterTest");
+       }
+}
+
+?>

Propchange: trunk/EventLog/tests/writers/writer_syslog_test.php
------------------------------------------------------------------------------
    svn:eol-style = native


-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to