PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 0ms

/Log/Writer/ZendMonitor.php

https://bitbucket.org/bigstylee/zend-framework
PHP | 131 lines | 46 code | 10 blank | 75 comment | 7 complexity | 2d0e1050c1be8b3f303eb75774ff1c7b MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Log
 17 * @subpackage Writer
 18 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: ZendMonitor.php 24593 2012-01-05 20:35:02Z matthew $
 21 */
 22
 23/** Zend_Log_Writer_Abstract */
 24require_once 'Zend/Log/Writer/Abstract.php';
 25
 26/**
 27 * @category   Zend
 28 * @package    Zend_Log
 29 * @subpackage Writer
 30 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 31 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 32 * @version    $Id: ZendMonitor.php 24593 2012-01-05 20:35:02Z matthew $
 33 */
 34class Zend_Log_Writer_ZendMonitor extends Zend_Log_Writer_Abstract
 35{
 36    /**
 37     * Is Zend Monitor enabled?
 38     *
 39     * @var boolean
 40     */
 41    protected $_isEnabled = true;
 42
 43    /**
 44     * Is this for a Zend Server intance?
 45     *
 46     * @var boolean
 47     */
 48    protected $_isZendServer = false;
 49
 50    /**
 51     * @return void
 52     */
 53    public function __construct()
 54    {
 55        if (!function_exists('monitor_custom_event')) {
 56            $this->_isEnabled = false;
 57        }
 58        if (function_exists('zend_monitor_custom_event')) {
 59            $this->_isZendServer = true;
 60        }
 61    }
 62
 63    /**
 64     * Create a new instance of Zend_Log_Writer_ZendMonitor
 65     *
 66     * @param  array|Zend_Config $config
 67     * @return Zend_Log_Writer_ZendMonitor
 68     */
 69    static public function factory($config)
 70    {
 71        return new self();
 72    }
 73
 74    /**
 75     * Is logging to this writer enabled?
 76     *
 77     * If the Zend Monitor extension is not enabled, this log writer will
 78     * fail silently. You can query this method to determine if the log
 79     * writer is enabled.
 80     *
 81     * @return boolean
 82     */
 83    public function isEnabled()
 84    {
 85        return $this->_isEnabled;
 86    }
 87
 88    /**
 89     * Log a message to this writer.
 90     *
 91     * @param  array $event log data event
 92     * @return void
 93     */
 94    public function write($event)
 95    {
 96        if (!$this->isEnabled()) {
 97            return;
 98        }
 99
100        parent::write($event);
101    }
102
103    /**
104     * Write a message to the log.
105     *
106     * @param  array  $event log data event
107     * @return void
108     */
109    protected function _write($event)
110    {
111        $priority = $event['priority'];
112        $message  = $event['message'];
113        unset($event['priority'], $event['message']);
114
115        if (!empty($event)) {
116            if ($this->_isZendServer) {
117                // On Zend Server; third argument should be the event
118                zend_monitor_custom_event($priority, $message, $event);
119            } else {
120                // On Zend Platform; third argument is severity -- either
121                // 0 or 1 -- and fourth is optional (event)
122                // Severity is either 0 (normal) or 1 (severe); classifying
123                // notice, info, and debug as "normal", and all others as
124                // "severe"
125                monitor_custom_event($priority, $message, ($priority > 4) ? 0 : 1, $event);
126            }
127        } else {
128            monitor_custom_event($priority, $message);
129        }
130    }
131}