log.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /**
  10. * logging utilities
  11. *
  12. * This is a stand in, this should be replaced by a Psr\Log\LoggerInterface
  13. * compatible logger. See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
  14. * for the full interface specification.
  15. *
  16. * MonoLog is an example implementing this interface.
  17. */
  18. class Log {
  19. private $logClass;
  20. /**
  21. * System is unusable.
  22. *
  23. * @param string $message
  24. * @param array $context
  25. */
  26. public function emergency($message, array $context = array()) {
  27. $this->log(\OC_Log::FATAL, $message, $context);
  28. }
  29. /**
  30. * Action must be taken immediately.
  31. *
  32. * Example: Entire website down, database unavailable, etc. This should
  33. * trigger the SMS alerts and wake you up.
  34. *
  35. * @param string $message
  36. * @param array $context
  37. */
  38. public function alert($message, array $context = array()) {
  39. $this->log(\OC_Log::ERROR, $message, $context);
  40. }
  41. /**
  42. * Critical conditions.
  43. *
  44. * Example: Application component unavailable, unexpected exception.
  45. *
  46. * @param string $message
  47. * @param array $context
  48. */
  49. public function critical($message, array $context = array()) {
  50. $this->log(\OC_Log::ERROR, $message, $context);
  51. }
  52. /**
  53. * Runtime errors that do not require immediate action but should typically
  54. * be logged and monitored.
  55. *
  56. * @param string $message
  57. * @param array $context
  58. */
  59. public function error($message, array $context = array()) {
  60. $this->log(\OC_Log::ERROR, $message, $context);
  61. }
  62. /**
  63. * Exceptional occurrences that are not errors.
  64. *
  65. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  66. * that are not necessarily wrong.
  67. *
  68. * @param string $message
  69. * @param array $context
  70. */
  71. public function warning($message, array $context = array()) {
  72. $this->log(\OC_Log::WARN, $message, $context);
  73. }
  74. /**
  75. * Normal but significant events.
  76. *
  77. * @param string $message
  78. * @param array $context
  79. */
  80. public function notice($message, array $context = array()) {
  81. $this->log(\OC_Log::INFO, $message, $context);
  82. }
  83. /**
  84. * Interesting events.
  85. *
  86. * Example: User logs in, SQL logs.
  87. *
  88. * @param string $message
  89. * @param array $context
  90. */
  91. public function info($message, array $context = array()) {
  92. $this->log(\OC_Log::INFO, $message, $context);
  93. }
  94. /**
  95. * Detailed debug information.
  96. *
  97. * @param string $message
  98. * @param array $context
  99. */
  100. public function debug($message, array $context = array()) {
  101. $this->log(\OC_Log::DEBUG, $message, $context);
  102. }
  103. public function __construct() {
  104. $this->logClass = 'OC_Log_'.ucfirst(\OC_Config::getValue('log_type', 'owncloud'));
  105. call_user_func(array($this->logClass, 'init'));
  106. }
  107. /**
  108. * Logs with an arbitrary level.
  109. *
  110. * @param mixed $level
  111. * @param string $message
  112. * @param array $context
  113. */
  114. public function log($level, $message, array $context = array()) {
  115. if (isset($context['app'])) {
  116. $app = $context['app'];
  117. } else {
  118. $app = 'no app in context';
  119. }
  120. $logClass=$this->logClass;
  121. $logClass::write($app, $message, $level);
  122. }
  123. }