PsrLoggerAdapter.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 AGPL-3.0-or-later
  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. public function __construct(
  36. private Log $logger,
  37. ) {
  38. }
  39. public function setEventDispatcher(IEventDispatcher $eventDispatcher): void {
  40. $this->logger->setEventDispatcher($eventDispatcher);
  41. }
  42. private function containsThrowable(array $context): bool {
  43. return array_key_exists('exception', $context) && $context['exception'] instanceof Throwable;
  44. }
  45. /**
  46. * System is unusable.
  47. *
  48. * @param $message
  49. * @param mixed[] $context
  50. */
  51. public function emergency($message, array $context = []): void {
  52. if ($this->containsThrowable($context)) {
  53. $this->logger->logException($context['exception'], array_merge(
  54. [
  55. 'message' => (string)$message,
  56. 'level' => ILogger::FATAL,
  57. ],
  58. $context
  59. ));
  60. } else {
  61. $this->logger->emergency((string)$message, $context);
  62. }
  63. }
  64. /**
  65. * Action must be taken immediately.
  66. *
  67. * Example: Entire website down, database unavailable, etc. This should
  68. * trigger the SMS alerts and wake you up.
  69. *
  70. * @param $message
  71. * @param mixed[] $context
  72. */
  73. public function alert($message, array $context = []): void {
  74. if ($this->containsThrowable($context)) {
  75. $this->logger->logException($context['exception'], array_merge(
  76. [
  77. 'message' => (string)$message,
  78. 'level' => ILogger::ERROR,
  79. ],
  80. $context
  81. ));
  82. } else {
  83. $this->logger->alert((string)$message, $context);
  84. }
  85. }
  86. /**
  87. * Critical conditions.
  88. *
  89. * Example: Application component unavailable, unexpected exception.
  90. *
  91. * @param $message
  92. * @param mixed[] $context
  93. */
  94. public function critical($message, array $context = []): void {
  95. if ($this->containsThrowable($context)) {
  96. $this->logger->logException($context['exception'], array_merge(
  97. [
  98. 'message' => (string)$message,
  99. 'level' => ILogger::ERROR,
  100. ],
  101. $context
  102. ));
  103. } else {
  104. $this->logger->critical((string)$message, $context);
  105. }
  106. }
  107. /**
  108. * Runtime errors that do not require immediate action but should typically
  109. * be logged and monitored.
  110. *
  111. * @param $message
  112. * @param mixed[] $context
  113. */
  114. public function error($message, array $context = []): void {
  115. if ($this->containsThrowable($context)) {
  116. $this->logger->logException($context['exception'], array_merge(
  117. [
  118. 'message' => (string)$message,
  119. 'level' => ILogger::ERROR,
  120. ],
  121. $context
  122. ));
  123. } else {
  124. $this->logger->error((string)$message, $context);
  125. }
  126. }
  127. /**
  128. * Exceptional occurrences that are not errors.
  129. *
  130. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  131. * that are not necessarily wrong.
  132. *
  133. * @param $message
  134. * @param mixed[] $context
  135. */
  136. public function warning($message, array $context = []): void {
  137. if ($this->containsThrowable($context)) {
  138. $this->logger->logException($context['exception'], array_merge(
  139. [
  140. 'message' => (string)$message,
  141. 'level' => ILogger::WARN,
  142. ],
  143. $context
  144. ));
  145. } else {
  146. $this->logger->warning((string)$message, $context);
  147. }
  148. }
  149. /**
  150. * Normal but significant events.
  151. *
  152. * @param $message
  153. * @param mixed[] $context
  154. */
  155. public function notice($message, array $context = []): void {
  156. if ($this->containsThrowable($context)) {
  157. $this->logger->logException($context['exception'], array_merge(
  158. [
  159. 'message' => (string)$message,
  160. 'level' => ILogger::INFO,
  161. ],
  162. $context
  163. ));
  164. } else {
  165. $this->logger->notice((string)$message, $context);
  166. }
  167. }
  168. /**
  169. * Interesting events.
  170. *
  171. * Example: User logs in, SQL logs.
  172. *
  173. * @param $message
  174. * @param mixed[] $context
  175. */
  176. public function info($message, array $context = []): void {
  177. if ($this->containsThrowable($context)) {
  178. $this->logger->logException($context['exception'], array_merge(
  179. [
  180. 'message' => (string)$message,
  181. 'level' => ILogger::INFO,
  182. ],
  183. $context
  184. ));
  185. } else {
  186. $this->logger->info((string)$message, $context);
  187. }
  188. }
  189. /**
  190. * Detailed debug information.
  191. *
  192. * @param $message
  193. * @param mixed[] $context
  194. */
  195. public function debug($message, array $context = []): void {
  196. if ($this->containsThrowable($context)) {
  197. $this->logger->logException($context['exception'], array_merge(
  198. [
  199. 'message' => (string)$message,
  200. 'level' => ILogger::DEBUG,
  201. ],
  202. $context
  203. ));
  204. } else {
  205. $this->logger->debug((string)$message, $context);
  206. }
  207. }
  208. /**
  209. * Logs with an arbitrary level.
  210. *
  211. * @param mixed $level
  212. * @param $message
  213. * @param mixed[] $context
  214. *
  215. * @throws InvalidArgumentException
  216. */
  217. public function log($level, $message, array $context = []): void {
  218. if (!is_int($level) || $level < ILogger::DEBUG || $level > ILogger::FATAL) {
  219. throw new InvalidArgumentException('Nextcloud allows only integer log levels');
  220. }
  221. if ($this->containsThrowable($context)) {
  222. $this->logger->logException($context['exception'], array_merge(
  223. [
  224. 'message' => (string)$message,
  225. 'level' => $level,
  226. ],
  227. $context
  228. ));
  229. } else {
  230. $this->logger->log($level, (string)$message, $context);
  231. }
  232. }
  233. public function logData(string $message, array $data, array $context = []): void {
  234. $this->logger->logData($message, $data, $context);
  235. }
  236. }