Author: chammers
Date: Tue Oct 6 19:54:12 2009
New Revision: 822461
URL: http://svn.apache.org/viewvc?rev=822461&view=rev
Log:
LOG4PHP-81
Boolean values in .properties files and incorrectly parsed by
LoggerConfiguratorIni.
Currently boolean values in log4php.properties have to be written as "true" and
"false", i.e. as strings because they do not get parsed correctly else.
This is quite irritating as the ini file format as well as the name of the
configurator suggests that this file does behave like a PHP .ini file where
true/false/on/off etc. can be used. Moreover it is no problem with log4j and
the properties files should be interchangeable if possible.
The patch basically it does three things:
1. LoggerOptionConvert::findAndSubst() no longer tries to be clever and skips
settings where empty($value)==true because PHPs parse_ini_file() already
converts a false without quotes to an empty string.
2. As the generic options parser cannot distinguish if the user has written the
number 0 or the boolean false (both is converted to string(1)"0" by
parse_ini_file()) etc. the conversion has to be done in the setter methods
because only they know if they want numbers or booleans for their attributes.
3. Those setter methods can use the already existing toBoolean() method.
Although until now it did not handle all cases that parse_ini_file() mangles
correctly.
A phpunit test case is also attached.
Added:
incubator/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php
Modified:
incubator/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php
Modified: incubator/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php
URL:
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php?rev=822461&r1=822460&r2=822461&view=diff
==============================================================================
--- incubator/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php
(original)
+++ incubator/log4php/trunk/src/main/php/helpers/LoggerOptionConverter.php Tue
Oct 6 19:54:12 2009
@@ -75,20 +75,24 @@
*
* @static
*/
- public static function toBoolean($value, $default) {
- if($value === null) {
+ public static function toBoolean($value, $default=true) {
+ if (is_null($value)) {
return $default;
- }
- if($value == 1) {
- return true;
- }
+ } elseif (is_string($value)) {
$trimmedVal = strtolower(trim($value));
- if("true" == $trimmedVal or "yes" == $trimmedVal) {
+
+ if("1" == $trimmedVal or "true" == $trimmedVal or "yes" ==
$trimmedVal or "on" == $trimmedVal) {
return true;
- }
- if("false" == $trimmedVal) {
+ } else if ("" == $trimmedVal or "0" == $trimmedVal or "false" ==
$trimmedVal or "no" == $trimmedVal or "off" == $trimmedVal) {
return false;
}
+ } elseif (is_bool($value)) {
+ return $value;
+ } elseif (is_int($value)) {
+ return !($value == 0); // true is everything but 0 like in
C
+ }
+
+ trigger_error("Could not convert ".var_export($value,1)." to
boolean!", E_USER_WARNING);
return $default;
}
@@ -212,10 +216,27 @@
*/
public static function findAndSubst($key, $props) {
$value = @$props[$key];
- if(!empty($value)) {
+
+ // If coming from the LoggerConfiguratorIni, some options were
+ // already mangled by parse_ini_file:
+ //
+ // not specified => never reaches this code
+ // ""|off|false|null => string(0) ""
+ // "1"|on|true => string(1) "1"
+ // "true" => string(4) "true"
+ // "false" => string(5) "false"
+ //
+ // As the integer 1 and the boolean true are therefore indistinguable
+ // it's up to the setter how to deal with it, they can not be cast
+ // into a boolean here. {...@see toBoolean}
+ // Even an empty value has to be given to the setter as it has been
+ // explicitly set by the user and is different from an option which
+ // has not been specified and therefore keeps its default value.
+ //
+ // if(!empty($value)) {
return LoggerOptionConverter::substVars($value, $props);
+ // }
}
- }
/**
* Perform variable substitution in string <var>$val</var> from the
@@ -244,7 +265,7 @@
* <p>A warn is thrown if <var>$val</var> contains a start delimeter
"${"
* which is not balanced by a stop delimeter "}" and an empty string is
returned.</p>
*
- * @log4j-author Avy Sharell
+ * @author Avy Sharell
*
* @param string $val The string on which variable substitution is
performed.
* @param array $props
Added:
incubator/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php
URL:
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php?rev=822461&view=auto
==============================================================================
--- incubator/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php
(added)
+++ incubator/log4php/trunk/src/test/php/helpers/LoggerOptionConverterTest.php
Tue Oct 6 19:54:12 2009
@@ -0,0 +1,45 @@
+<?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 helpers
+ * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License,
Version 2.0
+ * @version SVN: $Id$
+ * @link http://logging.apache.org/log4php
+ */
+// require_once "src/main/php/helpers/LoggerOptionConverter.php";
+class LoggerOptionConverterTest extends PHPUnit_Framework_TestCase {
+
+ public function testToBoolean() {
+ self::assertEquals(true, LoggerOptionConverter::toBoolean(null, true));
+ self::assertEquals(true, LoggerOptionConverter::toBoolean(null));
+ self::assertEquals(true, LoggerOptionConverter::toBoolean(true));
+ self::assertEquals(true, LoggerOptionConverter::toBoolean("1"));
+ self::assertEquals(true, LoggerOptionConverter::toBoolean("true"));
+ self::assertEquals(true, LoggerOptionConverter::toBoolean("on"));
+ self::assertEquals(true, LoggerOptionConverter::toBoolean("yes"));
+
+ self::assertEquals(false, LoggerOptionConverter::toBoolean(null,
false));
+ self::assertEquals(false, LoggerOptionConverter::toBoolean(false));
+ self::assertEquals(false, LoggerOptionConverter::toBoolean(""));
+ self::assertEquals(false, LoggerOptionConverter::toBoolean("0"));
+ self::assertEquals(false, LoggerOptionConverter::toBoolean("false"));
+ self::assertEquals(false, LoggerOptionConverter::toBoolean("off"));
+ self::assertEquals(false, LoggerOptionConverter::toBoolean("no"));
+ }
+}
\ No newline at end of file