MojaviLogAdapter.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * Mojavi logging adapter for propel
  11. *
  12. * @author Brandon Keepers <brandon@opensoul.org>
  13. * @version $Revision: 1612 $
  14. * @package propel.runtime.logger
  15. */
  16. class MojaviLogAdapter implements BasicLogger
  17. {
  18. /**
  19. * Instance of mojavi logger
  20. */
  21. private $logger = null;
  22. /**
  23. * constructor for setting up Mojavi log adapter
  24. *
  25. * @param ErrorLog $logger instance of Mojavi error log obtained by
  26. * calling LogManager::getLogger();
  27. */
  28. public function __construct($logger = null)
  29. {
  30. $this->logger = $logger;
  31. }
  32. /**
  33. * A convenience function for logging an alert event.
  34. *
  35. * @param mixed $message String or Exception object containing the message
  36. * to log.
  37. */
  38. public function alert($message)
  39. {
  40. $this->log($message, 'alert');
  41. }
  42. /**
  43. * A convenience function for logging a critical event.
  44. *
  45. * @param mixed $message String or Exception object containing the message
  46. * to log.
  47. */
  48. public function crit($message)
  49. {
  50. $this->log($message, 'crit');
  51. }
  52. /**
  53. * A convenience function for logging an error event.
  54. *
  55. * @param mixed $message String or Exception object containing the message
  56. * to log.
  57. */
  58. public function err($message)
  59. {
  60. $this->log($message, 'err');
  61. }
  62. /**
  63. * A convenience function for logging a warning event.
  64. *
  65. * @param mixed $message String or Exception object containing the message
  66. * to log.
  67. */
  68. public function warning($message)
  69. {
  70. $this->log($message, 'warning');
  71. }
  72. /**
  73. * A convenience function for logging an critical event.
  74. *
  75. * @param mixed $message String or Exception object containing the message
  76. * to log.
  77. */
  78. public function notice($message)
  79. {
  80. $this->log($message, 'notice');
  81. }
  82. /**
  83. * A convenience function for logging an critical event.
  84. *
  85. * @param mixed $message String or Exception object containing the message
  86. * to log.
  87. */
  88. public function info($message)
  89. {
  90. $this->log($message, 'info');
  91. }
  92. /**
  93. * A convenience function for logging a debug event.
  94. *
  95. * @param mixed $message String or Exception object containing the message
  96. * to log.
  97. */
  98. public function debug($message)
  99. {
  100. $this->log($message, 'debug');
  101. }
  102. /**
  103. * Primary method to handle logging.
  104. *
  105. * @param mixed $message String or Exception object containing the message
  106. * to log.
  107. * @param int $severity The numeric severity. Defaults to null so that no
  108. * assumptions are made about the logging backend.
  109. */
  110. public function log($message, $severity = null)
  111. {
  112. if (is_null($this->logger))
  113. $this->logger = LogManager::getLogger('propel');
  114. switch($severity)
  115. {
  116. case 'crit':
  117. $method = 'fatal';
  118. break;
  119. case 'err':
  120. $method = 'error';
  121. break;
  122. case 'alert':
  123. case 'warning':
  124. $method = 'warning';
  125. break;
  126. case 'notice':
  127. case 'info':
  128. $method = 'info';
  129. break;
  130. case 'debug':
  131. default:
  132. $method = 'debug';
  133. }
  134. // get a backtrace to pass class, function, file, & line to Mojavi logger
  135. $trace = debug_backtrace();
  136. // call the appropriate Mojavi logger method
  137. $this->logger->{$method} (
  138. $message,
  139. $trace[2]['class'],
  140. $trace[2]['function'],
  141. $trace[1]['file'],
  142. $trace[1]['line']
  143. );
  144. }
  145. }