PageRenderTime 35ms CodeModel.GetById 26ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Jelly/Log/DefaultLogger.php

https://bitbucket.org/nosen/jelly2
PHP | 130 lines | 72 code | 16 blank | 42 comment | 5 complexity | 8fbd2aa53029422e93b08f34ae8e9f0d MD5 | raw file
  1<?php namespace Jelly\Log;
  2
  3/**
  4 * ログクラス
  5 * timestamp書式のカスタマイズと、エラー検知用のsyslog出力
  6 *
  7 * @author kobari
  8 */
  9class DefaultLogger extends \Zend_Log
 10{
 11
 12    /**
 13     * timestampの書式
 14     */
 15    private $_dateFormat = 'c';
 16    /**
 17     * Syslogに ERR 以上のログを出力するかどうか
 18     **/
 19    private $_enableSyslogAlert = false;
 20
 21    /**
 22     * Constructor
 23     *
 24     * @param Zend_Log_Writer_Abstract $writer
 25     * @param string $dateFormat Format of Date method
 26     */
 27    public function __construct(\Zend_Log_Writer_Abstract $writer = null, $dateFormat = null)
 28    {
 29        // 指定があればtimestampの書式を上書き
 30        if (!empty($dateFormat)) {
 31            $this->_dateFormat = $dateFormat;
 32        }
 33        parent::__construct($writer);
 34    }
 35
 36    /**
 37     * Log a message at a priority
 38     *
 39     * @param  string   $message   Message to log
 40     * @param  integer  $priority  Priority of message
 41     * @return void
 42     * @throws \Zend_Log_Exception
 43     */
 44    public function log($message, $priority, $extras = null)
 45    {
 46        // デフォルトの日付表記が見辛いので日付書式を上書き
 47        parent::setEventItem('timestamp', date($this->_dateFormat));
 48
 49        // ここで、messageが例外オブジェクトの場合にエラーメッセージの組み立てを行う
 50        // 呼び元がcatchされたブロックの場合は、例外オブジェクトを渡す
 51        // 呼び元がcatchされたブロックでない場合は、例外を生成して渡す
 52        if ($priority <= self::ERR && $this->_enableSyslogAlert) {
 53            $arr_messages = $this->buildExceptionMessages($message, $priority);
 54            foreach ($arr_messages as $arr_message_index => $arr_message) {
 55                syslog(
 56                    $priority,
 57                    '[' . 'REPORT:'.$arr_message['errorlevel'].']' .
 58                    ' ' . $arr_message['context'] .
 59                    ' ' . $arr_message['loggingid'] . ':' . $arr_message['subno'] .
 60                    ' ' . http_build_query($arr_message)
 61                );
 62            }
 63        }
 64
 65        return parent::log($message, $priority, $extras);
 66    }
 67
 68     /**
 69     * build exception message
 70     *
 71     * @param  mixed    $message   ExceptionObject or string
 72     * @param  integer  $priority  Priority
 73     * @return array
 74     * @throws \Zend_Log_Exception
 75     */
 76    public function buildExceptionMessages($message, $priority)
 77    {
 78        $arr_messages = array();
 79        $arr_message = array();
 80
 81        $arr_message['priority'] = $priority;
 82        $arr_message['errorlevel'] = $this->_priorities[$priority];
 83
 84        $arr_message['datetime'] = date("Y-m-d H:i:s");
 85        $arr_message['server_name'] = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '-';
 86        $arr_message['server_addr'] = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '-';
 87
 88        $arr_message['loggingid'] = md5(uniqid($arr_message['server_addr'].':'));
 89        $arr_message['subno'] = 0;
 90
 91        $arr_message['class'] = '-';
 92        $arr_message['file'] = '-';
 93        $arr_message['line'] = 0;
 94        $arr_message['message'] = '';
 95        $arr_message['context'] = md5($arr_message['file'] . ":" . $arr_message['line']);
 96
 97        if ($message instanceof Exception) {
 98            do {
 99                $arr_message['class'] = get_class($message);
100                $arr_message['file'] = $message->getFile();
101                $arr_message['line'] = $message->getLine();
102                $arr_message['message'] = $message->getMessage();
103                $arr_message['context'] = md5($arr_message['file'] . ":" . $arr_message['line']);
104
105                $arr_messages[] = $arr_message;
106                $arr_message['subno']++;
107            } while($message = $message->getPrevious());
108        }
109        elseif (is_string($message)) {
110            $arr_message['message'] = $message;
111            $arr_message['context'] = md5($arr_message['file'] . ":" . $arr_message['line']);
112
113            $arr_messages[] = $arr_message;
114        }
115        else {
116            $arr_message['context'] = md5($arr_message['file'] . ":" . $arr_message['line']);
117            $arr_messages[] = $arr_message;
118        }
119
120        return $arr_messages;
121    }
122    
123    /**
124     * syslog へのアラート出力を有効にする
125     **/
126    public function enableSyslogAlert()
127    {
128        $this->_enableSyslogAlert = true;
129    }
130}