PsrLoggerAdapter.php 5.9 KB

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