Author: ihabunek
Date: Sun Oct 23 14:21:01 2011
New Revision: 1187892

URL: http://svn.apache.org/viewvc?rev=1187892&view=rev
Log:
LOG4PHP-155: New layout LoggerLayoutSerialized

Added:
    logging/log4php/trunk/src/main/php/layouts/LoggerLayoutSerialized.php
    logging/log4php/trunk/src/test/php/layouts/LoggerLayoutSerializedTest.php
Modified:
    logging/log4php/trunk/src/changes/changes.xml
    logging/log4php/trunk/src/main/php/Logger.php

Modified: logging/log4php/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/logging/log4php/trunk/src/changes/changes.xml?rev=1187892&r1=1187891&r2=1187892&view=diff
==============================================================================
--- logging/log4php/trunk/src/changes/changes.xml (original)
+++ logging/log4php/trunk/src/changes/changes.xml Sun Oct 23 14:21:01 2011
@@ -21,6 +21,7 @@
        </properties>
        <body>
            <release version="2.2.0" date="SVN">
+               <action date="2011-10-23" type="add" issue="LOG4PHP-155" 
dev="Ivan Habunek">Created a new layout LoggerLayoutSerialized which formats 
events as serialized objects.</action>
                <action date="2011-10-23" type="fix" issue="LOG4PHP-159" 
dev="Ivan Habunek" due-to="Justin Cherniak" due-to-email="justin dot cherniak 
at gmail dot com">Appenders do not close gracefully if a fatal error 
occurs.</action>
                <action date="2011-10-15" type="update" issue="LOG4PHP-152" 
dev="Ivan Habunek">A rewrite of the configurator.</action>
                <action date="2011-09-03" type="fix" issue="LOG4PHP-145" 
dev="Ivan Habunek">The syslog appender does not correctly parse options</action>

Modified: logging/log4php/trunk/src/main/php/Logger.php
URL: 
http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/Logger.php?rev=1187892&r1=1187891&r2=1187892&view=diff
==============================================================================
--- logging/log4php/trunk/src/main/php/Logger.php (original)
+++ logging/log4php/trunk/src/main/php/Logger.php Sun Oct 23 14:21:01 2011
@@ -97,6 +97,7 @@ class Logger {
                'LoggerLayoutSimple' => '/layouts/LoggerLayoutSimple.php',
                'LoggerLayoutTTCC' => '/layouts/LoggerLayoutTTCC.php',
                'LoggerLayoutPattern' => '/layouts/LoggerLayoutPattern.php',
+               'LoggerLayoutSerialized' => 
'/layouts/LoggerLayoutSerialized.php',
                'LoggerLayoutXml' => '/layouts/LoggerLayoutXml.php',
                'LoggerRendererDefault' => 
'/renderers/LoggerRendererDefault.php',
                'LoggerRendererObject' => '/renderers/LoggerRendererObject.php',

Added: logging/log4php/trunk/src/main/php/layouts/LoggerLayoutSerialized.php
URL: 
http://svn.apache.org/viewvc/logging/log4php/trunk/src/main/php/layouts/LoggerLayoutSerialized.php?rev=1187892&view=auto
==============================================================================
--- logging/log4php/trunk/src/main/php/layouts/LoggerLayoutSerialized.php 
(added)
+++ logging/log4php/trunk/src/main/php/layouts/LoggerLayoutSerialized.php Sun 
Oct 23 14:21:01 2011
@@ -0,0 +1,61 @@
+<?php
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @package log4php
+ */
+
+/**
+ * Layout which formats the events using PHP's serialize() function.
+ * 
+ * Available options:
+ * - locationInfo - If set to true, the event's location information will also
+ *                  be serialized (slow, defaults to false).
+ * 
+ * @version $Revision: 1059292 $
+ * @package log4php
+ * @subpackage layouts
+ * @since 2.2
+ */  
+class LoggerLayoutSerialized extends LoggerLayout {
+       
+       /** Whether to include the event's location information (slow). */
+       private $locationInfo = false;
+       
+       /** Sets the location information flag. */
+       public function setLocationInfo($value) {
+               try {
+                       $this->locationInfo = 
LoggerOptionConverter::toBooleanEx($value);
+               } catch (Exception $ex) {
+                       $strVal = var_export($value, true);
+                       $default = var_export($this->locationInfo, true);
+                       $this->warn("Invalid value provided for locationInfo: 
[$strVal]. Expected a boolean. Using default value: [$default].");
+               }
+       }
+       
+       /** Returns the location information flag. */
+       public function getLocationInfo() {
+               return $this->locationInfo;
+       }
+       
+       public function format(LoggerLoggingEvent $event) {
+               // If required, initialize the location data
+               if($this->locationInfo) {
+                       $event->getLocationInformation();
+               }
+               return serialize($event) . PHP_EOL;
+       }
+}

Added: logging/log4php/trunk/src/test/php/layouts/LoggerLayoutSerializedTest.php
URL: 
http://svn.apache.org/viewvc/logging/log4php/trunk/src/test/php/layouts/LoggerLayoutSerializedTest.php?rev=1187892&view=auto
==============================================================================
--- logging/log4php/trunk/src/test/php/layouts/LoggerLayoutSerializedTest.php 
(added)
+++ logging/log4php/trunk/src/test/php/layouts/LoggerLayoutSerializedTest.php 
Sun Oct 23 14:21:01 2011
@@ -0,0 +1,107 @@
+<?php
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ * @category   tests   
+ * @package    log4php
+ * @subpackage appenders
+ * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, 
Version 2.0
+ * @version    SVN: $Id$
+ * @link       http://logging.apache.org/log4php
+ */
+
+/**
+ * @group layouts
+ */
+class LoggerLayoutSerializedTest extends PHPUnit_Framework_TestCase {
+
+       public function testLocationInfo() {
+               $layout = new LoggerLayoutSerialized();
+               self::assertFalse($layout->getLocationInfo());
+               $layout->setLocationInfo(true);
+               self::assertTrue($layout->getLocationInfo());
+               $layout->setLocationInfo(false);
+               self::assertFalse($layout->getLocationInfo());
+       }
+       
+       /**
+        * @expectedException PHPUnit_Framework_Error
+        * @expectedExceptionMessage log4php: [LoggerLayoutSerialized]: Invalid 
value provided for locationInfo: ['foo']. Expected a boolean. Using default 
value: [false].
+        */
+       public function testLocationInfoFail() {
+               $layout = new LoggerLayoutSerialized();
+               $layout->setLocationInfo('foo');
+       }
+       
+       public function testLayout() {
+               Logger::configure(array(
+                       'appenders' => array(
+                               'default' => array(
+                                       'class' => 'LoggerAppenderEcho',
+                                       'layout' => array(
+                                               'class' => 
'LoggerLayoutSerialized'
+                                       )
+                               )
+                       ),
+                       'rootLogger' => array(
+                               'appenders' => array('default')
+                       )
+               ));
+
+               ob_start();
+               $foo = Logger::getLogger('foo');
+               $foo->info("Interesting message.");
+               $actual = ob_get_contents();
+               ob_end_clean();
+               
+               $event = unserialize($actual);
+               
+               self::assertInstanceOf('LoggerLoggingEvent', $event);
+               self::assertEquals('Interesting message.', 
$event->getMessage());
+               self::assertEquals(LoggerLevel::getLevelInfo(), 
$event->getLevel());
+       }
+       
+       public function testLayoutWithLocationInfo() {
+               Logger::configure(array(
+                       'appenders' => array(
+                               'default' => array(
+                                       'class' => 'LoggerAppenderEcho',
+                                       'layout' => array(
+                                               'class' => 
'LoggerLayoutSerialized',
+                                               'params' => array(
+                                                       'locationInfo' => true
+                                               )
+                                       )
+                               )
+                       ),
+                       'rootLogger' => array(
+                               'appenders' => array('default')
+                       )
+               ));
+       
+               ob_start();
+               $foo = Logger::getLogger('foo');
+               $foo->info("Interesting message.");
+               $actual = ob_get_contents();
+               ob_end_clean();
+       
+               $event = unserialize($actual);
+       
+               self::assertInstanceOf('LoggerLoggingEvent', $event);
+               self::assertEquals('Interesting message.', 
$event->getMessage());
+               self::assertEquals(LoggerLevel::getLevelInfo(), 
$event->getLevel());
+       }
+}


Reply via email to