log.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC;
  9. use \OCP\ILogger;
  10. /**
  11. * logging utilities
  12. *
  13. * This is a stand in, this should be replaced by a Psr\Log\LoggerInterface
  14. * compatible logger. See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
  15. * for the full interface specification.
  16. *
  17. * MonoLog is an example implementing this interface.
  18. */
  19. class Log implements ILogger {
  20. private $logger;
  21. /**
  22. * @param string $logger The logger that should be used
  23. */
  24. public function __construct($logger=null) {
  25. // FIXME: Add this for backwards compatibility, should be fixed at some point probably
  26. if($logger === null) {
  27. $this->logger = 'OC_Log_'.ucfirst(\OC_Config::getValue('log_type', 'owncloud'));
  28. call_user_func(array($this->logger, 'init'));
  29. } else {
  30. $this->logger = $logger;
  31. }
  32. }
  33. /**
  34. * System is unusable.
  35. *
  36. * @param string $message
  37. * @param array $context
  38. */
  39. public function emergency($message, array $context = array()) {
  40. $this->log(\OC_Log::FATAL, $message, $context);
  41. }
  42. /**
  43. * Action must be taken immediately.
  44. *
  45. * Example: Entire website down, database unavailable, etc. This should
  46. * trigger the SMS alerts and wake you up.
  47. *
  48. * @param string $message
  49. * @param array $context
  50. */
  51. public function alert($message, array $context = array()) {
  52. $this->log(\OC_Log::ERROR, $message, $context);
  53. }
  54. /**
  55. * Critical conditions.
  56. *
  57. * Example: Application component unavailable, unexpected exception.
  58. *
  59. * @param string $message
  60. * @param array $context
  61. */
  62. public function critical($message, array $context = array()) {
  63. $this->log(\OC_Log::ERROR, $message, $context);
  64. }
  65. /**
  66. * Runtime errors that do not require immediate action but should typically
  67. * be logged and monitored.
  68. *
  69. * @param string $message
  70. * @param array $context
  71. */
  72. public function error($message, array $context = array()) {
  73. $this->log(\OC_Log::ERROR, $message, $context);
  74. }
  75. /**
  76. * Exceptional occurrences that are not errors.
  77. *
  78. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  79. * that are not necessarily wrong.
  80. *
  81. * @param string $message
  82. * @param array $context
  83. */
  84. public function warning($message, array $context = array()) {
  85. $this->log(\OC_Log::WARN, $message, $context);
  86. }
  87. /**
  88. * Normal but significant events.
  89. *
  90. * @param string $message
  91. * @param array $context
  92. */
  93. public function notice($message, array $context = array()) {
  94. $this->log(\OC_Log::INFO, $message, $context);
  95. }
  96. /**
  97. * Interesting events.
  98. *
  99. * Example: User logs in, SQL logs.
  100. *
  101. * @param string $message
  102. * @param array $context
  103. */
  104. public function info($message, array $context = array()) {
  105. $this->log(\OC_Log::INFO, $message, $context);
  106. }
  107. /**
  108. * Detailed debug information.
  109. *
  110. * @param string $message
  111. * @param array $context
  112. */
  113. public function debug($message, array $context = array()) {
  114. $this->log(\OC_Log::DEBUG, $message, $context);
  115. }
  116. /**
  117. * Logs with an arbitrary level.
  118. *
  119. * @param mixed $level
  120. * @param string $message
  121. * @param array $context
  122. */
  123. public function log($level, $message, array $context = array()) {
  124. if (isset($context['app'])) {
  125. $app = $context['app'];
  126. } else {
  127. $app = 'no app in context';
  128. }
  129. // interpolate $message as defined in PSR-3
  130. $replace = array();
  131. foreach ($context as $key => $val) {
  132. $replace['{' . $key . '}'] = $val;
  133. }
  134. // interpolate replacement values into the message and return
  135. $message = strtr($message, $replace);
  136. $logger = $this->logger;
  137. call_user_func(array($logger, 'write'), $app, $message, $level);
  138. }
  139. }