diff --git a/library/log4php/pattern/LoggerPatternConverterSuperglobal.php b/library/log4php/pattern/LoggerPatternConverterSuperglobal.php new file mode 100644 --- /dev/null +++ b/library/log4php/pattern/LoggerPatternConverterSuperglobal.php @@ -0,0 +1,100 @@ +option) && $this->option !== '') { + $key = $this->option; + } + + /* + * There is a bug in PHP which doesn't allow superglobals to be + * accessed when their name is stored in a variable, e.g.: + * + * $name = '_SERVER'; + * $array = $$name; + * + * This code does not work when run from within a method (only when run + * in global scope). But the following code does work: + * + * $name = '_SERVER'; + * global $$name; + * $array = $$name; + * + * That's why global is used here. + */ + global ${$this->name}; + + // Check the given superglobal exists. It is possible that it is not initialized. + if (!isset(${$this->name})) { + $class = get_class($this); + trigger_error("log4php: $class: Cannot find superglobal variable \${$this->name}.", E_USER_WARNING); + return; + } + + $source = ${$this->name}; + + // When the key is set, display the matching value + if (isset($key)) { + if (isset($source[$key])) { + $this->value = $source[$key]; + } + } // When the key is not set, display all values + else { + $values = array(); + foreach ($source as $key => $value) { + $values[] = "$key=$value"; + } + $this->value = implode(', ', $values); + } + } + + public function convert(LoggerLoggingEvent $event) { + return $this->value; + } +}