PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/apache-log4php-2.2.1/src/configurators/LoggerConfigurationAdapterINI.php

https://code.google.com/
PHP | 306 lines | 127 code | 35 blank | 144 comment | 32 complexity | 3d9513b14507a31161f270d73c256ebd MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @package log4php
  19. */
  20. /**
  21. * Converts ini configuration files to a PHP array.
  22. *
  23. * These used to be called "properties" files (inherited from log4j), and that
  24. * file extension is still supported.
  25. *
  26. * @package log4php
  27. * @subpackage configurators
  28. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  29. * @version $Revision$
  30. * @since 2.2
  31. */
  32. class LoggerConfigurationAdapterINI implements LoggerConfigurationAdapter {
  33. /** Name to assign to the root logger. */
  34. const ROOT_LOGGER_NAME = "root";
  35. /** Prefix used for defining logger additivity. */
  36. const ADDITIVITY_PREFIX = "log4php.additivity.";
  37. /** Prefix used for defining logger threshold. */
  38. const THRESHOLD_PREFIX = "log4php.threshold";
  39. /** Prefix used for defining the root logger. */
  40. const ROOT_LOGGER_PREFIX = "log4php.rootLogger";
  41. /** Prefix used for defining a logger. */
  42. const LOGGER_PREFIX = "log4php.logger.";
  43. /** Prefix used for defining an appender. */
  44. const APPENDER_PREFIX = "log4php.appender.";
  45. /** Prefix used for defining a renderer. */
  46. const RENDERER_PREFIX = "log4php.renderer.";
  47. /** Holds the configuration. */
  48. private $config = array();
  49. /**
  50. * Loads and parses the INI configuration file.
  51. *
  52. * INI_SCANNER_RAW is used here because otherwise parse_ini_file() will
  53. * try to parse all values, with some strange results. For example, "true"
  54. * will become "1", while "false" and "null" will become "" (empty string).
  55. *
  56. * @see http://php.net/manual/en/function.parse-ini-file.php
  57. *
  58. * @param string $url Path to the config file.
  59. * @throws LoggerException
  60. */
  61. private function load($url) {
  62. if (!file_exists($url)) {
  63. throw new LoggerException("File [$url] does not exist.");
  64. }
  65. $properties = @parse_ini_file($url, true);
  66. if ($properties === false) {
  67. $error = error_get_last();
  68. throw new LoggerException("Error parsing configuration file: {$error['message']}");
  69. }
  70. return $properties;
  71. }
  72. /**
  73. * Converts the provided INI configuration file to a PHP array config.
  74. *
  75. * @param string $path Path to the config file.
  76. * @throws LoggerException If the file cannot be loaded or parsed.
  77. */
  78. public function convert($path) {
  79. // Load the configuration
  80. $properties = $this->load($path);
  81. // Parse threshold
  82. if (isset($properties[self::THRESHOLD_PREFIX])) {
  83. $this->config['threshold'] = $properties[self::THRESHOLD_PREFIX];
  84. }
  85. // Parse root logger
  86. if (isset($properties[self::ROOT_LOGGER_PREFIX])) {
  87. $this->parseLogger($properties[self::ROOT_LOGGER_PREFIX], self::ROOT_LOGGER_NAME);
  88. }
  89. $appenders = array();
  90. foreach($properties as $key => $value) {
  91. // Parse loggers
  92. if ($this->beginsWith($key, self::LOGGER_PREFIX)) {
  93. $name = substr($key, strlen(self::LOGGER_PREFIX));
  94. $this->parseLogger($value, $name);
  95. }
  96. // Parse additivity
  97. if ($this->beginsWith($key, self::ADDITIVITY_PREFIX)) {
  98. $name = substr($key, strlen(self::ADDITIVITY_PREFIX));
  99. $this->config['loggers'][$name]['additivity'] = $value;
  100. }
  101. // Parse appenders
  102. else if ($this->beginsWith($key, self::APPENDER_PREFIX)) {
  103. $this->parseAppender($key, $value);
  104. }
  105. // Parse renderers
  106. else if ($this->beginsWith($key, self::RENDERER_PREFIX)) {
  107. $this->parseRenderer($key, $value);
  108. }
  109. }
  110. return $this->config;
  111. }
  112. /**
  113. * Parses a logger definition.
  114. *
  115. * Loggers are defined in the following manner:
  116. * <pre>
  117. * log4php.logger.<name> = [<level>], [<appender-ref>, <appender-ref>, ...]
  118. * </pre>
  119. *
  120. * @param string $value The configuration value (level and appender-refs).
  121. * @param string $name Logger name.
  122. */
  123. private function parseLogger($value, $name) {
  124. // Value is divided by commas
  125. $parts = explode(',', $value);
  126. if (empty($value) || empty($parts)) {
  127. return;
  128. }
  129. // The first value is the logger level
  130. $level = array_shift($parts);
  131. // The remaining values are appender references
  132. $appenders = array();
  133. while($appender = array_shift($parts)) {
  134. $appender = trim($appender);
  135. if (!empty($appender)) {
  136. $appenders[] = trim($appender);
  137. }
  138. }
  139. // Find the target configuration
  140. if ($name == self::ROOT_LOGGER_NAME) {
  141. $this->config['rootLogger']['level'] = trim($level);
  142. $this->config['rootLogger']['appenders'] = $appenders;
  143. } else {
  144. $this->config['loggers'][$name]['level'] = trim($level);
  145. $this->config['loggers'][$name]['appenders'] = $appenders;
  146. }
  147. }
  148. /**
  149. * Parses an configuration line pertaining to an appender.
  150. *
  151. * Parses the following patterns:
  152. *
  153. * Appender class:
  154. * <pre>
  155. * log4php.appender.<name> = <class>
  156. * </pre>
  157. *
  158. * Appender parameter:
  159. * <pre>
  160. * log4php.appender.<name>.<param> = <value>
  161. * </pre>
  162. *
  163. * Appender threshold:
  164. * <pre>
  165. * log4php.appender.<name>.threshold = <level>
  166. * </pre>
  167. *
  168. * Appender layout:
  169. * <pre>
  170. * log4php.appender.<name>.layout = <layoutClass>
  171. * </pre>
  172. *
  173. * Layout parameter:
  174. * <pre>
  175. * log4php.appender.<name>.layout.<param> = <value>
  176. * </pre>
  177. *
  178. * For example, a full appender config might look like:
  179. * <pre>
  180. * log4php.appender.myAppender = LoggerAppenderConsole
  181. * log4php.appender.myAppender.threshold = info
  182. * log4php.appender.myAppender.target = stdout
  183. * log4php.appender.myAppender.layout = LoggerLayoutPattern
  184. * log4php.appender.myAppender.layout.conversionPattern = "%d %c: %m%n"
  185. * </pre>
  186. *
  187. * After parsing all these options, the following configuration can be
  188. * found under $this->config['appenders']['myAppender']:
  189. * <pre>
  190. * array(
  191. * 'class' => LoggerAppenderConsole,
  192. * 'threshold' => info,
  193. * 'params' => array(
  194. * 'target' => 'stdout'
  195. * ),
  196. * 'layout' => array(
  197. * 'class' => 'LoggerAppenderConsole',
  198. * 'params' => array(
  199. * 'conversionPattern' => '%d %c: %m%n'
  200. * )
  201. * )
  202. * )
  203. * </pre>
  204. *
  205. * @param string $key
  206. * @param string $value
  207. */
  208. private function parseAppender($key, $value) {
  209. // Remove the appender prefix from key
  210. $subKey = substr($key, strlen(self::APPENDER_PREFIX));
  211. // Divide the string by dots
  212. $parts = explode('.', $subKey);
  213. $count = count($parts);
  214. // The first part is always the appender name
  215. $name = trim($parts[0]);
  216. // Only one part - this line defines the appender class
  217. if ($count == 1) {
  218. $this->config['appenders'][$name]['class'] = $value;
  219. return;
  220. }
  221. // Two parts - either a parameter, a threshold or layout class
  222. else if ($count == 2) {
  223. if ($parts[1] == 'layout') {
  224. $this->config['appenders'][$name]['layout']['class'] = $value;
  225. return;
  226. } else if ($parts[1] == 'threshold') {
  227. $this->config['appenders'][$name]['threshold'] = $value;
  228. return;
  229. } else {
  230. $this->config['appenders'][$name]['params'][$parts[1]] = $value;
  231. return;
  232. }
  233. }
  234. // Three parts - this can only be a layout parameter
  235. else if ($count == 3) {
  236. if ($parts[1] == 'layout') {
  237. $this->config['appenders'][$name]['layout']['params'][$parts[2]] = $value;
  238. return;
  239. }
  240. }
  241. trigger_error("log4php: Don't know how to parse the following line: \"$key = $value\". Skipping.");
  242. }
  243. /**
  244. * Parses a renderer definition.
  245. *
  246. * Renderers are defined as:
  247. * <pre>
  248. * log4php.renderer.<renderedClass> = <renderingClass>
  249. * </pre>
  250. *
  251. * @param string $key log4php.renderer.<renderedClass>
  252. * @param string $value <renderingClass>
  253. */
  254. private function parseRenderer($key, $value) {
  255. // Remove the appender prefix from key
  256. $renderedClass = substr($key, strlen(self::APPENDER_PREFIX));
  257. $renderingClass = $value;
  258. $this->config['renderers'][] = compact('renderedClass', 'renderingClass');
  259. }
  260. /** Helper method. Returns true if $str begins with $sub. */
  261. private function beginsWith($str, $sub) {
  262. return (strncmp($str, $sub, strlen($sub)) == 0);
  263. }
  264. }
  265. ?>