PsrLoggerAdapter.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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\ILogger;
  27. use OCP\Log\IDataLogger;
  28. use Psr\Log\InvalidArgumentException;
  29. use Psr\Log\LoggerInterface;
  30. use Throwable;
  31. use function array_key_exists;
  32. use function array_merge;
  33. final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
  34. /** @var Log */
  35. private $logger;
  36. public function __construct(Log $logger) {
  37. $this->logger = $logger;
  38. }
  39. private function containsThrowable(array $context): bool {
  40. return array_key_exists('exception', $context) && $context['exception'] instanceof Throwable;
  41. }
  42. /**
  43. * System is unusable.
  44. *
  45. * @param string $message
  46. * @param array $context
  47. *
  48. * @return void
  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. * @param array $context
  71. *
  72. * @return void
  73. */
  74. public function alert($message, array $context = []) {
  75. if ($this->containsThrowable($context)) {
  76. $this->logger->logException($context['exception'], array_merge(
  77. [
  78. 'message' => $message,
  79. 'level' => ILogger::ERROR,
  80. ],
  81. $context
  82. ));
  83. } else {
  84. $this->logger->alert($message, $context);
  85. }
  86. }
  87. /**
  88. * Critical conditions.
  89. *
  90. * Example: Application component unavailable, unexpected exception.
  91. *
  92. * @param string $message
  93. * @param array $context
  94. *
  95. * @return void
  96. */
  97. public function critical($message, array $context = []) {
  98. if ($this->containsThrowable($context)) {
  99. $this->logger->logException($context['exception'], array_merge(
  100. [
  101. 'message' => $message,
  102. 'level' => ILogger::ERROR,
  103. ],
  104. $context
  105. ));
  106. } else {
  107. $this->logger->critical($message, $context);
  108. }
  109. }
  110. /**
  111. * Runtime errors that do not require immediate action but should typically
  112. * be logged and monitored.
  113. *
  114. * @param string $message
  115. * @param array $context
  116. *
  117. * @return void
  118. */
  119. public function error($message, array $context = []) {
  120. if ($this->containsThrowable($context)) {
  121. $this->logger->logException($context['exception'], array_merge(
  122. [
  123. 'message' => $message,
  124. 'level' => ILogger::ERROR,
  125. ],
  126. $context
  127. ));
  128. } else {
  129. $this->logger->error($message, $context);
  130. }
  131. }
  132. /**
  133. * Exceptional occurrences that are not errors.
  134. *
  135. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  136. * that are not necessarily wrong.
  137. *
  138. * @param string $message
  139. * @param array $context
  140. *
  141. * @return void
  142. */
  143. public function warning($message, array $context = []) {
  144. if ($this->containsThrowable($context)) {
  145. $this->logger->logException($context['exception'], array_merge(
  146. [
  147. 'message' => $message,
  148. 'level' => ILogger::WARN,
  149. ],
  150. $context
  151. ));
  152. } else {
  153. $this->logger->warning($message, $context);
  154. }
  155. }
  156. /**
  157. * Normal but significant events.
  158. *
  159. * @param string $message
  160. * @param array $context
  161. *
  162. * @return void
  163. */
  164. public function notice($message, array $context = []) {
  165. if ($this->containsThrowable($context)) {
  166. $this->logger->logException($context['exception'], array_merge(
  167. [
  168. 'message' => $message,
  169. 'level' => ILogger::INFO,
  170. ],
  171. $context
  172. ));
  173. } else {
  174. $this->logger->notice($message, $context);
  175. }
  176. }
  177. /**
  178. * Interesting events.
  179. *
  180. * Example: User logs in, SQL logs.
  181. *
  182. * @param string $message
  183. * @param array $context
  184. *
  185. * @return void
  186. */
  187. public function info($message, array $context = []) {
  188. if ($this->containsThrowable($context)) {
  189. $this->logger->logException($context['exception'], array_merge(
  190. [
  191. 'message' => $message,
  192. 'level' => ILogger::INFO,
  193. ],
  194. $context
  195. ));
  196. } else {
  197. $this->logger->info($message, $context);
  198. }
  199. }
  200. /**
  201. * Detailed debug information.
  202. *
  203. * @param string $message
  204. * @param array $context
  205. *
  206. * @return void
  207. */
  208. public function debug($message, array $context = []) {
  209. if ($this->containsThrowable($context)) {
  210. $this->logger->logException($context['exception'], array_merge(
  211. [
  212. 'message' => $message,
  213. 'level' => ILogger::DEBUG,
  214. ],
  215. $context
  216. ));
  217. } else {
  218. $this->logger->debug($message, $context);
  219. }
  220. }
  221. /**
  222. * Logs with an arbitrary level.
  223. *
  224. * @param mixed $level
  225. * @param string $message
  226. * @param array $context
  227. *
  228. * @return void
  229. *
  230. * @throws InvalidArgumentException
  231. */
  232. public function log($level, $message, array $context = []) {
  233. if (!is_int($level) || $level < ILogger::DEBUG || $level > ILogger::FATAL) {
  234. throw new InvalidArgumentException('Nextcloud allows only integer log levels');
  235. }
  236. if ($this->containsThrowable($context)) {
  237. $this->logger->logException($context['exception'], array_merge(
  238. [
  239. 'message' => $message,
  240. 'level' => $level,
  241. ],
  242. $context
  243. ));
  244. } else {
  245. $this->logger->log($level, $message, $context);
  246. }
  247. }
  248. public function logData(string $message, array $data, array $context = []): void {
  249. $this->logger->logData($message, $data, $context);
  250. }
  251. }