[795] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * $Header: /repository/pear/Log/Log/error_log.php,v 1.9 2008/09/20 19:36:41 jon Exp $ |
---|
| 4 | * |
---|
| 5 | * @version $Revision: 1.9 $ |
---|
| 6 | * @package Log |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /** |
---|
| 10 | * The Log_error_log class is a concrete implementation of the Log abstract |
---|
| 11 | * class that logs messages using PHP's error_log() function. |
---|
| 12 | * |
---|
| 13 | * @author Jon Parise <jon@php.net> |
---|
| 14 | * @since Log 1.7.0 |
---|
| 15 | * @package Log |
---|
| 16 | * |
---|
| 17 | * @example error_log.php Using the error_log handler. |
---|
| 18 | */ |
---|
| 19 | class Log_error_log extends Log |
---|
| 20 | { |
---|
| 21 | /** |
---|
| 22 | * The error_log() log type. |
---|
| 23 | * @var integer |
---|
| 24 | * @access private |
---|
| 25 | */ |
---|
| 26 | var $_type = PEAR_LOG_TYPE_SYSTEM; |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * The type-specific destination value. |
---|
| 30 | * @var string |
---|
| 31 | * @access private |
---|
| 32 | */ |
---|
| 33 | var $_destination = ''; |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * Additional headers to pass to the mail() function when the |
---|
| 37 | * PEAR_LOG_TYPE_MAIL type is used. |
---|
| 38 | * @var string |
---|
| 39 | * @access private |
---|
| 40 | */ |
---|
| 41 | var $_extra_headers = ''; |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * String containing the format of a log line. |
---|
| 45 | * @var string |
---|
| 46 | * @access private |
---|
| 47 | */ |
---|
| 48 | var $_lineFormat = '%2$s: %4$s'; |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * String containing the timestamp format. It will be passed directly to |
---|
| 52 | * strftime(). Note that the timestamp string will generated using the |
---|
| 53 | * current locale. |
---|
| 54 | * @var string |
---|
| 55 | * @access private |
---|
| 56 | */ |
---|
| 57 | var $_timeFormat = '%b %d %H:%M:%S'; |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * Constructs a new Log_error_log object. |
---|
| 61 | * |
---|
| 62 | * @param string $name Ignored. |
---|
| 63 | * @param string $ident The identity string. |
---|
| 64 | * @param array $conf The configuration array. |
---|
| 65 | * @param int $level Log messages up to and including this level. |
---|
| 66 | * @access public |
---|
| 67 | */ |
---|
| 68 | function Log_error_log($name, $ident = '', $conf = array(), |
---|
| 69 | $level = PEAR_LOG_DEBUG) |
---|
| 70 | { |
---|
| 71 | $this->_id = md5(microtime()); |
---|
| 72 | $this->_type = $name; |
---|
| 73 | $this->_ident = $ident; |
---|
| 74 | $this->_mask = Log::UPTO($level); |
---|
| 75 | |
---|
| 76 | if (!empty($conf['destination'])) { |
---|
| 77 | $this->_destination = $conf['destination']; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | if (!empty($conf['extra_headers'])) { |
---|
| 81 | $this->_extra_headers = $conf['extra_headers']; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | if (!empty($conf['lineFormat'])) { |
---|
| 85 | $this->_lineFormat = str_replace(array_keys($this->_formatMap), |
---|
| 86 | array_values($this->_formatMap), |
---|
| 87 | $conf['lineFormat']); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | if (!empty($conf['timeFormat'])) { |
---|
| 91 | $this->_timeFormat = $conf['timeFormat']; |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * Opens the handler. |
---|
| 97 | * |
---|
| 98 | * @access public |
---|
| 99 | * @since Log 1.9.6 |
---|
| 100 | */ |
---|
| 101 | function open() |
---|
| 102 | { |
---|
| 103 | $this->_opened = true; |
---|
| 104 | return true; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | * Closes the handler. |
---|
| 109 | * |
---|
| 110 | * @access public |
---|
| 111 | * @since Log 1.9.6 |
---|
| 112 | */ |
---|
| 113 | function close() |
---|
| 114 | { |
---|
| 115 | $this->_opened = false; |
---|
| 116 | return true; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | /** |
---|
| 120 | * Logs $message using PHP's error_log() function. The message is also |
---|
| 121 | * passed along to any Log_observer instances that are observing this Log. |
---|
| 122 | * |
---|
| 123 | * @param mixed $message String or object containing the message to log. |
---|
| 124 | * @param string $priority The priority of the message. Valid |
---|
| 125 | * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT, |
---|
| 126 | * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING, |
---|
| 127 | * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG. |
---|
| 128 | * @return boolean True on success or false on failure. |
---|
| 129 | * @access public |
---|
| 130 | */ |
---|
| 131 | function log($message, $priority = null) |
---|
| 132 | { |
---|
| 133 | /* If a priority hasn't been specified, use the default value. */ |
---|
| 134 | if ($priority === null) { |
---|
| 135 | $priority = $this->_priority; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /* Abort early if the priority is above the maximum logging level. */ |
---|
| 139 | if (!$this->_isMasked($priority)) { |
---|
| 140 | return false; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | /* Extract the string representation of the message. */ |
---|
| 144 | $message = $this->_extractMessage($message); |
---|
| 145 | |
---|
| 146 | /* Build the string containing the complete log line. */ |
---|
| 147 | $line = $this->_format($this->_lineFormat, |
---|
| 148 | strftime($this->_timeFormat), |
---|
| 149 | $priority, $message); |
---|
| 150 | |
---|
| 151 | /* Pass the log line and parameters to the error_log() function. */ |
---|
| 152 | $success = error_log($line, $this->_type, $this->_destination, |
---|
| 153 | $this->_extra_headers); |
---|
| 154 | |
---|
| 155 | $this->_announce(array('priority' => $priority, 'message' => $message)); |
---|
| 156 | |
---|
| 157 | return $success; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | } |
---|