[18220] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
| 4 | * contributor license agreements. See the NOTICE file distributed with |
---|
| 5 | * this work for additional information regarding copyright ownership. |
---|
| 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
| 7 | * (the "License"); you may not use this file except in compliance with |
---|
| 8 | * the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | * |
---|
| 18 | * |
---|
| 19 | * @package log4php |
---|
| 20 | * @subpackage appenders |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * @ignore |
---|
| 25 | */ |
---|
| 26 | if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..'); |
---|
| 27 | |
---|
| 28 | require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php'); |
---|
| 29 | require_once(LOG4PHP_DIR . '/LoggerLevel.php'); |
---|
| 30 | require_once(LOG4PHP_DIR . '/LoggerLog.php'); |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * Log events using php {@link PHP_MANUAL#syslog} function. |
---|
| 34 | * |
---|
| 35 | * Levels are mapped as follows: |
---|
| 36 | * - <b>level >= FATAL</b> to LOG_ALERT |
---|
| 37 | * - <b>FATAL > level >= ERROR</b> to LOG_ERR |
---|
| 38 | * - <b>ERROR > level >= WARN</b> to LOG_WARNING |
---|
| 39 | * - <b>WARN > level >= INFO</b> to LOG_INFO |
---|
| 40 | * - <b>INFO > level >= DEBUG</b> to LOG_DEBUG |
---|
| 41 | * |
---|
| 42 | * @author VxR <vxr@vxr.it> |
---|
| 43 | * @version $Revision: 635069 $ |
---|
| 44 | * @package log4php |
---|
| 45 | * @subpackage appenders |
---|
| 46 | */ |
---|
| 47 | class LoggerAppenderSyslog extends LoggerAppenderSkeleton { |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * The ident string is added to each message. Typically the name of your application. |
---|
| 51 | * |
---|
| 52 | * @var string Ident for your application |
---|
| 53 | */ |
---|
| 54 | private $_ident = "Log4PHP Syslog-Event"; |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * The priority parameter value indicates the level of importance of the message. |
---|
| 58 | * It is passed on to the Syslog daemon. |
---|
| 59 | * |
---|
| 60 | * @var int Syslog priority |
---|
| 61 | */ |
---|
| 62 | private $_priority; |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * The option used when generating a log message. |
---|
| 66 | * It is passed on to the Syslog daemon. |
---|
| 67 | * |
---|
| 68 | * @var int Syslog priority |
---|
| 69 | */ |
---|
| 70 | private $_option; |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * The facility value indicates the source of the message. |
---|
| 74 | * It is passed on to the Syslog daemon. |
---|
| 75 | * |
---|
| 76 | * @var const int Syslog facility |
---|
| 77 | */ |
---|
| 78 | private $_facility; |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * If it is necessary to define logging priority in the .properties-file, |
---|
| 82 | * set this variable to "true". |
---|
| 83 | * |
---|
| 84 | * @var const int value indicating whether the priority of the message is defined in the .properties-file |
---|
| 85 | * (or properties-array) |
---|
| 86 | */ |
---|
| 87 | private $_overridePriority; |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * Set the ident of the syslog message. |
---|
| 91 | * |
---|
| 92 | * @param string Ident |
---|
| 93 | */ |
---|
| 94 | public function setIdent($ident) { |
---|
| 95 | $this->_ident = $ident; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * Set the priority value for the syslog message. |
---|
| 100 | * |
---|
| 101 | * @param const int Priority |
---|
| 102 | */ |
---|
| 103 | public function setPriority($priority) { |
---|
| 104 | $this->_priority = $priority; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * Set the facility value for the syslog message. |
---|
| 110 | * |
---|
| 111 | * @param const int Facility |
---|
| 112 | */ |
---|
| 113 | public function setFacility($facility) { |
---|
| 114 | $this->_facility = $facility; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * If the priority of the message to be sent can be defined by a value in the properties-file, |
---|
| 119 | * set parameter value to "true". |
---|
| 120 | * |
---|
| 121 | * @param bool Override priority |
---|
| 122 | */ |
---|
| 123 | public function setOverridePriority($overridePriority) { |
---|
| 124 | $this->_overridePriority = $overridePriority; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * Set the option value for the syslog message. |
---|
| 129 | * This value is used as a parameter for php openlog() |
---|
| 130 | * and passed on to the syslog daemon. |
---|
| 131 | * |
---|
| 132 | * @param string $option |
---|
| 133 | */ |
---|
| 134 | public function setOption($option) { |
---|
| 135 | $this->_option = $option; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | public function activateOptions() { |
---|
| 140 | define_syslog_variables(); |
---|
| 141 | $this->closed = false; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | public function close() { |
---|
| 145 | closelog(); |
---|
| 146 | $this->closed = true; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | public function append($event) { |
---|
| 150 | |
---|
| 151 | if($this->_option == NULL){ |
---|
| 152 | $this->_option = LOG_PID | LOG_CONS; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | // Attach the process ID to the message, use the facility defined in the .properties-file |
---|
| 156 | openlog($this->_ident, $this->_option, $this->_facility); |
---|
| 157 | |
---|
| 158 | $level = $event->getLevel(); |
---|
| 159 | $message = $event->getRenderedMessage(); |
---|
| 160 | |
---|
| 161 | // If the priority of a syslog message can be overridden by a value defined in the properties-file, |
---|
| 162 | // use that value, else use the one that is defined in the code. |
---|
| 163 | if($this->_overridePriority){ |
---|
| 164 | syslog($this->_priority, $message); |
---|
| 165 | } else { |
---|
| 166 | if ($level->isGreaterOrEqual(LoggerLevel::getLevelFatal())) { |
---|
| 167 | syslog(LOG_ALERT, $message); |
---|
| 168 | } elseif ($level->isGreaterOrEqual(LoggerLevel::getLevelError())) { |
---|
| 169 | syslog(LOG_ERR, $message); |
---|
| 170 | } elseif ($level->isGreaterOrEqual(LoggerLevel::getLevelWarn())) { |
---|
| 171 | syslog(LOG_WARNING, $message); |
---|
| 172 | } elseif ($level->isGreaterOrEqual(LoggerLevel::getLevelInfo())) { |
---|
| 173 | syslog(LOG_INFO, $message); |
---|
| 174 | } elseif ($level->isGreaterOrEqual(LoggerLevel::getLevelDebug())) { |
---|
| 175 | syslog(LOG_DEBUG, $message); |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | closelog(); |
---|
| 179 | } |
---|
| 180 | } |
---|