jelly2 /library/Jelly/Log/DefaultLogger.php

Language PHP Lines 131
MD5 Hash 8fbd2aa53029422e93b08f34ae8e9f0d Estimated Cost $1,705 (why?)
Repository https://bitbucket.org/nosen/jelly2 View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php namespace Jelly\Log;

/**
 * ログクラス
 * timestamp書式のカスタマイズと、エラー検知用のsyslog出力
 *
 * @author kobari
 */
class DefaultLogger extends \Zend_Log
{

    /**
     * timestampの書式
     */
    private $_dateFormat = 'c';
    /**
     * Syslogに ERR 以上のログを出力するかどうか
     **/
    private $_enableSyslogAlert = false;

    /**
     * Constructor
     *
     * @param Zend_Log_Writer_Abstract $writer
     * @param string $dateFormat Format of Date method
     */
    public function __construct(\Zend_Log_Writer_Abstract $writer = null, $dateFormat = null)
    {
        // 指定があればtimestampの書式を上書き
        if (!empty($dateFormat)) {
            $this->_dateFormat = $dateFormat;
        }
        parent::__construct($writer);
    }

    /**
     * Log a message at a priority
     *
     * @param  string   $message   Message to log
     * @param  integer  $priority  Priority of message
     * @return void
     * @throws \Zend_Log_Exception
     */
    public function log($message, $priority, $extras = null)
    {
        // デフォルトの日付表記が見辛いので日付書式を上書き
        parent::setEventItem('timestamp', date($this->_dateFormat));

        // ここで、messageが例外オブジェクトの場合にエラーメッセージの組み立てを行う
        // 呼び元がcatchされたブロックの場合は、例外オブジェクトを渡す
        // 呼び元がcatchされたブロックでない場合は、例外を生成して渡す
        if ($priority <= self::ERR && $this->_enableSyslogAlert) {
            $arr_messages = $this->buildExceptionMessages($message, $priority);
            foreach ($arr_messages as $arr_message_index => $arr_message) {
                syslog(
                    $priority,
                    '[' . 'REPORT:'.$arr_message['errorlevel'].']' .
                    ' ' . $arr_message['context'] .
                    ' ' . $arr_message['loggingid'] . ':' . $arr_message['subno'] .
                    ' ' . http_build_query($arr_message)
                );
            }
        }

        return parent::log($message, $priority, $extras);
    }

     /**
     * build exception message
     *
     * @param  mixed    $message   ExceptionObject or string
     * @param  integer  $priority  Priority
     * @return array
     * @throws \Zend_Log_Exception
     */
    public function buildExceptionMessages($message, $priority)
    {
        $arr_messages = array();
        $arr_message = array();

        $arr_message['priority'] = $priority;
        $arr_message['errorlevel'] = $this->_priorities[$priority];

        $arr_message['datetime'] = date("Y-m-d H:i:s");
        $arr_message['server_name'] = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '-';
        $arr_message['server_addr'] = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '-';

        $arr_message['loggingid'] = md5(uniqid($arr_message['server_addr'].':'));
        $arr_message['subno'] = 0;

        $arr_message['class'] = '-';
        $arr_message['file'] = '-';
        $arr_message['line'] = 0;
        $arr_message['message'] = '';
        $arr_message['context'] = md5($arr_message['file'] . ":" . $arr_message['line']);

        if ($message instanceof Exception) {
            do {
                $arr_message['class'] = get_class($message);
                $arr_message['file'] = $message->getFile();
                $arr_message['line'] = $message->getLine();
                $arr_message['message'] = $message->getMessage();
                $arr_message['context'] = md5($arr_message['file'] . ":" . $arr_message['line']);

                $arr_messages[] = $arr_message;
                $arr_message['subno']++;
            } while($message = $message->getPrevious());
        }
        elseif (is_string($message)) {
            $arr_message['message'] = $message;
            $arr_message['context'] = md5($arr_message['file'] . ":" . $arr_message['line']);

            $arr_messages[] = $arr_message;
        }
        else {
            $arr_message['context'] = md5($arr_message['file'] . ":" . $arr_message['line']);
            $arr_messages[] = $arr_message;
        }

        return $arr_messages;
    }
    
    /**
     * syslog へのアラート出力を有効にする
     **/
    public function enableSyslogAlert()
    {
        $this->_enableSyslogAlert = true;
    }
}
Back to Top