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

/Slim/Log.php

http://github.com/codeguy/Slim
PHP | 354 lines | 142 code | 27 blank | 185 comment | 16 complexity | 1de82275a3194f00a7ff988b6e5b66f1 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.6.3
  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, array $context )
  42. * info( mixed $object, array $context )
  43. * notice( mixed $object, array $context )
  44. * warning( mixed $object, array $context )
  45. * error( mixed $object, array $context )
  46. * critical( mixed $object, array $context )
  47. * alert( mixed $object, array $context )
  48. * emergency( mixed $object, array $context )
  49. * log( mixed $level, mixed $object, array $context )
  50. *
  51. * This class assumes only that your Log Writer has a public `write()` method
  52. * that accepts any object as its one and only argument. The Log Writer
  53. * class may write or send its argument anywhere: a file, STDERR,
  54. * a remote web API, etc. The possibilities are endless.
  55. *
  56. * @package Slim
  57. * @author Josh Lockhart
  58. * @since 1.0.0
  59. */
  60. class Log
  61. {
  62. const EMERGENCY = 1;
  63. const ALERT = 2;
  64. const CRITICAL = 3;
  65. const FATAL = 3; //DEPRECATED replace with CRITICAL
  66. const ERROR = 4;
  67. const WARN = 5;
  68. const NOTICE = 6;
  69. const INFO = 7;
  70. const DEBUG = 8;
  71. /**
  72. * @var array
  73. */
  74. protected static $levels = array(
  75. self::EMERGENCY => 'EMERGENCY',
  76. self::ALERT => 'ALERT',
  77. self::CRITICAL => 'CRITICAL',
  78. self::ERROR => 'ERROR',
  79. self::WARN => 'WARNING',
  80. self::NOTICE => 'NOTICE',
  81. self::INFO => 'INFO',
  82. self::DEBUG => 'DEBUG'
  83. );
  84. /**
  85. * @var mixed
  86. */
  87. protected $writer;
  88. /**
  89. * @var bool
  90. */
  91. protected $enabled;
  92. /**
  93. * @var int
  94. */
  95. protected $level;
  96. /**
  97. * Constructor
  98. * @param mixed $writer
  99. */
  100. public function __construct($writer)
  101. {
  102. $this->writer = $writer;
  103. $this->enabled = true;
  104. $this->level = self::DEBUG;
  105. }
  106. /**
  107. * Is logging enabled?
  108. * @return bool
  109. */
  110. public function getEnabled()
  111. {
  112. return $this->enabled;
  113. }
  114. /**
  115. * Enable or disable logging
  116. * @param bool $enabled
  117. */
  118. public function setEnabled($enabled)
  119. {
  120. if ($enabled) {
  121. $this->enabled = true;
  122. } else {
  123. $this->enabled = false;
  124. }
  125. }
  126. /**
  127. * Set level
  128. * @param int $level
  129. * @throws \InvalidArgumentException If invalid log level specified
  130. */
  131. public function setLevel($level)
  132. {
  133. if (!isset(self::$levels[$level])) {
  134. throw new \InvalidArgumentException('Invalid log level');
  135. }
  136. $this->level = $level;
  137. }
  138. /**
  139. * Get level
  140. * @return int
  141. */
  142. public function getLevel()
  143. {
  144. return $this->level;
  145. }
  146. /**
  147. * Set writer
  148. * @param mixed $writer
  149. */
  150. public function setWriter($writer)
  151. {
  152. $this->writer = $writer;
  153. }
  154. /**
  155. * Get writer
  156. * @return mixed
  157. */
  158. public function getWriter()
  159. {
  160. return $this->writer;
  161. }
  162. /**
  163. * Is logging enabled?
  164. * @return bool
  165. */
  166. public function isEnabled()
  167. {
  168. return $this->enabled;
  169. }
  170. /**
  171. * Log debug message
  172. * @param mixed $object
  173. * @param array $context
  174. * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
  175. */
  176. public function debug($object, $context = array())
  177. {
  178. return $this->log(self::DEBUG, $object, $context);
  179. }
  180. /**
  181. * Log info message
  182. * @param mixed $object
  183. * @param array $context
  184. * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
  185. */
  186. public function info($object, $context = array())
  187. {
  188. return $this->log(self::INFO, $object, $context);
  189. }
  190. /**
  191. * Log notice message
  192. * @param mixed $object
  193. * @param array $context
  194. * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
  195. */
  196. public function notice($object, $context = array())
  197. {
  198. return $this->log(self::NOTICE, $object, $context);
  199. }
  200. /**
  201. * Log warning message
  202. * @param mixed $object
  203. * @param array $context
  204. * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
  205. */
  206. public function warning($object, $context = array())
  207. {
  208. return $this->log(self::WARN, $object, $context);
  209. }
  210. /**
  211. * DEPRECATED for function warning
  212. * Log warning message
  213. * @param mixed $object
  214. * @param array $context
  215. * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
  216. */
  217. public function warn($object, $context = array())
  218. {
  219. return $this->log(self::WARN, $object, $context);
  220. }
  221. /**
  222. * Log error message
  223. * @param mixed $object
  224. * @param array $context
  225. * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
  226. */
  227. public function error($object, $context = array())
  228. {
  229. return $this->log(self::ERROR, $object, $context);
  230. }
  231. /**
  232. * Log critical message
  233. * @param mixed $object
  234. * @param array $context
  235. * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
  236. */
  237. public function critical($object, $context = array())
  238. {
  239. return $this->log(self::CRITICAL, $object, $context);
  240. }
  241. /**
  242. * DEPRECATED for function critical
  243. * Log fatal message
  244. * @param mixed $object
  245. * @param array $context
  246. * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
  247. */
  248. public function fatal($object, $context = array())
  249. {
  250. return $this->log(self::CRITICAL, $object, $context);
  251. }
  252. /**
  253. * Log alert message
  254. * @param mixed $object
  255. * @param array $context
  256. * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
  257. */
  258. public function alert($object, $context = array())
  259. {
  260. return $this->log(self::ALERT, $object, $context);
  261. }
  262. /**
  263. * Log emergency message
  264. * @param mixed $object
  265. * @param array $context
  266. * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
  267. */
  268. public function emergency($object, $context = array())
  269. {
  270. return $this->log(self::EMERGENCY, $object, $context);
  271. }
  272. /**
  273. * Log message
  274. * @param mixed $level
  275. * @param mixed $object
  276. * @param array $context
  277. * @return mixed|bool What the Logger returns, or false if Logger not set or not enabled
  278. * @throws \InvalidArgumentException If invalid log level
  279. */
  280. public function log($level, $object, $context = array())
  281. {
  282. if (!isset(self::$levels[$level])) {
  283. throw new \InvalidArgumentException('Invalid log level supplied to function');
  284. } else if ($this->enabled && $this->writer && $level <= $this->level) {
  285. if (is_array($object) || (is_object($object) && !method_exists($object, "__toString"))) {
  286. $message = print_r($object, true);
  287. } else {
  288. $message = (string) $object;
  289. }
  290. if (count($context) > 0) {
  291. if (isset($context['exception']) && $context['exception'] instanceof \Exception) {
  292. $message .= ' - ' . $context['exception'];
  293. unset($context['exception']);
  294. }
  295. $message = $this->interpolate($message, $context);
  296. }
  297. return $this->writer->write($message, $level);
  298. } else {
  299. return false;
  300. }
  301. }
  302. /**
  303. * DEPRECATED for function log
  304. * Log message
  305. * @param mixed $object The object to log
  306. * @param int $level The message level
  307. * @return int|bool
  308. */
  309. protected function write($object, $level)
  310. {
  311. return $this->log($level, $object);
  312. }
  313. /**
  314. * Interpolate log message
  315. * @param mixed $message The log message
  316. * @param array $context An array of placeholder values
  317. * @return string The processed string
  318. */
  319. protected function interpolate($message, $context = array())
  320. {
  321. $replace = array();
  322. foreach ($context as $key => $value) {
  323. $replace['{' . $key . '}'] = $value;
  324. }
  325. return strtr($message, $replace);
  326. }
  327. }