Dispatcher.php 5.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\log;

use Yii;
use yii\base\Component;

/**
 * Dispatcher manages a set of [[Target|log targets]].
 *
16
 * Dispatcher implements the [[dispatch()]]-method that forwards the log messages from a [[Logger]] to
17 18
 * the registered log [[targets]].
 *
19
 * An instance of Dispatcher is registered as a core application component and can be accessed using `Yii::$app->log`.
20 21 22
 *
 * You may configure the targets in application configuration, like the following:
 *
23
 * ```php
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 * [
 *     'components' => [
 *         'log' => [
 *             'targets' => [
 *                 'file' => [
 *                     'class' => 'yii\log\FileTarget',
 *                     'levels' => ['trace', 'info'],
 *                     'categories' => ['yii\*'],
 *                 ],
 *                 'email' => [
 *                     'class' => 'yii\log\EmailTarget',
 *                     'levels' => ['error', 'warning'],
 *                     'message' => [
 *                         'to' => 'admin@example.com',
 *                     ],
 *                 ],
 *             ],
 *         ],
 *     ],
 * ]
44
 * ```
45
 *
46
 * Each log target can have a name and can be referenced via the [[targets]] property as follows:
47
 *
48
 * ```php
49
 * Yii::$app->log->targets['file']->enabled = false;
50
 * ```
51
 *
52 53 54 55 56 57
 * @property integer $flushInterval How many messages should be logged before they are sent to targets. This
 * method returns the value of [[Logger::flushInterval]].
 * @property Logger $logger The logger. If not set, [[\Yii::getLogger()]] will be used.
 * @property integer $traceLevel How many application call stacks should be logged together with each message.
 * This method returns the value of [[Logger::traceLevel]]. Defaults to 0.
 *
58 59 60 61 62 63 64 65 66 67
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Dispatcher extends Component
{
    /**
     * @var array|Target[] the log targets. Each array element represents a single [[Target|log target]] instance
     * or the configuration for creating the log target instance.
     */
    public $targets = [];
68

Qiang Xue committed
69
    /**
70
     * @var Logger the logger.
Qiang Xue committed
71
     */
72 73
    private $_logger;

74

75 76 77 78 79
    /**
     * @inheritdoc
     */
    public function __construct($config = [])
    {
80
        // ensure logger gets set before any other config option
81
        if (isset($config['logger'])) {
82
            $this->setLogger($config['logger']);
83 84 85 86 87 88 89 90
            unset($config['logger']);
        }
        // connect logger and dispatcher
        $this->getLogger();

        parent::__construct($config);
    }

91
    /**
92
     * @inheritdoc
93 94 95 96 97 98 99 100 101 102
     */
    public function init()
    {
        parent::init();

        foreach ($this->targets as $name => $target) {
            if (!$target instanceof Target) {
                $this->targets[$name] = Yii::createObject($target);
            }
        }
103 104 105 106 107 108 109 110 111 112 113
    }

    /**
     * Gets the connected logger.
     * If not set, [[\Yii::getLogger()]] will be used.
     * @property Logger the logger. If not set, [[\Yii::getLogger()]] will be used.
     * @return Logger the logger.
     */
    public function getLogger()
    {
        if ($this->_logger === null) {
114
            $this->setLogger(Yii::getLogger());
Qiang Xue committed
115
        }
116 117 118 119 120 121 122 123 124 125
        return $this->_logger;
    }

    /**
     * Sets the connected logger.
     * @param Logger $value the logger.
     */
    public function setLogger($value)
    {
        $this->_logger = $value;
126
        $this->_logger->dispatcher = $this;
127 128 129 130 131 132 133 134
    }

    /**
     * @return integer how many application call stacks should be logged together with each message.
     * This method returns the value of [[Logger::traceLevel]]. Defaults to 0.
     */
    public function getTraceLevel()
    {
135
        return $this->getLogger()->traceLevel;
136 137 138 139 140 141 142 143 144 145
    }

    /**
     * @param integer $value how many application call stacks should be logged together with each message.
     * This method will set the value of [[Logger::traceLevel]]. If the value is greater than 0,
     * at most that number of call stacks will be logged. Note that only application call stacks are counted.
     * Defaults to 0.
     */
    public function setTraceLevel($value)
    {
146
        $this->getLogger()->traceLevel = $value;
147 148 149 150 151 152 153 154
    }

    /**
     * @return integer how many messages should be logged before they are sent to targets.
     * This method returns the value of [[Logger::flushInterval]].
     */
    public function getFlushInterval()
    {
155
        return $this->getLogger()->flushInterval;
156 157 158 159 160 161 162 163 164 165 166 167
    }

    /**
     * @param integer $value how many messages should be logged before they are sent to targets.
     * This method will set the value of [[Logger::flushInterval]].
     * Defaults to 1000, meaning the [[Logger::flush()]] method will be invoked once every 1000 messages logged.
     * Set this property to be 0 if you don't want to flush messages until the application terminates.
     * This property mainly affects how much memory will be taken by the logged messages.
     * A smaller value means less memory, but will increase the execution time due to the overhead of [[Logger::flush()]].
     */
    public function setFlushInterval($value)
    {
168
        $this->getLogger()->flushInterval = $value;
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    }

    /**
     * Dispatches the logged messages to [[targets]].
     * @param array $messages the logged messages
     * @param boolean $final whether this method is called at the end of the current application
     */
    public function dispatch($messages, $final)
    {
        foreach ($this->targets as $target) {
            if ($target->enabled) {
                $target->collect($messages, $final);
            }
        }
    }
}