log.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC;
  28. use \OCP\ILogger;
  29. use OCP\Security\StringUtils;
  30. /**
  31. * logging utilities
  32. *
  33. * This is a stand in, this should be replaced by a Psr\Log\LoggerInterface
  34. * compatible logger. See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
  35. * for the full interface specification.
  36. *
  37. * MonoLog is an example implementing this interface.
  38. */
  39. class Log implements ILogger {
  40. /** @var string */
  41. private $logger;
  42. /** @var SystemConfig */
  43. private $config;
  44. /** @var boolean|null cache the result of the log condition check for the request */
  45. private $logConditionSatisfied = null;
  46. /**
  47. * @param string $logger The logger that should be used
  48. * @param SystemConfig $config the system config object
  49. */
  50. public function __construct($logger=null, SystemConfig $config=null) {
  51. // FIXME: Add this for backwards compatibility, should be fixed at some point probably
  52. if($config === null) {
  53. $config = \OC::$server->getSystemConfig();
  54. }
  55. $this->config = $config;
  56. // FIXME: Add this for backwards compatibility, should be fixed at some point probably
  57. if($logger === null) {
  58. $this->logger = 'OC_Log_'.ucfirst($this->config->getValue('log_type', 'owncloud'));
  59. call_user_func(array($this->logger, 'init'));
  60. } else {
  61. $this->logger = $logger;
  62. }
  63. }
  64. /**
  65. * System is unusable.
  66. *
  67. * @param string $message
  68. * @param array $context
  69. */
  70. public function emergency($message, array $context = array()) {
  71. $this->log(\OC_Log::FATAL, $message, $context);
  72. }
  73. /**
  74. * Action must be taken immediately.
  75. *
  76. * Example: Entire website down, database unavailable, etc. This should
  77. * trigger the SMS alerts and wake you up.
  78. *
  79. * @param string $message
  80. * @param array $context
  81. */
  82. public function alert($message, array $context = array()) {
  83. $this->log(\OC_Log::ERROR, $message, $context);
  84. }
  85. /**
  86. * Critical conditions.
  87. *
  88. * Example: Application component unavailable, unexpected exception.
  89. *
  90. * @param string $message
  91. * @param array $context
  92. */
  93. public function critical($message, array $context = array()) {
  94. $this->log(\OC_Log::ERROR, $message, $context);
  95. }
  96. /**
  97. * Runtime errors that do not require immediate action but should typically
  98. * be logged and monitored.
  99. *
  100. * @param string $message
  101. * @param array $context
  102. */
  103. public function error($message, array $context = array()) {
  104. $this->log(\OC_Log::ERROR, $message, $context);
  105. }
  106. /**
  107. * Exceptional occurrences that are not errors.
  108. *
  109. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  110. * that are not necessarily wrong.
  111. *
  112. * @param string $message
  113. * @param array $context
  114. */
  115. public function warning($message, array $context = array()) {
  116. $this->log(\OC_Log::WARN, $message, $context);
  117. }
  118. /**
  119. * Normal but significant events.
  120. *
  121. * @param string $message
  122. * @param array $context
  123. */
  124. public function notice($message, array $context = array()) {
  125. $this->log(\OC_Log::INFO, $message, $context);
  126. }
  127. /**
  128. * Interesting events.
  129. *
  130. * Example: User logs in, SQL logs.
  131. *
  132. * @param string $message
  133. * @param array $context
  134. */
  135. public function info($message, array $context = array()) {
  136. $this->log(\OC_Log::INFO, $message, $context);
  137. }
  138. /**
  139. * Detailed debug information.
  140. *
  141. * @param string $message
  142. * @param array $context
  143. */
  144. public function debug($message, array $context = array()) {
  145. $this->log(\OC_Log::DEBUG, $message, $context);
  146. }
  147. /**
  148. * Logs with an arbitrary level.
  149. *
  150. * @param mixed $level
  151. * @param string $message
  152. * @param array $context
  153. */
  154. public function log($level, $message, array $context = array()) {
  155. $minLevel = min($this->config->getValue('loglevel', \OC_Log::WARN), \OC_Log::ERROR);
  156. $logCondition = $this->config->getValue('log.condition', []);
  157. if (isset($context['app'])) {
  158. $app = $context['app'];
  159. /**
  160. * check log condition based on the context of each log message
  161. * once this is met -> change the required log level to debug
  162. */
  163. if(!empty($logCondition)
  164. && isset($logCondition['apps'])
  165. && in_array($app, $logCondition['apps'], true)) {
  166. $minLevel = \OC_Log::DEBUG;
  167. }
  168. } else {
  169. $app = 'no app in context';
  170. }
  171. // interpolate $message as defined in PSR-3
  172. $replace = array();
  173. foreach ($context as $key => $val) {
  174. $replace['{' . $key . '}'] = $val;
  175. }
  176. // interpolate replacement values into the message and return
  177. $message = strtr($message, $replace);
  178. /**
  179. * check for a special log condition - this enables an increased log on
  180. * a per request/user base
  181. */
  182. if($this->logConditionSatisfied === null) {
  183. // default to false to just process this once per request
  184. $this->logConditionSatisfied = false;
  185. if(!empty($logCondition)) {
  186. // check for secret token in the request
  187. if(isset($logCondition['shared_secret'])) {
  188. $request = \OC::$server->getRequest();
  189. // if token is found in the request change set the log condition to satisfied
  190. if($request && StringUtils::equals($request->getParam('log_secret'), $logCondition['shared_secret'])) {
  191. $this->logConditionSatisfied = true;
  192. }
  193. }
  194. // check for user
  195. if(isset($logCondition['users'])) {
  196. $user = \OC::$server->getUserSession()->getUser();
  197. // if the user matches set the log condition to satisfied
  198. if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
  199. $this->logConditionSatisfied = true;
  200. }
  201. }
  202. }
  203. }
  204. // if log condition is satisfied change the required log level to DEBUG
  205. if($this->logConditionSatisfied) {
  206. $minLevel = \OC_Log::DEBUG;
  207. }
  208. if ($level >= $minLevel) {
  209. $logger = $this->logger;
  210. call_user_func(array($logger, 'write'), $app, $message, $level);
  211. }
  212. }
  213. }