2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.log4j.helpers;
21 import java.io.Writer;
22 import java.net.DatagramSocket;
23 import java.net.InetAddress;
24 import java.net.DatagramPacket;
25 import java.net.UnknownHostException;
26 import java.net.SocketException;
27 import java.io.IOException;
29 import java.net.MalformedURLException;
32 SyslogWriter is a wrapper around the java.net.DatagramSocket class
33 so that it behaves like a java.io.Writer.
37 public class SyslogWriter extends Writer {
39 final int SYSLOG_PORT = 514;
41 * Host string from last constructed SyslogWriter.
44 static String syslogHost;
46 private InetAddress address;
47 private final int port;
48 private DatagramSocket ds;
51 * Constructs a new instance of SyslogWriter.
52 * @param syslogHost host name, may not be null. A port
53 * may be specified by following the name or IPv4 literal address with
54 * a colon and a decimal port number. To specify a port with an IPv6
55 * address, enclose the IPv6 address in square brackets before appending
56 * the colon and decimal port number.
59 SyslogWriter(final String syslogHost) {
60 SyslogWriter.syslogHost = syslogHost;
61 if (syslogHost == null) {
62 throw new NullPointerException("syslogHost");
65 String host = syslogHost;
69 // If not an unbracketed IPv6 address then
72 if (host.indexOf("[") != -1 || host.indexOf(':') == host.lastIndexOf(':')) {
74 URL url = new URL("http://" + host);
75 if (url.getHost() != null) {
77 // if host is a IPv6 literal, strip off the brackets
78 if(host.startsWith("[") && host.charAt(host.length() - 1) == ']') {
79 host = host.substring(1, host.length() - 1);
81 urlPort = url.getPort();
83 } catch(MalformedURLException e) {
84 LogLog.error("Malformed URL: will attempt to interpret as InetAddress.", e);
89 urlPort = SYSLOG_PORT;
94 this.address = InetAddress.getByName(host);
96 catch (UnknownHostException e) {
97 LogLog.error("Could not find " + host +
98 ". All logging will FAIL.", e);
102 this.ds = new DatagramSocket();
104 catch (SocketException e) {
106 LogLog.error("Could not instantiate DatagramSocket to " + host +
107 ". All logging will FAIL.", e);
114 void write(char[] buf, int off, int len) throws IOException {
115 this.write(new String(buf, off, len));
119 void write(final String string) throws IOException {
121 if(this.ds != null && this.address != null) {
122 byte[] bytes = string.getBytes();
124 // syslog packets must be less than 1024 bytes
126 int bytesLength = bytes.length;
127 if (bytesLength >= 1024) {
130 DatagramPacket packet = new DatagramPacket(bytes, bytesLength,
140 public void close() {