PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Slim/Log.php

https://bitbucket.org/PapayaMedia/diplomado
PHP | 225 lines | 73 code | 19 blank | 133 comment | 7 complexity | e7526863f381bdabe8617daa2c2393f3 MD5 | raw file
  1. <?php
  2. /**
  3. * Slim - a micro PHP 5 framework
  4. *
  5. * @author Josh Lockhart <info@slimframework.com>
  6. * @copyright 2011 Josh Lockhart
  7. * @link http://www.slimframework.com
  8. * @license http://www.slimframework.com/license
  9. * @version 1.6.4
  10. * @package Slim
  11. *
  12. * MIT LICENSE
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining
  15. * a copy of this software and associated documentation files (the
  16. * "Software"), to deal in the Software without restriction, including
  17. * without limitation the rights to use, copy, modify, merge, publish,
  18. * distribute, sublicense, and/or sell copies of the Software, and to
  19. * permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. /**
  34. * Log
  35. *
  36. * This is the primary logger for a Slim application. You may provide
  37. * a Log Writer in conjunction with this Log to write to various output
  38. * destinations (e.g. a file). This class provides this interface:
  39. *
  40. * debug( mixed $object )
  41. * info( mixed $object )
  42. * warn( mixed $object )
  43. * error( mixed $object )
  44. * fatal( mixed $object )
  45. *
  46. * This class assumes only that your Log Writer has a public `write()` method
  47. * that accepts any object as its one and only argument. The Log Writer
  48. * class may write or send its argument anywhere: a file, STDERR,
  49. * a remote web API, etc. The possibilities are endless.
  50. *
  51. * @package Slim
  52. * @author Josh Lockhart
  53. * @since 1.0.0
  54. */
  55. class Slim_Log {
  56. const FATAL = 0;
  57. const ERROR = 1;
  58. const WARN = 2;
  59. const INFO = 3;
  60. const DEBUG = 4;
  61. /**
  62. * @var array
  63. */
  64. static protected $levels = array(
  65. 0 => 'FATAL',
  66. 1 => 'ERROR',
  67. 2 => 'WARN',
  68. 3 => 'INFO',
  69. 4 => 'DEBUG'
  70. );
  71. /**
  72. * @var mixed
  73. */
  74. protected $writer;
  75. /**
  76. * @var bool
  77. */
  78. protected $enabled;
  79. /**
  80. * @var int
  81. */
  82. protected $level;
  83. /**
  84. * Constructor
  85. * @param mixed $writer
  86. * @return void
  87. */
  88. public function __construct( $writer ) {
  89. $this->writer = $writer;
  90. $this->enabled = true;
  91. $this->level = 4;
  92. }
  93. /**
  94. * Is logging enabled?
  95. * @return bool
  96. */
  97. public function getEnabled() {
  98. return $this->enabled;
  99. }
  100. /**
  101. * Enable or disable logging
  102. * @param bool $enabled
  103. * @return void
  104. */
  105. public function setEnabled( $enabled ) {
  106. if ( $enabled ) {
  107. $this->enabled = true;
  108. } else {
  109. $this->enabled = false;
  110. }
  111. }
  112. /**
  113. * Set level
  114. * @param int $level
  115. * @return void
  116. * @throws InvalidArgumentException
  117. */
  118. public function setLevel( $level ) {
  119. if ( !isset(self::$levels[$level]) ) {
  120. throw new InvalidArgumentException('Invalid log level');
  121. }
  122. $this->level = $level;
  123. }
  124. /**
  125. * Get level
  126. * @return int
  127. */
  128. public function getLevel() {
  129. return $this->level;
  130. }
  131. /**
  132. * Set writer
  133. * @param mixed $writer
  134. * @return void
  135. */
  136. public function setWriter( $writer ) {
  137. $this->writer = $writer;
  138. }
  139. /**
  140. * Get writer
  141. * @return mixed
  142. */
  143. public function getWriter() {
  144. return $this->writer;
  145. }
  146. /**
  147. * Is logging enabled?
  148. * @return bool
  149. */
  150. public function isEnabled() {
  151. return $this->enabled;
  152. }
  153. /**
  154. * Log debug message
  155. * @param mixed $object
  156. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  157. */
  158. public function debug( $object ) {
  159. return $this->log($object, 4);
  160. }
  161. /**
  162. * Log info message
  163. * @param mixed $object
  164. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  165. */
  166. public function info( $object ) {
  167. return $this->log($object, 3);
  168. }
  169. /**
  170. * Log warn message
  171. * @param mixed $object
  172. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  173. */
  174. public function warn( $object ) {
  175. return $this->log($object, 2);
  176. }
  177. /**
  178. * Log error message
  179. * @param mixed $object
  180. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  181. */
  182. public function error( $object ) {
  183. return $this->log($object, 1);
  184. }
  185. /**
  186. * Log fatal message
  187. * @param mixed $object
  188. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  189. */
  190. public function fatal( $object ) {
  191. return $this->log($object, 0);
  192. }
  193. /**
  194. * Log message
  195. * @param mixed The object to log
  196. * @param int The message level
  197. * @return int|false
  198. */
  199. protected function log( $object, $level ) {
  200. if ( $this->enabled && $this->writer && $level <= $this->level ) {
  201. return $this->writer->write($object, $level);
  202. } else {
  203. return false;
  204. }
  205. }
  206. }