PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Slim/Log.php

http://github.com/shameerc/TextPress
PHP | 219 lines | 68 code | 18 blank | 133 comment | 7 complexity | 9c872a014b8bdd0390eb7efcfe4a32d0 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.0
  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. /**
  57. * @var array
  58. */
  59. static protected $levels = array(
  60. 0 => 'FATAL',
  61. 1 => 'ERROR',
  62. 2 => 'WARN',
  63. 3 => 'INFO',
  64. 4 => 'DEBUG'
  65. );
  66. /**
  67. * @var mixed
  68. */
  69. protected $writer;
  70. /**
  71. * @var bool
  72. */
  73. protected $enabled;
  74. /**
  75. * @var int
  76. */
  77. protected $level;
  78. /**
  79. * Constructor
  80. * @param mixed $writer
  81. * @return void
  82. */
  83. public function __construct( $writer ) {
  84. $this->writer = $writer;
  85. $this->enabled = true;
  86. $this->level = 4;
  87. }
  88. /**
  89. * Is logging enabled?
  90. * @return bool
  91. */
  92. public function getEnabled() {
  93. return $this->enabled;
  94. }
  95. /**
  96. * Enable or disable logging
  97. * @param bool $enabled
  98. * @return void
  99. */
  100. public function setEnabled( $enabled ) {
  101. if ( $enabled ) {
  102. $this->enabled = true;
  103. } else {
  104. $this->enabled = false;
  105. }
  106. }
  107. /**
  108. * Set level
  109. * @param int $level
  110. * @return void
  111. * @throws InvalidArgumentException
  112. */
  113. public function setLevel( $level ) {
  114. if ( !isset(self::$levels[$level]) ) {
  115. throw new InvalidArgumentException('Invalid log level');
  116. }
  117. $this->level = $level;
  118. }
  119. /**
  120. * Get level
  121. * @return int
  122. */
  123. public function getLevel() {
  124. return $this->level;
  125. }
  126. /**
  127. * Set writer
  128. * @param mixed $writer
  129. * @return void
  130. */
  131. public function setWriter( $writer ) {
  132. $this->writer = $writer;
  133. }
  134. /**
  135. * Get writer
  136. * @return mixed
  137. */
  138. public function getWriter() {
  139. return $this->writer;
  140. }
  141. /**
  142. * Is logging enabled?
  143. * @return bool
  144. */
  145. public function isEnabled() {
  146. return $this->enabled;
  147. }
  148. /**
  149. * Log debug message
  150. * @param mixed $object
  151. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  152. */
  153. public function debug( $object ) {
  154. return $this->log($object, 4);
  155. }
  156. /**
  157. * Log info message
  158. * @param mixed $object
  159. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  160. */
  161. public function info( $object ) {
  162. return $this->log($object, 3);
  163. }
  164. /**
  165. * Log warn message
  166. * @param mixed $object
  167. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  168. */
  169. public function warn( $object ) {
  170. return $this->log($object, 2);
  171. }
  172. /**
  173. * Log error message
  174. * @param mixed $object
  175. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  176. */
  177. public function error( $object ) {
  178. return $this->log($object, 1);
  179. }
  180. /**
  181. * Log fatal message
  182. * @param mixed $object
  183. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  184. */
  185. public function fatal( $object ) {
  186. return $this->log($object, 0);
  187. }
  188. /**
  189. * Log message
  190. * @param mixed The object to log
  191. * @param int The message level
  192. * @return int|false
  193. */
  194. protected function log( $object, $level ) {
  195. if ( $this->enabled && $this->writer && $level <= $this->level ) {
  196. return $this->writer->write($object, $level);
  197. } else {
  198. return false;
  199. }
  200. }
  201. }