PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/_app/vendor/Slim/Log.php

https://bitbucket.org/andymoogaloo/beamarshall
PHP | 237 lines | 89 code | 19 blank | 129 comment | 7 complexity | 570bfe552e544113356c3d27a0e2ae14 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 2.0.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. namespace Slim;
  34. /**
  35. * Log
  36. *
  37. * This is the primary logger for a Slim application. You may provide
  38. * a Log Writer in conjunction with this Log to write to various output
  39. * destinations (e.g. a file). This class provides this interface:
  40. *
  41. * debug( mixed $object )
  42. * info( mixed $object )
  43. * warn( mixed $object )
  44. * error( mixed $object )
  45. * fatal( mixed $object )
  46. *
  47. * This class assumes only that your Log Writer has a public `write()` method
  48. * that accepts any object as its one and only argument. The Log Writer
  49. * class may write or send its argument anywhere: a file, STDERR,
  50. * a remote web API, etc. The possibilities are endless.
  51. *
  52. * @package Slim
  53. * @author Josh Lockhart
  54. * @since 1.0.0
  55. */
  56. class Log
  57. {
  58. const FATAL = 0;
  59. const ERROR = 1;
  60. const WARN = 2;
  61. const INFO = 3;
  62. const DEBUG = 4;
  63. /**
  64. * @var array
  65. */
  66. protected static $levels = array(
  67. self::FATAL => 'FATAL',
  68. self::ERROR => 'ERROR',
  69. self::WARN => 'WARN',
  70. self::INFO => 'INFO',
  71. self::DEBUG => 'DEBUG'
  72. );
  73. /**
  74. * @var mixed
  75. */
  76. protected $writer;
  77. /**
  78. * @var bool
  79. */
  80. protected $enabled;
  81. /**
  82. * @var int
  83. */
  84. protected $level;
  85. /**
  86. * Constructor
  87. * @param mixed $writer
  88. */
  89. public function __construct($writer)
  90. {
  91. $this->writer = $writer;
  92. $this->enabled = true;
  93. $this->level = self::DEBUG;
  94. }
  95. /**
  96. * Is logging enabled?
  97. * @return bool
  98. */
  99. public function getEnabled()
  100. {
  101. return $this->enabled;
  102. }
  103. /**
  104. * Enable or disable logging
  105. * @param bool $enabled
  106. */
  107. public function setEnabled($enabled)
  108. {
  109. if ($enabled) {
  110. $this->enabled = true;
  111. } else {
  112. $this->enabled = false;
  113. }
  114. }
  115. /**
  116. * Set level
  117. * @param int $level
  118. * @throws \InvalidArgumentException If invalid log level specified
  119. */
  120. public function setLevel($level)
  121. {
  122. if (!isset(self::$levels[$level])) {
  123. throw new \InvalidArgumentException('Invalid log level');
  124. }
  125. $this->level = $level;
  126. }
  127. /**
  128. * Get level
  129. * @return int
  130. */
  131. public function getLevel()
  132. {
  133. return $this->level;
  134. }
  135. /**
  136. * Set writer
  137. * @param mixed $writer
  138. */
  139. public function setWriter($writer)
  140. {
  141. $this->writer = $writer;
  142. }
  143. /**
  144. * Get writer
  145. * @return mixed
  146. */
  147. public function getWriter()
  148. {
  149. return $this->writer;
  150. }
  151. /**
  152. * Is logging enabled?
  153. * @return bool
  154. */
  155. public function isEnabled()
  156. {
  157. return $this->enabled;
  158. }
  159. /**
  160. * Log debug message
  161. * @param mixed $object
  162. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  163. */
  164. public function debug($object)
  165. {
  166. return $this->log($object, self::DEBUG);
  167. }
  168. /**
  169. * Log info message
  170. * @param mixed $object
  171. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  172. */
  173. public function info($object)
  174. {
  175. return $this->log($object, self::INFO);
  176. }
  177. /**
  178. * Log warn 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 warn($object)
  183. {
  184. return $this->log($object, self::WARN);
  185. }
  186. /**
  187. * Log error message
  188. * @param mixed $object
  189. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  190. */
  191. public function error($object)
  192. {
  193. return $this->log($object, self::ERROR);
  194. }
  195. /**
  196. * Log fatal message
  197. * @param mixed $object
  198. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  199. */
  200. public function fatal($object)
  201. {
  202. return $this->log($object, self::FATAL);
  203. }
  204. /**
  205. * Log message
  206. * @param mixed The object to log
  207. * @param int The message level
  208. * @return int|false
  209. */
  210. protected function log($object, $level)
  211. {
  212. if ($this->enabled && $this->writer && $level <= $this->level) {
  213. return $this->writer->write($object, $level);
  214. } else {
  215. return false;
  216. }
  217. }
  218. }