3 * log4php is a PHP port of the log4j java logging package.
5 * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
6 * <p>Design, strategies and part of the methods documentation are developed by log4j team
7 * (Ceki Gülcü as log4j project founder and
8 * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
10 * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
11 * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
13 * <p>This software is published under the terms of the LGPL License
14 * a copy of which has been included with this distribution in the LICENSE file.</p>
23 if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
25 require_once(LOG4PHP_DIR . '/LoggerLevel.php');
27 define('LOG4PHP_OPTION_CONVERTER_DELIM_START', '${');
28 define('LOG4PHP_OPTION_CONVERTER_DELIM_STOP', '}');
29 define('LOG4PHP_OPTION_CONVERTER_DELIM_START_LEN', 2);
30 define('LOG4PHP_OPTION_CONVERTER_DELIM_STOP_LEN', 1);
33 * A convenience class to convert property values to specific types.
35 * @author VxR <vxr@vxr.it>
36 * @version $Revision: 2 $
42 class LoggerOptionConverter {
45 * OptionConverter is a static class.
47 function OptionConverter()
59 function concatanateArrays($l, $r)
61 return array_merge($l, $r);
65 * Read a predefined var.
67 * It returns a value referenced by <var>$key</var> using this search criteria:
68 * - if <var>$key</var> is a constant then return it. Else
69 * - if <var>$key</var> is set in <var>$_ENV</var> then return it. Else
70 * - return <var>$def</var>.
72 * @param string $key The key to search for.
73 * @param string $def The default value to return.
74 * @return string the string value of the system property, or the default
75 * value if there is no property with that key.
79 function getSystemProperty($key, $def)
81 LoggerLog::debug("LoggerOptionConverter::getSystemProperty():key=[{$key}]:def=[{$def}].");
84 return (string)constant($key);
85 } elseif (isset($_ENV[$key])) {
86 return (string)$_ENV[$key];
93 * If <var>$value</var> is <i>true</i>, then <i>true</i> is
94 * returned. If <var>$value</var> is <i>false</i>, then
95 * <i>true</i> is returned. Otherwise, <var>$default</var> is
98 * <p>Case of value is unimportant.</p>
100 * @param string $value
101 * @param boolean $default
106 function toBoolean($value, $default)
112 $trimmedVal = strtolower(trim($value));
113 if ("true" == $trimmedVal or "yes" == $trimmedVal)
115 if ("false" == $trimmedVal)
121 * @param string $value
122 * @param integer $default
126 function toInt($value, $default)
128 $value = trim($value);
129 if (is_numeric($value)) {
137 * Converts a standard or custom priority level to a Level
140 * <p> If <var>$value</var> is of form "<b>level#full_file_classname</b>",
141 * where <i>full_file_classname</i> means the class filename with path
142 * but without php extension, then the specified class' <i>toLevel()</i> method
143 * is called to process the specified level string; if no '#'
144 * character is present, then the default {@link LoggerLevel}
145 * class is used to process the level value.</p>
147 * <p>As a special case, if the <var>$value</var> parameter is
148 * equal to the string "NULL", then the value <i>null</i> will
151 * <p>If any error occurs while converting the value to a level,
152 * the <var>$defaultValue</var> parameter, which may be
153 * <i>null</i>, is returned.</p>
155 * <p>Case of <var>$value</var> is insignificant for the level level, but is
156 * significant for the class name part, if present.</p>
158 * @param string $value
159 * @param LoggerLevel $defaultValue
160 * @return LoggerLevel a {@link LoggerLevel} or null
163 //function toLevel($value, $defaultValue) //
\8fC
\90³(2006/12/12)
164 static function toLevel($value, $defaultValue)
167 return $defaultValue;
169 $hashIndex = strpos($value, '#');
170 if ($hashIndex === false) {
171 if("NULL" == strtoupper($value)) {
174 // no class name specified : use standard Level class
175 return LoggerLevel::toLevel($value, $defaultValue);
179 $result = $defaultValue;
181 $clazz = substr($value, ($hashIndex + 1));
182 $levelName = substr($value, 0, $hashIndex);
184 // This is degenerate case but you never know.
185 if("NULL" == strtoupper($levelName)) {
189 LoggerLog::debug("LoggerOptionConverter::toLevel():class=[{$clazz}]:pri=[{$levelName}]");
191 if (!class_exists($clazz))
192 @include_once("{$clazz}.php");
194 $clazz = basename($clazz);
196 if (class_exists($clazz)) {
197 $result = @call_user_func(array($clazz, 'toLevel'), $value, $defaultValue);
198 //if (!is_a($result, 'loggerlevel')) {
199 if (!($result instanceof LoggerLevel)){ //
\8fC
\90³(2006/12/12)
200 LoggerLog::debug("LoggerOptionConverter::toLevel():class=[{$clazz}] cannot call toLevel(). Returning default.");
201 $result = $defaultValue;
204 LoggerLog::warn("LoggerOptionConverter::toLevel() class '{$clazz}' doesnt exists.");
210 * @param string $value
211 * @param float $default
216 function toFileSize($value, $default)
221 $s = strtoupper(trim($value));
222 $multiplier = (float)1;
223 if(($index = strpos($s, 'KB')) !== false) {
225 $s = substr($s, 0, $index);
226 } elseif(($index = strpos($s, 'MB')) !== false) {
227 $multiplier = 1024 * 1024;
228 $s = substr($s, 0, $index);
229 } elseif(($index = strpos($s, 'GB')) !== false) {
230 $multiplier = 1024 * 1024 * 1024;
231 $s = substr($s, 0, $index);
234 return (float)$s * $multiplier;
236 LoggerLog::warn("LoggerOptionConverter::toFileSize() [{$s}] is not in proper form.");
242 * Find the value corresponding to <var>$key</var> in
243 * <var>$props</var>. Then perform variable substitution on the
247 * @param array $props
252 //function findAndSubst($key, $props)
253 static function findAndSubst($key, $props) //
\8fC
\90³(2006/12/12)
255 $value = @$props[$key];
259 return LoggerOptionConverter::substVars($value, $props);
263 * Perform variable substitution in string <var>$val</var> from the
264 * values of keys found with the {@link getSystemProperty()} method.
266 * <p>The variable substitution delimeters are <b>${</b> and <b>}</b>.
268 * <p>For example, if the "MY_CONSTANT" contains "value", then
271 * $s = LoggerOptionConverter::substituteVars("Value of key is ${MY_CONSTANT}.");
273 * will set the variable <i>$s</i> to "Value of key is value.".</p>
275 * <p>If no value could be found for the specified key, then the
276 * <var>$props</var> parameter is searched, if the value could not
277 * be found there, then substitution defaults to the empty string.</p>
279 * <p>For example, if {@link getSystemProperty()} cannot find any value for the key
280 * "inexistentKey", then the call
282 * $s = LoggerOptionConverter::substVars("Value of inexistentKey is [${inexistentKey}]");
284 * will set <var>$s</var> to "Value of inexistentKey is []".</p>
286 * <p>A warn is thrown if <var>$val</var> contains a start delimeter "${"
287 * which is not balanced by a stop delimeter "}" and an empty string is returned.</p>
289 * @log4j-author Avy Sharell
291 * @param string $val The string on which variable substitution is performed.
292 * @param array $props
297 //function substVars($val, $props = null)
298 static function substVars($val, $props = null) //
\8fC
\90³(2006/12/12)
300 LoggerLog::debug("LoggerOptionConverter::substVars():val=[{$val}]");
305 $j = strpos($val, LOG4PHP_OPTION_CONVERTER_DELIM_START, $i);
307 LoggerLog::debug("LoggerOptionConverter::substVars() no more variables");
309 if ($i == 0) { // this is a simple string
310 LoggerLog::debug("LoggerOptionConverter::substVars() simple string");
312 } else { // add the tail string which contails no variables and return the result.
313 $sbuf .= substr($val, $i);
314 LoggerLog::debug("LoggerOptionConverter::substVars():sbuf=[{$sbuf}]. Returning sbuf");
319 $sbuf .= substr($val, $i, $j-$i);
320 LoggerLog::debug("LoggerOptionConverter::substVars():sbuf=[{$sbuf}]:i={$i}:j={$j}.");
321 $k = strpos($val, LOG4PHP_OPTION_CONVERTER_DELIM_STOP, $j);
324 "LoggerOptionConverter::substVars() " .
325 "'{$val}' has no closing brace. Opening brace at position {$j}."
329 $j += LOG4PHP_OPTION_CONVERTER_DELIM_START_LEN;
330 $key = substr($val, $j, $k - $j);
331 // first try in System properties
332 $replacement = LoggerOptionConverter::getSystemProperty($key, null);
333 // then try props parameter
334 if($replacement == null and $props !== null) {
335 $replacement = @$props[$key];
338 if(!empty($replacement)) {
339 // Do variable substitution on the replacement string
340 // such that we can solve "Hello ${x2}" as "Hello p1"
341 // the where the properties are
344 $recursiveReplacement = LoggerOptionConverter::substVars($replacement, $props);
345 $sbuf .= $recursiveReplacement;
347 $i = $k + LOG4PHP_OPTION_CONVERTER_DELIM_STOP_LEN;