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 25 26 27
     */
    public $identity;
    /**
     * @var integer syslog facility.
     */
28
    public $facility = LOG_USER;
miramir committed
29 30 31 32

    /**
     * @var array syslog levels
     */
33
    private $_syslogLevels = [
miramir committed
34 35 36 37 38 39
        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,
40
    ];
miramir committed
41

42

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

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

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