PsrLoggerAdapter.php 6.5 KB

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