|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
-
-
- namespace yii\log;
-
- use Yii;
- use yii\base\Component;
- use yii\base\ErrorHandler;
-
-
- class Dispatcher extends Component
- {
-
-
- public $targets = [];
-
-
-
- private $_logger;
-
-
-
-
- public function __construct($config = [])
- {
-
- if (isset($config['logger'])) {
- $this->setLogger($config['logger']);
- unset($config['logger']);
- }
-
- $this->getLogger();
-
- parent::__construct($config);
- }
-
-
-
- public function init()
- {
- parent::init();
-
- foreach ($this->targets as $name => $target) {
- if (!$target instanceof Target) {
- $this->targets[$name] = Yii::createObject($target);
- }
- }
- }
-
-
-
- public function getLogger()
- {
- if ($this->_logger === null) {
- $this->setLogger(Yii::getLogger());
- }
- return $this->_logger;
- }
-
-
-
- public function setLogger($value)
- {
- if (is_string($value) || is_array($value)) {
- $value = Yii::createObject($value);
- }
- $this->_logger = $value;
- $this->_logger->dispatcher = $this;
- }
-
-
-
- public function getTraceLevel()
- {
- return $this->getLogger()->traceLevel;
- }
-
-
-
- public function setTraceLevel($value)
- {
- $this->getLogger()->traceLevel = $value;
- }
-
-
-
- public function getFlushInterval()
- {
- return $this->getLogger()->flushInterval;
- }
-
-
-
- public function setFlushInterval($value)
- {
- $this->getLogger()->flushInterval = $value;
- }
-
-
-
- public function dispatch($messages, $final)
- {
- $targetErrors = [];
- foreach ($this->targets as $target) {
- if ($target->enabled) {
- try {
- $target->collect($messages, $final);
- } catch (\Exception $e) {
- $target->enabled = false;
- $targetErrors[] = [
- 'Unable to send log via ' . get_class($target) . ': ' . ErrorHandler::convertExceptionToString($e),
- Logger::LEVEL_WARNING,
- __METHOD__,
- microtime(true),
- [],
- ];
- }
- }
- }
-
- if (!empty($targetErrors)) {
- $this->dispatch($targetErrors, true);
- }
- }
- }
|