PageRenderTime 62ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/EventLog/src/log_message.php

https://github.com/Yannix/zetacomponents
PHP | 225 lines | 85 code | 12 blank | 128 comment | 7 complexity | 1fbc46f40e7bad4273cd83e368f7ebfb MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the ezcLogMessage class.
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package EventLog
  23. * @version //autogentag//
  24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25. * @access private
  26. */
  27. /**
  28. * Holds a log message and provides convenience methods to read the information.
  29. *
  30. * The ezclogMessage class is used for subtracting the information from the message
  31. * parameter from {@link trigger_error()}. See the {@link ezcLog::logHandler} for
  32. * more information.
  33. *
  34. * The message formats that can be parsed are:
  35. *
  36. * <pre>
  37. * [ source, category, error_type ] Message
  38. * </pre>
  39. *
  40. * <pre>
  41. * [ source, category ] Message
  42. * </pre>
  43. *
  44. * When one name is given between the brackets, the category will be set and the message has a default source:
  45. * <pre>
  46. * [ category ] Message
  47. * </pre>
  48. *
  49. * Without any names between the brackets, the default category and source are used:
  50. * <pre>
  51. * Message
  52. * </pre>
  53. *
  54. * The following properties are set after construction or after calling {@link parseMessage()}:
  55. * - message, contains the message without extra the additional information.
  56. * - source, contains either the default source or the source set in the incoming message.
  57. * - category, contains either the default category or the category set in the incoming message.
  58. * - error_type, any severity without the leading "ezcLog::" (see {@link ezcLogMessage::parseMessage}); which are:
  59. * ezcLog::DEBUG, ezcLog::INFO, ezcLog::NOTICE, ezcLog::WARNING, ezcLog::ERROR, ezcLog::FATAL, ezcLog::FAILED_AUDIT, ezcLog::SUCCESS_AUDIT.
  60. * - severity, if error_type is not set: severity of the error. Which is ezcLog::NOTICE, ezcLog::WARNING, or ezcLog::ERROR.
  61. *
  62. * @package EventLog
  63. * @version //autogentag//
  64. * @access private
  65. */
  66. class ezcLogMessage
  67. {
  68. /**
  69. * Holds the properties of this class.
  70. *
  71. * @var array(string=>mixed)
  72. */
  73. protected $properties = array( "message" => "", "source" => "", "category" => "", "severity" => "" );
  74. /**
  75. * Constructs the ezcLogMessage from the $message, $severity, $defaultSource and $defaultCategory.
  76. *
  77. * $message is parsed by parseMessage() and properties are set.
  78. *
  79. * @param string $message
  80. * @param int $severity
  81. * @param string $defaultSource Use this source when not given in the message itself.
  82. * @param string $defaultCategory Use this category when not give in the message itself.
  83. */
  84. public function __construct( $message, $severity, $defaultSource, $defaultCategory )
  85. {
  86. $this->parseMessage( $message, $severity, $defaultSource, $defaultCategory );
  87. }
  88. /**
  89. * Sets the property $name to $value.
  90. *
  91. * @throws ezcBasePropertyNotFoundException
  92. * If the property $name does not exist
  93. * @param string $name
  94. * @param mixed $value
  95. * @ignore
  96. */
  97. public function __set( $name, $value )
  98. {
  99. switch ( $name )
  100. {
  101. case 'message':
  102. case 'source':
  103. case 'category':
  104. case 'severity':
  105. $this->properties[$name] = $value;
  106. return;
  107. }
  108. throw new ezcBasePropertyNotFoundException( $name );
  109. }
  110. /**
  111. * Returns the property $name.
  112. *
  113. * @throws ezcBasePropertyNotFoundException
  114. * If the property $name does not exist
  115. * @param string $name
  116. * @return mixed
  117. * @ignore
  118. */
  119. public function __get( $name )
  120. {
  121. switch ( $name )
  122. {
  123. case 'message':
  124. case 'source':
  125. case 'category':
  126. case 'severity':
  127. return $this->properties[$name];
  128. }
  129. throw new ezcBasePropertyNotFoundException( $name );
  130. }
  131. /**
  132. * Returns true if the property $name is set, otherwise false.
  133. *
  134. * @param string $name
  135. * @return bool
  136. * @ignore
  137. */
  138. public function __isset( $name )
  139. {
  140. switch ( $name )
  141. {
  142. case 'message':
  143. case 'source':
  144. case 'category':
  145. case 'severity':
  146. return isset( $this->properties[$name] );
  147. default:
  148. return false;
  149. }
  150. }
  151. /**
  152. * Parses the message $message and sets the properties.
  153. *
  154. * See the general class documentation for message format.
  155. * The severity $severity can be a E_USER_* PHP constant. The values will be translated accordingly:
  156. * - E_USER_NOTICE -> ezcLog::NOTICE
  157. * - E_USER_WARNING -> ezcLog::WARNING
  158. * - E_USER_ERROR -> ezcLog::ERROR
  159. *
  160. * Any other severity from ezcLog can be encapsulated in the message, for example:
  161. * - [source, message, debug] -> ezcLog::DEBUG
  162. * - [source, message, info] -> ezcLog::INFO
  163. * - [source, message, notice] -> ezcLog::NOTICE
  164. * - [source, message, error] -> ezcLog::ERROR
  165. * - [source, message, warning] -> ezcLog::WARNING
  166. * - [source, message, fatal] -> ezcLog::FATAL
  167. * - [source, message, success_audit] -> ezcLog::SUCCESS_AUDIT
  168. * - [source, message, failed_audit] -> ezcLog::FAILED_AUDIT
  169. *
  170. * @param string $message
  171. * @param int $severity
  172. * @param string $defaultSource
  173. * @param string $defaultCategory
  174. */
  175. public function parseMessage( $message, $severity, $defaultSource, $defaultCategory )
  176. {
  177. preg_match( "/^\s*(?:\[(?:\s?)(?P<source>[^,\]]*)(?:,\s(?P<category>[^,\]]*))?(?:,\s?(?P<level>[a-zA-Z_]*))?\s?\])?\s*(?P<message>.*)$/", $message, $matches );
  178. $this->message = $matches['message'] === '' ? false : $matches['message'];
  179. if ( $matches['category'] === '' )
  180. {
  181. $this->category = $matches['source'] === '' ? $defaultCategory : $matches['source'];
  182. $this->source = $defaultSource;
  183. }
  184. else
  185. {
  186. $this->category = $matches['category'];
  187. $this->source = $matches['source'];
  188. }
  189. if ( $matches['level'] === '' )
  190. {
  191. switch ( $severity )
  192. {
  193. case E_USER_NOTICE: $this->severity = ezcLog::NOTICE; break;
  194. case E_USER_WARNING: $this->severity = ezcLog::WARNING; break;
  195. case E_USER_ERROR: $this->severity = ezcLog::ERROR; break;
  196. default: $this->severity = false;
  197. }
  198. }
  199. else
  200. {
  201. $constantName = 'ezcLog::' . strtoupper( trim( $matches['level'] ) );
  202. if ( !defined( $constantName ) )
  203. {
  204. throw new ezcLogWrongSeverityException( trim( $matches['level'] ) );
  205. }
  206. else
  207. {
  208. $this->severity = constant( $constantName );
  209. }
  210. }
  211. }
  212. }
  213. ?>