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 * @package log4php
19 */
20
21/**
22 * Log events using php {@link PHP_MANUAL#syslog} function.
23 *
24 * This appender can be configured by changing the following attributes:
25 *
26 * - layout           - Sets the layout class for this appender
27 * - ident            - Set the ident of the syslog message.
28 * - priority         - Set the priority value for the syslog message.
29 * - facility         - Set the facility value for the syslog message
30 * - overridePriority - If the priority of the message to be sent can be
31 *                      defined by a value in the properties-file, set
32 *                      parameter value to "true"
33 * - option           - Set the option value for the syslog message.
34 *                      This value is used as a parameter for php openlog()
35 *                      and passed on to the syslog daemon.
36 *
37 * Levels are mapped as follows:
38 * - <b>level >= FATAL</b> to LOG_ALERT
39 * - <b>FATAL > level >= ERROR</b> to LOG_ERR
40 * - <b>ERROR > level >= WARN</b> to LOG_WARNING
41 * - <b>WARN  > level >= INFO</b> to LOG_INFO
42 * - <b>INFO  > level >= DEBUG</b> to LOG_DEBUG
43 *
44 * An example:
45 *
46 * {@example ../../examples/php/appender_syslog.php 19}
47 *
48 * {@example ../../examples/resources/appender_syslog.properties 18}
49 *
50 * @version $Revision: 883108 $
51 * @package log4php
52 * @subpackage appenders
53 */
54class LoggerAppenderSyslog extends LoggerAppender {
55
56	/**
57	 * The ident string is added to each message. Typically the name of your application.
58	 *
59	 * @var string Ident for your application
60	 */
61	private $_ident = "Log4PHP Syslog-Event";
62
63	/**
64	 * The priority parameter value indicates the level of importance of the message.
65	 * It is passed on to the Syslog daemon.
66	 *
67	 * @var int Syslog priority
68	 */
69	private $_priority;
70
71	/**
72	 * The option used when generating a log message.
73	 * It is passed on to the Syslog daemon.
74	 *
75	 * @var int Syslog priority
76	 */
77	private $_option;
78
79	/**
80	 * The facility value indicates the source of the message.
81	 * It is passed on to the Syslog daemon.
82	 *
83	 * @var const int Syslog facility
84	 */
85	private $_facility;
86
87	/**
88	 * If it is necessary to define logging priority in the .properties-file,
89	 * set this variable to "true".
90	 *
91	 * @var const int  value indicating whether the priority of the message is defined in the .properties-file
92	 *				   (or properties-array)
93	 */
94	private $_overridePriority;
95
96	/** @var indiciates if this appender should run in dry mode */
97	private $dry = false;
98
99	public function __construct($name = '') {
100		parent::__construct($name);
101		$this->requiresLayout = true;
102	}
103
104	public function __destruct() {
105       $this->close();
106   	}
107
108   	public function setDry($dry) {
109		$this->dry = $dry;
110	}
111
112	/**
113	 * Set the ident of the syslog message.
114	 *
115	 * @param string Ident
116	 */
117	public function setIdent($ident) {
118		$this->_ident = $ident;
119	}
120
121	/**
122	 * Set the priority value for the syslog message.
123	 *
124	 * @param const int Priority
125	 */
126	public function setPriority($priority) {
127		$this->_priority = $priority;
128	}
129
130
131	/**
132	 * Set the facility value for the syslog message.
133	 *
134	 * @param const int Facility
135	 */
136	public function setFacility($facility) {
137		$this->_facility = $facility;
138	}
139
140	/**
141	 * If the priority of the message to be sent can be defined by a value in the properties-file,
142	 * set parameter value to "true".
143	 *
144	 * @param bool Override priority
145	 */
146	public function setOverridePriority($overridePriority) {
147		$this->_overridePriority = $overridePriority;
148	}
149
150	/**
151	 * Set the option value for the syslog message.
152	 * This value is used as a parameter for php openlog()
153	 * and passed on to the syslog daemon.
154	 *
155	 * @param string	$option
156	 */
157	public function setOption($option) {
158		$this->_option = $option;
159	}
160
161	public function activateOptions() {
162		// Deprecated as of 5.3 and removed in 6.0
163		// define_syslog_variables();
164		$this->closed = false;
165	}
166
167	public function close() {
168		if($this->closed != true) {
169			closelog();
170			$this->closed = true;
171		}
172	}
173
174	public function append(LoggerLoggingEvent $event) {
175		if($this->_option == NULL){
176			$this->_option = LOG_PID | LOG_CONS;
177		}
178
179		$level	 = $event->getLevel();
180		if($this->layout === null) {
181			$message = $event->getRenderedMessage();
182		} else {
183			$message = $this->layout->format($event);
184		}
185
186		// If the priority of a syslog message can be overridden by a value defined in the properties-file,
187		// use that value, else use the one that is defined in the code.
188		if(!$this->dry) {
189			// Attach the process ID to the message, use the facility defined in the .properties-file
190			openlog($this->_ident, $this->_option, $this->_facility);
191
192			if($this->_overridePriority) {
193				syslog($this->_priority, $message);
194			} else {
195				if($level->isGreaterOrEqual(LoggerLevel::getLevelFatal())) {
196					syslog(LOG_ALERT, $message);
197				} else if ($level->isGreaterOrEqual(LoggerLevel::getLevelError())) {
198					syslog(LOG_ERR, $message);
199				} else if ($level->isGreaterOrEqual(LoggerLevel::getLevelWarn())) {
200					syslog(LOG_WARNING, $message);
201				} else if ($level->isGreaterOrEqual(LoggerLevel::getLevelInfo())) {
202					syslog(LOG_INFO, $message);
203				} else if ($level->isGreaterOrEqual(LoggerLevel::getLevelDebug())) {
204					syslog(LOG_DEBUG, $message);
205				}
206			}
207			closelog();
208		} else {
209		      echo "DRY MODE OF SYSLOG APPENDER: ".$message;
210		}
211	}
212}
213