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