PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/api/Slim/Log.php

https://bitbucket.org/aventer/cellar
PHP | 155 lines | 39 code | 14 blank | 102 comment | 7 complexity | 9ef6156fb7bd726e67518dac09f6e864 MD5 | raw file
  1. <?php
  2. /**
  3. * Slim - a micro PHP 5 framework
  4. *
  5. * @author Josh Lockhart <info@joshlockhart.com>
  6. * @copyright 2011 Josh Lockhart
  7. * @link http://www.slimframework.com
  8. * @license http://www.slimframework.com/license
  9. * @version 1.5.0
  10. *
  11. * MIT LICENSE
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining
  14. * a copy of this software and associated documentation files (the
  15. * "Software"), to deal in the Software without restriction, including
  16. * without limitation the rights to use, copy, modify, merge, publish,
  17. * distribute, sublicense, and/or sell copies of the Software, and to
  18. * permit persons to whom the Software is furnished to do so, subject to
  19. * the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be
  22. * included in all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. */
  32. /**
  33. * Log Adapter
  34. *
  35. * This is an adapter for your own custom Logger. This adapter assumes
  36. * your custom Logger provides the following public instance methods:
  37. *
  38. * debug( mixed $object )
  39. * info( mixed $object )
  40. * warn( mixed $object )
  41. * error( mixed $object )
  42. * fatal( mixed $object )
  43. *
  44. * This class assumes nothing else about your custom Logger, so you are free
  45. * to use Apache's Log4PHP logger or any other log class that, at the
  46. * very least, implements the five public instance methods shown above.
  47. *
  48. * @package Slim
  49. * @author Josh Lockhart <info@joshlockhart.com>
  50. * @since Version 1.0
  51. */
  52. class Slim_Log {
  53. /**
  54. * @var mixed An object that implements expected Logger interface
  55. */
  56. protected $logger;
  57. /**
  58. * @var bool Enable logging?
  59. */
  60. protected $enabled;
  61. /**
  62. * Constructor
  63. */
  64. public function __construct() {
  65. $this->enabled = true;
  66. }
  67. /**
  68. * Enable or disable logging
  69. * @param bool $enabled
  70. * @return void
  71. */
  72. public function setEnabled( $enabled ) {
  73. if ( $enabled ) {
  74. $this->enabled = true;
  75. } else {
  76. $this->enabled = false;
  77. }
  78. }
  79. /**
  80. * Is logging enabled?
  81. * @return bool
  82. */
  83. public function isEnabled() {
  84. return $this->enabled;
  85. }
  86. /**
  87. * Log debug message
  88. * @param mixed $object
  89. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  90. */
  91. public function debug( $object ) {
  92. return isset($this->logger) && $this->isEnabled() ? $this->logger->debug($object) : false;
  93. }
  94. /**
  95. * Log info message
  96. * @param mixed $object
  97. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  98. */
  99. public function info( $object ) {
  100. return isset($this->logger) && $this->isEnabled() ? $this->logger->info($object) : false;
  101. }
  102. /**
  103. * Log warn message
  104. * @param mixed $object
  105. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  106. */
  107. public function warn( $object ) {
  108. return isset($this->logger) && $this->isEnabled() ? $this->logger->warn($object) : false;
  109. }
  110. /**
  111. * Log error message
  112. * @param mixed $object
  113. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  114. */
  115. public function error( $object ) {
  116. return isset($this->logger) && $this->isEnabled() ? $this->logger->error($object) : false;
  117. }
  118. /**
  119. * Log fatal message
  120. * @param mixed $object
  121. * @return mixed|false What the Logger returns, or false if Logger not set or not enabled
  122. */
  123. public function fatal( $object ) {
  124. return isset($this->logger) && $this->isEnabled() ? $this->logger->fatal($object) : false;
  125. }
  126. /**
  127. * Set Logger
  128. * @param mixed $logger
  129. * @return void
  130. */
  131. public function setLogger( $logger ) {
  132. $this->logger = $logger;
  133. }
  134. /**
  135. * Get Logger
  136. * @return mixed
  137. */
  138. public function getLogger() {
  139. return $this->logger;
  140. }
  141. }