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

/simpus/vendor/yiisoft/yii2/log/Dispatcher.php

https://gitlab.com/isdzulqor/Slis-Dev
PHP | 201 lines | 79 code | 17 blank | 105 comment | 5 complexity | 3bfd83c419bc9d546be56f8326447236 MD5 | raw file
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\log;
  8. use Yii;
  9. use yii\base\Component;
  10. use yii\base\ErrorHandler;
  11. /**
  12. * Dispatcher manages a set of [[Target|log targets]].
  13. *
  14. * Dispatcher implements the [[dispatch()]]-method that forwards the log messages from a [[Logger]] to
  15. * the registered log [[targets]].
  16. *
  17. * An instance of Dispatcher is registered as a core application component and can be accessed using `Yii::$app->log`.
  18. *
  19. * You may configure the targets in application configuration, like the following:
  20. *
  21. * ```php
  22. * [
  23. * 'components' => [
  24. * 'log' => [
  25. * 'targets' => [
  26. * 'file' => [
  27. * 'class' => 'yii\log\FileTarget',
  28. * 'levels' => ['trace', 'info'],
  29. * 'categories' => ['yii\*'],
  30. * ],
  31. * 'email' => [
  32. * 'class' => 'yii\log\EmailTarget',
  33. * 'levels' => ['error', 'warning'],
  34. * 'message' => [
  35. * 'to' => 'admin@example.com',
  36. * ],
  37. * ],
  38. * ],
  39. * ],
  40. * ],
  41. * ]
  42. * ```
  43. *
  44. * Each log target can have a name and can be referenced via the [[targets]] property as follows:
  45. *
  46. * ```php
  47. * Yii::$app->log->targets['file']->enabled = false;
  48. * ```
  49. *
  50. * @property integer $flushInterval How many messages should be logged before they are sent to targets. This
  51. * method returns the value of [[Logger::flushInterval]].
  52. * @property Logger $logger The logger. If not set, [[\Yii::getLogger()]] will be used.
  53. * @property integer $traceLevel How many application call stacks should be logged together with each message.
  54. * This method returns the value of [[Logger::traceLevel]]. Defaults to 0.
  55. *
  56. * @author Qiang Xue <qiang.xue@gmail.com>
  57. * @since 2.0
  58. */
  59. class Dispatcher extends Component
  60. {
  61. /**
  62. * @var array|Target[] the log targets. Each array element represents a single [[Target|log target]] instance
  63. * or the configuration for creating the log target instance.
  64. */
  65. public $targets = [];
  66. /**
  67. * @var Logger the logger.
  68. */
  69. private $_logger;
  70. /**
  71. * @inheritdoc
  72. */
  73. public function __construct($config = [])
  74. {
  75. // ensure logger gets set before any other config option
  76. if (isset($config['logger'])) {
  77. $this->setLogger($config['logger']);
  78. unset($config['logger']);
  79. }
  80. // connect logger and dispatcher
  81. $this->getLogger();
  82. parent::__construct($config);
  83. }
  84. /**
  85. * @inheritdoc
  86. */
  87. public function init()
  88. {
  89. parent::init();
  90. foreach ($this->targets as $name => $target) {
  91. if (!$target instanceof Target) {
  92. $this->targets[$name] = Yii::createObject($target);
  93. }
  94. }
  95. }
  96. /**
  97. * Gets the connected logger.
  98. * If not set, [[\Yii::getLogger()]] will be used.
  99. * @property Logger the logger. If not set, [[\Yii::getLogger()]] will be used.
  100. * @return Logger the logger.
  101. */
  102. public function getLogger()
  103. {
  104. if ($this->_logger === null) {
  105. $this->setLogger(Yii::getLogger());
  106. }
  107. return $this->_logger;
  108. }
  109. /**
  110. * Sets the connected logger.
  111. * @param Logger $value the logger.
  112. */
  113. public function setLogger($value)
  114. {
  115. $this->_logger = $value;
  116. $this->_logger->dispatcher = $this;
  117. }
  118. /**
  119. * @return integer how many application call stacks should be logged together with each message.
  120. * This method returns the value of [[Logger::traceLevel]]. Defaults to 0.
  121. */
  122. public function getTraceLevel()
  123. {
  124. return $this->getLogger()->traceLevel;
  125. }
  126. /**
  127. * @param integer $value how many application call stacks should be logged together with each message.
  128. * This method will set the value of [[Logger::traceLevel]]. If the value is greater than 0,
  129. * at most that number of call stacks will be logged. Note that only application call stacks are counted.
  130. * Defaults to 0.
  131. */
  132. public function setTraceLevel($value)
  133. {
  134. $this->getLogger()->traceLevel = $value;
  135. }
  136. /**
  137. * @return integer how many messages should be logged before they are sent to targets.
  138. * This method returns the value of [[Logger::flushInterval]].
  139. */
  140. public function getFlushInterval()
  141. {
  142. return $this->getLogger()->flushInterval;
  143. }
  144. /**
  145. * @param integer $value how many messages should be logged before they are sent to targets.
  146. * This method will set the value of [[Logger::flushInterval]].
  147. * Defaults to 1000, meaning the [[Logger::flush()]] method will be invoked once every 1000 messages logged.
  148. * Set this property to be 0 if you don't want to flush messages until the application terminates.
  149. * This property mainly affects how much memory will be taken by the logged messages.
  150. * A smaller value means less memory, but will increase the execution time due to the overhead of [[Logger::flush()]].
  151. */
  152. public function setFlushInterval($value)
  153. {
  154. $this->getLogger()->flushInterval = $value;
  155. }
  156. /**
  157. * Dispatches the logged messages to [[targets]].
  158. * @param array $messages the logged messages
  159. * @param boolean $final whether this method is called at the end of the current application
  160. */
  161. public function dispatch($messages, $final)
  162. {
  163. $targetErrors = [];
  164. foreach ($this->targets as $target) {
  165. if ($target->enabled) {
  166. try {
  167. $target->collect($messages, $final);
  168. } catch (\Exception $e) {
  169. $target->enabled = false;
  170. $targetErrors[] = [
  171. 'Unable to send log via ' . get_class($target) . ': ' . ErrorHandler::convertExceptionToString($e),
  172. Logger::LEVEL_WARNING,
  173. __METHOD__,
  174. microtime(true),
  175. [],
  176. ];
  177. }
  178. }
  179. }
  180. if (!empty($targetErrors)) {
  181. $this->dispatch($targetErrors, true);
  182. }
  183. }
  184. }