You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Log.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Olivier Paroz <github@oparoz.com>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC;
  32. use InterfaSys\LogNormalizer\Normalizer;
  33. use \OCP\ILogger;
  34. use OCP\Security\StringUtils;
  35. use OCP\Util;
  36. /**
  37. * logging utilities
  38. *
  39. * This is a stand in, this should be replaced by a Psr\Log\LoggerInterface
  40. * compatible logger. See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
  41. * for the full interface specification.
  42. *
  43. * MonoLog is an example implementing this interface.
  44. */
  45. class Log implements ILogger {
  46. /** @var string */
  47. private $logger;
  48. /** @var SystemConfig */
  49. private $config;
  50. /** @var boolean|null cache the result of the log condition check for the request */
  51. private $logConditionSatisfied = null;
  52. /** @var Normalizer */
  53. private $normalizer;
  54. protected $methodsWithSensitiveParameters = [
  55. // Session/User
  56. 'login',
  57. 'checkPassword',
  58. 'loginWithPassword',
  59. 'updatePrivateKeyPassword',
  60. 'validateUserPass',
  61. // TokenProvider
  62. 'getToken',
  63. 'isTokenPassword',
  64. 'getPassword',
  65. 'decryptPassword',
  66. 'logClientIn',
  67. 'generateToken',
  68. 'validateToken',
  69. // TwoFactorAuth
  70. 'solveChallenge',
  71. 'verifyChallenge',
  72. //ICrypto
  73. 'calculateHMAC',
  74. 'encrypt',
  75. 'decrypt',
  76. //LoginController
  77. 'tryLogin'
  78. ];
  79. /**
  80. * @param string $logger The logger that should be used
  81. * @param SystemConfig $config the system config object
  82. * @param null $normalizer
  83. */
  84. public function __construct($logger=null, SystemConfig $config=null, $normalizer = null) {
  85. // FIXME: Add this for backwards compatibility, should be fixed at some point probably
  86. if($config === null) {
  87. $config = \OC::$server->getSystemConfig();
  88. }
  89. $this->config = $config;
  90. // FIXME: Add this for backwards compatibility, should be fixed at some point probably
  91. if($logger === null) {
  92. // TODO: Drop backwards compatibility for config in the future
  93. $logType = $this->config->getValue('log_type', 'file');
  94. if($logType==='owncloud') {
  95. $logType = 'file';
  96. }
  97. $this->logger = 'OC\\Log\\'.ucfirst($logType);
  98. call_user_func(array($this->logger, 'init'));
  99. } else {
  100. $this->logger = $logger;
  101. }
  102. if ($normalizer === null) {
  103. $this->normalizer = new Normalizer();
  104. } else {
  105. $this->normalizer = $normalizer;
  106. }
  107. }
  108. /**
  109. * System is unusable.
  110. *
  111. * @param string $message
  112. * @param array $context
  113. * @return void
  114. */
  115. public function emergency($message, array $context = array()) {
  116. $this->log(Util::FATAL, $message, $context);
  117. }
  118. /**
  119. * Action must be taken immediately.
  120. *
  121. * Example: Entire website down, database unavailable, etc. This should
  122. * trigger the SMS alerts and wake you up.
  123. *
  124. * @param string $message
  125. * @param array $context
  126. * @return void
  127. */
  128. public function alert($message, array $context = array()) {
  129. $this->log(Util::ERROR, $message, $context);
  130. }
  131. /**
  132. * Critical conditions.
  133. *
  134. * Example: Application component unavailable, unexpected exception.
  135. *
  136. * @param string $message
  137. * @param array $context
  138. * @return void
  139. */
  140. public function critical($message, array $context = array()) {
  141. $this->log(Util::ERROR, $message, $context);
  142. }
  143. /**
  144. * Runtime errors that do not require immediate action but should typically
  145. * be logged and monitored.
  146. *
  147. * @param string $message
  148. * @param array $context
  149. * @return void
  150. */
  151. public function error($message, array $context = array()) {
  152. $this->log(Util::ERROR, $message, $context);
  153. }
  154. /**
  155. * Exceptional occurrences that are not errors.
  156. *
  157. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  158. * that are not necessarily wrong.
  159. *
  160. * @param string $message
  161. * @param array $context
  162. * @return void
  163. */
  164. public function warning($message, array $context = array()) {
  165. $this->log(Util::WARN, $message, $context);
  166. }
  167. /**
  168. * Normal but significant events.
  169. *
  170. * @param string $message
  171. * @param array $context
  172. * @return void
  173. */
  174. public function notice($message, array $context = array()) {
  175. $this->log(Util::INFO, $message, $context);
  176. }
  177. /**
  178. * Interesting events.
  179. *
  180. * Example: User logs in, SQL logs.
  181. *
  182. * @param string $message
  183. * @param array $context
  184. * @return void
  185. */
  186. public function info($message, array $context = array()) {
  187. $this->log(Util::INFO, $message, $context);
  188. }
  189. /**
  190. * Detailed debug information.
  191. *
  192. * @param string $message
  193. * @param array $context
  194. * @return void
  195. */
  196. public function debug($message, array $context = array()) {
  197. $this->log(Util::DEBUG, $message, $context);
  198. }
  199. /**
  200. * Logs with an arbitrary level.
  201. *
  202. * @param mixed $level
  203. * @param string $message
  204. * @param array $context
  205. * @return void
  206. */
  207. public function log($level, $message, array $context = array()) {
  208. $minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::ERROR);
  209. $logCondition = $this->config->getValue('log.condition', []);
  210. array_walk($context, [$this->normalizer, 'format']);
  211. if (isset($context['app'])) {
  212. $app = $context['app'];
  213. /**
  214. * check log condition based on the context of each log message
  215. * once this is met -> change the required log level to debug
  216. */
  217. if(!empty($logCondition)
  218. && isset($logCondition['apps'])
  219. && in_array($app, $logCondition['apps'], true)) {
  220. $minLevel = Util::DEBUG;
  221. }
  222. } else {
  223. $app = 'no app in context';
  224. }
  225. // interpolate $message as defined in PSR-3
  226. $replace = array();
  227. foreach ($context as $key => $val) {
  228. $replace['{' . $key . '}'] = $val;
  229. }
  230. // interpolate replacement values into the message and return
  231. $message = strtr($message, $replace);
  232. /**
  233. * check for a special log condition - this enables an increased log on
  234. * a per request/user base
  235. */
  236. if($this->logConditionSatisfied === null) {
  237. // default to false to just process this once per request
  238. $this->logConditionSatisfied = false;
  239. if(!empty($logCondition)) {
  240. // check for secret token in the request
  241. if(isset($logCondition['shared_secret'])) {
  242. $request = \OC::$server->getRequest();
  243. // if token is found in the request change set the log condition to satisfied
  244. if($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret'))) {
  245. $this->logConditionSatisfied = true;
  246. }
  247. }
  248. // check for user
  249. if(isset($logCondition['users'])) {
  250. $user = \OC::$server->getUserSession()->getUser();
  251. // if the user matches set the log condition to satisfied
  252. if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
  253. $this->logConditionSatisfied = true;
  254. }
  255. }
  256. }
  257. }
  258. // if log condition is satisfied change the required log level to DEBUG
  259. if($this->logConditionSatisfied) {
  260. $minLevel = Util::DEBUG;
  261. }
  262. if ($level >= $minLevel) {
  263. $logger = $this->logger;
  264. call_user_func(array($logger, 'write'), $app, $message, $level);
  265. }
  266. }
  267. /**
  268. * Logs an exception very detailed
  269. *
  270. * @param \Exception | \Throwable $exception
  271. * @param array $context
  272. * @return void
  273. * @since 8.2.0
  274. */
  275. public function logException($exception, array $context = array()) {
  276. $exception = array(
  277. 'Exception' => get_class($exception),
  278. 'Message' => $exception->getMessage(),
  279. 'Code' => $exception->getCode(),
  280. 'Trace' => $exception->getTraceAsString(),
  281. 'File' => $exception->getFile(),
  282. 'Line' => $exception->getLine(),
  283. );
  284. $exception['Trace'] = preg_replace('!(' . implode('|', $this->methodsWithSensitiveParameters) . ')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $exception['Trace']);
  285. $msg = isset($context['message']) ? $context['message'] : 'Exception';
  286. $msg .= ': ' . json_encode($exception);
  287. $this->error($msg, $context);
  288. }
  289. }