PageRenderTime 36ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/mailpoet/vendor-prefixed/monolog/monolog/src/Monolog/Logger.php

https://gitlab.com/remyvianne/krowkaramel
PHP | 297 lines | 293 code | 0 blank | 4 comment | 23 complexity | 3f4cf10cab7000aee21afa03677ed4da MD5 | raw file
  1. <?php
  2. namespace MailPoetVendor\Monolog;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoetVendor\Monolog\Handler\HandlerInterface;
  5. use MailPoetVendor\Monolog\Handler\StreamHandler;
  6. use MailPoetVendor\Psr\Log\LoggerInterface;
  7. use MailPoetVendor\Psr\Log\InvalidArgumentException;
  8. use Exception;
  9. class Logger implements LoggerInterface, ResettableInterface
  10. {
  11. const DEBUG = 100;
  12. const INFO = 200;
  13. const NOTICE = 250;
  14. const WARNING = 300;
  15. const ERROR = 400;
  16. const CRITICAL = 500;
  17. const ALERT = 550;
  18. const EMERGENCY = 600;
  19. const API = 1;
  20. protected static $levels = array(self::DEBUG => 'DEBUG', self::INFO => 'INFO', self::NOTICE => 'NOTICE', self::WARNING => 'WARNING', self::ERROR => 'ERROR', self::CRITICAL => 'CRITICAL', self::ALERT => 'ALERT', self::EMERGENCY => 'EMERGENCY');
  21. protected static $timezone;
  22. protected $name;
  23. protected $handlers;
  24. protected $processors;
  25. protected $microsecondTimestamps = \true;
  26. protected $exceptionHandler;
  27. public function __construct($name, array $handlers = array(), array $processors = array())
  28. {
  29. $this->name = $name;
  30. $this->setHandlers($handlers);
  31. $this->processors = $processors;
  32. }
  33. public function getName()
  34. {
  35. return $this->name;
  36. }
  37. public function withName($name)
  38. {
  39. $new = clone $this;
  40. $new->name = $name;
  41. return $new;
  42. }
  43. public function pushHandler(HandlerInterface $handler)
  44. {
  45. \array_unshift($this->handlers, $handler);
  46. return $this;
  47. }
  48. public function popHandler()
  49. {
  50. if (!$this->handlers) {
  51. throw new \LogicException('You tried to pop from an empty handler stack.');
  52. }
  53. return \array_shift($this->handlers);
  54. }
  55. public function setHandlers(array $handlers)
  56. {
  57. $this->handlers = array();
  58. foreach (\array_reverse($handlers) as $handler) {
  59. $this->pushHandler($handler);
  60. }
  61. return $this;
  62. }
  63. public function getHandlers()
  64. {
  65. return $this->handlers;
  66. }
  67. public function pushProcessor($callback)
  68. {
  69. if (!\is_callable($callback)) {
  70. throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), ' . \var_export($callback, \true) . ' given');
  71. }
  72. \array_unshift($this->processors, $callback);
  73. return $this;
  74. }
  75. public function popProcessor()
  76. {
  77. if (!$this->processors) {
  78. throw new \LogicException('You tried to pop from an empty processor stack.');
  79. }
  80. return \array_shift($this->processors);
  81. }
  82. public function getProcessors()
  83. {
  84. return $this->processors;
  85. }
  86. public function useMicrosecondTimestamps($micro)
  87. {
  88. $this->microsecondTimestamps = (bool) $micro;
  89. }
  90. public function addRecord($level, $message, array $context = array())
  91. {
  92. if (!$this->handlers) {
  93. $this->pushHandler(new StreamHandler('php://stderr', static::DEBUG));
  94. }
  95. $levelName = static::getLevelName($level);
  96. // check if any handler will handle this message so we can return early and save cycles
  97. $handlerKey = null;
  98. \reset($this->handlers);
  99. while ($handler = \current($this->handlers)) {
  100. if ($handler->isHandling(array('level' => $level))) {
  101. $handlerKey = \key($this->handlers);
  102. break;
  103. }
  104. \next($this->handlers);
  105. }
  106. if (null === $handlerKey) {
  107. return \false;
  108. }
  109. if (!static::$timezone) {
  110. static::$timezone = new \DateTimeZone(\date_default_timezone_get() ?: 'UTC');
  111. }
  112. // php7.1+ always has microseconds enabled, so we do not need this hack
  113. if ($this->microsecondTimestamps && \PHP_VERSION_ID < 70100) {
  114. $ts = \DateTime::createFromFormat('U.u', \sprintf('%.6F', \microtime(\true)), static::$timezone);
  115. } else {
  116. $ts = new \DateTime('now', static::$timezone);
  117. }
  118. $ts->setTimezone(static::$timezone);
  119. $record = array('message' => (string) $message, 'context' => $context, 'level' => $level, 'level_name' => $levelName, 'channel' => $this->name, 'datetime' => $ts, 'extra' => array());
  120. try {
  121. foreach ($this->processors as $processor) {
  122. $record = \call_user_func($processor, $record);
  123. }
  124. while ($handler = \current($this->handlers)) {
  125. if (\true === $handler->handle($record)) {
  126. break;
  127. }
  128. \next($this->handlers);
  129. }
  130. } catch (Exception $e) {
  131. $this->handleException($e, $record);
  132. }
  133. return \true;
  134. }
  135. public function close()
  136. {
  137. foreach ($this->handlers as $handler) {
  138. if (\method_exists($handler, 'close')) {
  139. $handler->close();
  140. }
  141. }
  142. }
  143. public function reset()
  144. {
  145. foreach ($this->handlers as $handler) {
  146. if ($handler instanceof ResettableInterface) {
  147. $handler->reset();
  148. }
  149. }
  150. foreach ($this->processors as $processor) {
  151. if ($processor instanceof ResettableInterface) {
  152. $processor->reset();
  153. }
  154. }
  155. }
  156. public function addDebug($message, array $context = array())
  157. {
  158. return $this->addRecord(static::DEBUG, $message, $context);
  159. }
  160. public function addInfo($message, array $context = array())
  161. {
  162. return $this->addRecord(static::INFO, $message, $context);
  163. }
  164. public function addNotice($message, array $context = array())
  165. {
  166. return $this->addRecord(static::NOTICE, $message, $context);
  167. }
  168. public function addWarning($message, array $context = array())
  169. {
  170. return $this->addRecord(static::WARNING, $message, $context);
  171. }
  172. public function addError($message, array $context = array())
  173. {
  174. return $this->addRecord(static::ERROR, $message, $context);
  175. }
  176. public function addCritical($message, array $context = array())
  177. {
  178. return $this->addRecord(static::CRITICAL, $message, $context);
  179. }
  180. public function addAlert($message, array $context = array())
  181. {
  182. return $this->addRecord(static::ALERT, $message, $context);
  183. }
  184. public function addEmergency($message, array $context = array())
  185. {
  186. return $this->addRecord(static::EMERGENCY, $message, $context);
  187. }
  188. public static function getLevels()
  189. {
  190. return \array_flip(static::$levels);
  191. }
  192. public static function getLevelName($level)
  193. {
  194. if (!isset(static::$levels[$level])) {
  195. throw new InvalidArgumentException('Level "' . $level . '" is not defined, use one of: ' . \implode(', ', \array_keys(static::$levels)));
  196. }
  197. return static::$levels[$level];
  198. }
  199. public static function toMonologLevel($level)
  200. {
  201. if (\is_string($level)) {
  202. // Contains chars of all log levels and avoids using strtoupper() which may have
  203. // strange results depending on locale (for example, "i" will become "İ")
  204. $upper = \strtr($level, 'abcdefgilmnortuwy', 'ABCDEFGILMNORTUWY');
  205. if (\defined(__CLASS__ . '::' . $upper)) {
  206. return \constant(__CLASS__ . '::' . $upper);
  207. }
  208. }
  209. return $level;
  210. }
  211. public function isHandling($level)
  212. {
  213. $record = array('level' => $level);
  214. foreach ($this->handlers as $handler) {
  215. if ($handler->isHandling($record)) {
  216. return \true;
  217. }
  218. }
  219. return \false;
  220. }
  221. public function setExceptionHandler($callback)
  222. {
  223. if (!\is_callable($callback)) {
  224. throw new \InvalidArgumentException('Exception handler must be valid callable (callback or object with an __invoke method), ' . \var_export($callback, \true) . ' given');
  225. }
  226. $this->exceptionHandler = $callback;
  227. return $this;
  228. }
  229. public function getExceptionHandler()
  230. {
  231. return $this->exceptionHandler;
  232. }
  233. protected function handleException(Exception $e, array $record)
  234. {
  235. if (!$this->exceptionHandler) {
  236. throw $e;
  237. }
  238. \call_user_func($this->exceptionHandler, $e, $record);
  239. }
  240. public function log($level, $message, array $context = array())
  241. {
  242. $level = static::toMonologLevel($level);
  243. return $this->addRecord($level, $message, $context);
  244. }
  245. public function debug($message, array $context = array())
  246. {
  247. return $this->addRecord(static::DEBUG, $message, $context);
  248. }
  249. public function info($message, array $context = array())
  250. {
  251. return $this->addRecord(static::INFO, $message, $context);
  252. }
  253. public function notice($message, array $context = array())
  254. {
  255. return $this->addRecord(static::NOTICE, $message, $context);
  256. }
  257. public function warn($message, array $context = array())
  258. {
  259. return $this->addRecord(static::WARNING, $message, $context);
  260. }
  261. public function warning($message, array $context = array())
  262. {
  263. return $this->addRecord(static::WARNING, $message, $context);
  264. }
  265. public function err($message, array $context = array())
  266. {
  267. return $this->addRecord(static::ERROR, $message, $context);
  268. }
  269. public function error($message, array $context = array())
  270. {
  271. return $this->addRecord(static::ERROR, $message, $context);
  272. }
  273. public function crit($message, array $context = array())
  274. {
  275. return $this->addRecord(static::CRITICAL, $message, $context);
  276. }
  277. public function critical($message, array $context = array())
  278. {
  279. return $this->addRecord(static::CRITICAL, $message, $context);
  280. }
  281. public function alert($message, array $context = array())
  282. {
  283. return $this->addRecord(static::ALERT, $message, $context);
  284. }
  285. public function emerg($message, array $context = array())
  286. {
  287. return $this->addRecord(static::EMERGENCY, $message, $context);
  288. }
  289. public function emergency($message, array $context = array())
  290. {
  291. return $this->addRecord(static::EMERGENCY, $message, $context);
  292. }
  293. public static function setTimezone(\DateTimeZone $tz)
  294. {
  295. self::$timezone = $tz;
  296. }
  297. }