SyslogTarget.php 1.56 KB
Newer Older
miramir committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Carsten Brandt committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
miramir committed
5 6 7 8 9 10
 * @license http://www.yiiframework.com/license/
 */

namespace yii\log;

use Yii;
11
use yii\helpers\VarDumper;
miramir committed
12 13

/**
14
 * SyslogTarget writes log to syslog.
miramir committed
15 16 17 18 19 20 21
 *
 * @author miramir <gmiramir@gmail.com>
 * @since 2.0
 */
class SyslogTarget extends Target
{
    /**
22
     * @var string syslog identity
miramir committed
23 24
     */
    public $identity;
25

miramir committed
26 27 28 29 30 31 32 33
    /**
     * @var integer syslog facility.
     */
    public $facility = LOG_SYSLOG;

    /**
     * @var array syslog levels
     */
34
    private $_syslogLevels = [
miramir committed
35 36 37 38 39 40
        Logger::LEVEL_TRACE => LOG_DEBUG,
        Logger::LEVEL_PROFILE_BEGIN => LOG_DEBUG,
        Logger::LEVEL_PROFILE_END => LOG_DEBUG,
        Logger::LEVEL_INFO => LOG_INFO,
        Logger::LEVEL_WARNING => LOG_WARNING,
        Logger::LEVEL_ERROR => LOG_ERR,
41
    ];
miramir committed
42

43

miramir committed
44
    /**
45
     * Writes log messages to syslog
miramir committed
46 47 48 49
     */
    public function export()
    {
        openlog($this->identity, LOG_ODELAY | LOG_PID, $this->facility);
50
        foreach ($this->messages as $message) {
51
            syslog($this->_syslogLevels[$message[1]], $this->formatMessage($message));
miramir committed
52 53 54 55 56 57 58 59 60 61 62 63
        }
        closelog();
    }

    /**
     * @inheritdoc
     */
    public function formatMessage($message)
    {
        list($text, $level, $category, $timestamp) = $message;
        $level = Logger::getLevelName($level);
        if (!is_string($text)) {
Carsten Brandt committed
64
            $text = VarDumper::export($text);
miramir committed
65 66
        }

67
        $prefix = $this->getMessagePrefix($message);
miramir committed
68 69 70
        return "{$prefix}[$level][$category] $text";
    }
}