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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/*
* 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 org.apache.log4j.net;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.DatagramPacket;
import java.net.UnknownHostException;
import java.net.SocketException;
import org.apache.log4j.helpers.OptionConverter;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
import org.apache.log4j.Layout;
import org.apache.log4j.helpers.SingleLineTracerPrintWriter;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.helpers.QuietWriter;
/**
Use DatagramStringAppender to send log messages to a remote daemon
which accepts Datagram (UDP) messages.
<p>
The benefits of UDP are that the client is guarunteed not to
slow down if the network or remote log daemon is slow, and that
no permanent TCP connection between client and server exists.
<p>
The disadvantages are that log messages can be lost if the network
or remote daemon are under excessive load.
<p>
This class builts the final message string <b>before</b> sending
the UDP packet, hence the "string" component in the class name. This
means that the receiving application can be written in any language.
The data is transmitted in whatever encoding is specified in the
configuration file; this may be an 8-bit encoding (eg ISO-8859-1, also
known as LATIN-1) or a larger encoding, eg UTF-16.
<p>
An alternative to building the message string within DatagramStringAppender
would be to serialize & send the complete logging event object (perhaps
such a class could be called a DatagramSerialAppender??). The
receiving end could then be configured with appropriate Layout objects
to generate the actual logged messages. This would ensure that the
logging of messages from different sources is done in a consistent
format, and give a central place to configure that format. It would ensure
(by transmitting messages as unicode) that the receiving end could control
the encoding in which the output is generated. It also would possibly allow
he receiving end to use the full log4j flexibility to pass the event to
different appenders at the receiving end, as the category information is
retained, etc. However, this does require that the receiving end is in
java, and that all clients of the logging daemon are java applications.
In contrast, this DatagramStringAppender can send mesages to a log daemon
that accepts messages from a variety of sources.
@author Simon Kitching
*/
public class DatagramStringAppender extends AppenderSkeleton {
/**
A string constant used in naming the option for setting the destination
server for messages. Current value of this string constant is
<b>DatagramHost</b>. */
public static final String DATAGRAM_HOST_OPTION = "DatagramHost";
/**
A string constant used in naming the option for setting the destination
port for messages. Current value of this string constant is
<b>DatagramPort</b>. */
public static final String DATAGRAM_PORT_OPTION = "DatagramPort";
/**
A string constant used in naming the option for setting the character
encoding used when generating the log message. Current value of this
string constant is <b>DatagramEncoding</b>. */
public static final String DATAGRAM_ENCODING_OPTION = "DatagramEncoding";
/**
The default value for the "host" attribute, ie the machine to which
messages are sent. Current value of this string constant is
<b>localhost</b>. */
public static final String DEFAULT_HOST = "localhost";
/**
The default value for the "port" attribute, ie the UDP port to which
messages are sent. Current value of this integer constant is
<b>8200</b>. This value was chosen for no particular reason. */
public static final int DEFAULT_PORT = 8200;
/**
The default value for the "encoding" attribute, ie the way in which
unicode message strings are converted into a stream of bytes before
their transmission as a UDP packet. The current value of this constant
is <b>null</b>, which means that the default platform encoding will
be used. */
public static final String DEFAULT_ENCODING = null;
String host = DEFAULT_HOST;
int port = DEFAULT_PORT;
String encoding = DEFAULT_ENCODING;
SingleLineTracerPrintWriter stp;
QuietWriter qw;
public
DatagramStringAppender() {
this.setDestination(DEFAULT_HOST, DEFAULT_PORT, DEFAULT_ENCODING);
}
public
DatagramStringAppender(Layout layout) {
this.setLayout(layout);
this.setDestination(DEFAULT_HOST, DEFAULT_PORT, DEFAULT_ENCODING);
}
public
DatagramStringAppender(Layout layout, String host, int port) {
this.setLayout(layout);
this.setDestination(host, port, DEFAULT_ENCODING);
}
public
DatagramStringAppender(Layout layout, String host, int port, String encoding) {
this.setLayout(layout);
this.setDestination(host, port, encoding);
}
/**
Release any resources held by this Appender
*/
public
void close() {
closed = true;
// A DatagramWriter is UDP based and needs no opening. Hence, it
// can't be closed. We just unset the variables here.
qw = null;
stp = null;
}
public
void append(LoggingEvent event) {
if(!isAsSevereAsThreshold(event.priority))
return;
// We must not attempt to append if qw is null.
if(qw == null) {
errorHandler.error(
"No host is set for DatagramStringAppender named \""
+ this.name + "\".");
return;
}
String buffer = layout.format(event);
qw.write(buffer);
if(event.throwable != null)
event.throwable.printStackTrace(stp);
else if (event.throwableInformation != null) {
// we must be the receiver of a serialized/deserialized LoggingEvent;
// the event's throwable member is transient, ie becomes null when
// deserialized, but that's ok because throwableInformation should
// have the string equivalent of the same info (ie stack trace)
qw.write(event.throwableInformation);
}
}
/**
Activate the options set via the setOption method.
@see #setOption
*/
public
void activateOptions() {
this.setDestination(this.host, this.port, this.encoding);
}
/**
Returns the option names for this component, namely the string
array consisting of {{@link #DATAGRAM_HOST_OPTION}, {@link
#DATAGRAM_PORT_OPTION}, {@link #DATAGRAM_ENCODING_OPTION} */
public
String[] getOptionStrings() {
return OptionConverter.concatanateArrays(super.getOptionStrings(),
new String[] {
DATAGRAM_HOST_OPTION,
DATAGRAM_PORT_OPTION,
DATAGRAM_ENCODING_OPTION});
}
/**
The DatagramStringAppender requires a layout. Hence, this method return
<code>true</code>.
@since 0.8.4 */
public
boolean requiresLayout() {
return true;
}
/**
Set DatagramStringAppender specific parameters.
<p>
The recognized options are <b>DatagramHost</b>, <b>DatagramPort</b> and
<b>DatagramEncoding</b>, i.e. the values of the string constants
{@link #DATAGRAM_HOST_OPTION}, {@link #DATAGRAM_PORT_OPTION} and
{@link #DATAGRAM_ENCODING_OPTION} respectively.
<p>
<dl>
<p>
<dt><b>DatagramHost</b>
<dd>
The name (or ip address) of the host machine where log output should go.
If the DatagramHost is not set, then this appender will default to
{@link #DEFAULT_HOST}.
<p>
<dt><b>DatagramPort</b>
<dd>
The UDP port number where log output should go. See {@link #DEFAULT_PORT}
<p>
<dt><b>DatagramEncoding</b>
<dd>
The ISO character encoding to be used when converting the Unicode
message to a sequence of bytes within a UDP packet. If not defined, then
the encoding defaults to the default platform encoding.
</dl>
*/
public
void setOption(String option, String value) {
if(value == null) return;
super.setOption(option, value);
if(option.equals(DATAGRAM_HOST_OPTION))
{
this.host = value;
}
else if(option.equals(DATAGRAM_PORT_OPTION))
{
this.port = OptionConverter.toInt(value, DEFAULT_PORT);
}
else if(option.equals(DATAGRAM_ENCODING_OPTION))
{
this.encoding = value;
}
}
public
void setDestination(String host, int port, String encoding) {
if (host==null) {
LogLog.error("setDestination: host is null");
host = DEFAULT_HOST;
}
this.host = host;
this.port = port;
this.encoding = encoding;
this.qw = new QuietWriter(
new DatagramStringWriter(host, port, encoding),
errorHandler);
this.stp = new SingleLineTracerPrintWriter(qw);
}
public
void setLayout(Layout layout) {
this.layout = layout;
}
}
|