1
0

PsrLoggerAdapter.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Log;
  25. use OC\Log;
  26. use OCP\EventDispatcher\IEventDispatcher;
  27. use OCP\ILogger;
  28. use OCP\Log\IDataLogger;
  29. use Psr\Log\InvalidArgumentException;
  30. use Psr\Log\LoggerInterface;
  31. use Throwable;
  32. use function array_key_exists;
  33. use function array_merge;
  34. final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
  35. /** @var Log */
  36. private $logger;
  37. public function __construct(Log $logger) {
  38. $this->logger = $logger;
  39. }
  40. public function setEventDispatcher(IEventDispatcher $eventDispatcher) {
  41. $this->logger->setEventDispatcher($eventDispatcher);
  42. }
  43. private function containsThrowable(array $context): bool {
  44. return array_key_exists('exception', $context) && $context['exception'] instanceof Throwable;
  45. }
  46. /**
  47. * System is unusable.
  48. *
  49. * @param string $message
  50. * @param array $context
  51. *
  52. * @return void
  53. */
  54. public function emergency($message, array $context = []): void {
  55. if ($this->containsThrowable($context)) {
  56. $this->logger->logException($context['exception'], array_merge(
  57. [
  58. 'message' => $message,
  59. 'level' => ILogger::FATAL,
  60. ],
  61. $context
  62. ));
  63. } else {
  64. $this->logger->emergency($message, $context);
  65. }
  66. }
  67. /**
  68. * Action must be taken immediately.
  69. *
  70. * Example: Entire website down, database unavailable, etc. This should
  71. * trigger the SMS alerts and wake you up.
  72. *
  73. * @param string $message
  74. * @param array $context
  75. *
  76. * @return void
  77. */
  78. public function alert($message, array $context = []) {
  79. if ($this->containsThrowable($context)) {
  80. $this->logger->logException($context['exception'], array_merge(
  81. [
  82. 'message' => $message,
  83. 'level' => ILogger::ERROR,
  84. ],
  85. $context
  86. ));
  87. } else {
  88. $this->logger->alert($message, $context);
  89. }
  90. }
  91. /**
  92. * Critical conditions.
  93. *
  94. * Example: Application component unavailable, unexpected exception.
  95. *
  96. * @param string $message
  97. * @param array $context
  98. *
  99. * @return void
  100. */
  101. public function critical($message, array $context = []) {
  102. if ($this->containsThrowable($context)) {
  103. $this->logger->logException($context['exception'], array_merge(
  104. [
  105. 'message' => $message,
  106. 'level' => ILogger::ERROR,
  107. ],
  108. $context
  109. ));
  110. } else {
  111. $this->logger->critical($message, $context);
  112. }
  113. }
  114. /**
  115. * Runtime errors that do not require immediate action but should typically
  116. * be logged and monitored.
  117. *
  118. * @param string $message
  119. * @param array $context
  120. *
  121. * @return void
  122. */
  123. public function error($message, array $context = []) {
  124. if ($this->containsThrowable($context)) {
  125. $this->logger->logException($context['exception'], array_merge(
  126. [
  127. 'message' => $message,
  128. 'level' => ILogger::ERROR,
  129. ],
  130. $context
  131. ));
  132. } else {
  133. $this->logger->error($message, $context);
  134. }
  135. }
  136. /**
  137. * Exceptional occurrences that are not errors.
  138. *
  139. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  140. * that are not necessarily wrong.
  141. *
  142. * @param string $message
  143. * @param array $context
  144. *
  145. * @return void
  146. */
  147. public function warning($message, array $context = []) {
  148. if ($this->containsThrowable($context)) {
  149. $this->logger->logException($context['exception'], array_merge(
  150. [
  151. 'message' => $message,
  152. 'level' => ILogger::WARN,
  153. ],
  154. $context
  155. ));
  156. } else {
  157. $this->logger->warning($message, $context);
  158. }
  159. }
  160. /**
  161. * Normal but significant events.
  162. *
  163. * @param string $message
  164. * @param array $context
  165. *
  166. * @return void
  167. */
  168. public function notice($message, array $context = []) {
  169. if ($this->containsThrowable($context)) {
  170. $this->logger->logException($context['exception'], array_merge(
  171. [
  172. 'message' => $message,
  173. 'level' => ILogger::INFO,
  174. ],
  175. $context
  176. ));
  177. } else {
  178. $this->logger->notice($message, $context);
  179. }
  180. }
  181. /**
  182. * Interesting events.
  183. *
  184. * Example: User logs in, SQL logs.
  185. *
  186. * @param string $message
  187. * @param array $context
  188. *
  189. * @return void
  190. */
  191. public function info($message, array $context = []) {
  192. if ($this->containsThrowable($context)) {
  193. $this->logger->logException($context['exception'], array_merge(
  194. [
  195. 'message' => $message,
  196. 'level' => ILogger::INFO,
  197. ],
  198. $context
  199. ));
  200. } else {
  201. $this->logger->info($message, $context);
  202. }
  203. }
  204. /**
  205. * Detailed debug information.
  206. *
  207. * @param string $message
  208. * @param array $context
  209. *
  210. * @return void
  211. */
  212. public function debug($message, array $context = []) {
  213. if ($this->containsThrowable($context)) {
  214. $this->logger->logException($context['exception'], array_merge(
  215. [
  216. 'message' => $message,
  217. 'level' => ILogger::DEBUG,
  218. ],
  219. $context
  220. ));
  221. } else {
  222. $this->logger->debug($message, $context);
  223. }
  224. }
  225. /**
  226. * Logs with an arbitrary level.
  227. *
  228. * @param mixed $level
  229. * @param string $message
  230. * @param array $context
  231. *
  232. * @return void
  233. *
  234. * @throws InvalidArgumentException
  235. */
  236. public function log($level, $message, array $context = []) {
  237. if (!is_int($level) || $level < ILogger::DEBUG || $level > ILogger::FATAL) {
  238. throw new InvalidArgumentException('Nextcloud allows only integer log levels');
  239. }
  240. if ($this->containsThrowable($context)) {
  241. $this->logger->logException($context['exception'], array_merge(
  242. [
  243. 'message' => $message,
  244. 'level' => $level,
  245. ],
  246. $context
  247. ));
  248. } else {
  249. $this->logger->log($level, $message, $context);
  250. }
  251. }
  252. public function logData(string $message, array $data, array $context = []): void {
  253. $this->logger->logData($message, $data, $context);
  254. }
  255. }