Syslog.java
8.58 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package com.sitech.ismp.ice.syslog;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
/**
* The Syslog class implements the UNIX syslog protocol allowing Java to log
* messages to a specified UNIX host. Care has been taken to preserve as much of
* the UNIX implementation as possible. <br>
* To use Syslog, simply create an instance, and use the Syslog() method to log
* your message. The class provides all the expected syslog constants. For
* example, LOG_ERR is Syslog.LOG_ERR. <br>
*
* Written: <a href="http://www.ice.com/time/">Tim Endres</a><br>
* Version: 1.2 - July 27, 1998<br>
* Version: 1.0 - August 14, 1996<br>
* Source: <a href="http://www.ice.com/java/syslog/index.shtml">Syslog.java</a>
*
* @see DatagramSocket
* @see InetAddress
*/
public class Syslog extends Object {
static private Syslog logger = null;
private String logName;
private String hostName;
private int portNum;
private int flags;
private boolean includeDate;
private InetAddress boundAddress;
private DatagramSocket socket;
private SimpleDateFormat date1Format;
private SimpleDateFormat date2Format;
// CLASS METHODS
/**
* Binds the Syslog class to a specified host for further logging. See the
* Syslog constructor for details on the parameters.
*/
static public void open(String hostname, String name, int flags)
throws SyslogException {
try {
Syslog.logger = new Syslog(hostname, SyslogDefs.DEFAULT_PORT, name,
flags);
} catch (SyslogException ex) {
throw ex;
}
}
/**
* Performs a syslog to the currently bound syslog host.
*/
static public void log(int fac, int lvl, String msg) throws SyslogException {
try {
logger.syslog(fac, lvl, msg);
} catch (SyslogException ex) {
throw ex;
}
}
/**
* Unbinds the current syslog host.
*/
static public void close() {
logger = null;
}
/**
* Creates a Syslog object instance, targeted for the UNIX host with the
* hostname 'hostname' on the syslog port 'port'. The only flags recognized
* are 'LOG_PERROR', which will log the message to Java's 'System.err'.
*/
public Syslog(String name, int flags) throws SyslogException {
super();
this.logName = name;
this.hostName = null;
this.portNum = SyslogDefs.DEFAULT_PORT;
this.flags = flags;
this.initialize();
}
/**
* Creates a Syslog object instance, targeted for the UNIX host with the
* hostname 'hostname' on the syslog port 'port'. The only flags recognized
* are 'LOG_PERROR', which will log the message to Java's 'System.err'.
*/
public Syslog(String hostname, int port, String name, int flags)
throws SyslogException {
super();
this.logName = name;
this.hostName = hostname;
this.portNum = port;
this.flags = flags;
try {
this.boundAddress = InetAddress.getByName(hostname);
} catch (UnknownHostException ex) {
String message = "error locating host named '" + hostname + "': "
+ ex.getMessage();
throw new SyslogException(message);
}
this.initialize();
}
private void initialize() throws SyslogException {
try {
this.socket = new DatagramSocket();
} catch (SocketException ex) {
String message = "error creating syslog udp socket: "
+ ex.getMessage();
throw new SyslogException(message);
}
// This determines if the timestamp is added to the message
// in this, the client, or if it is left off for the server
// to fill in. Adding it in the client is the "standard" way,
// but most servers support the "old style" (no timestamp)
// messages as well. I leave this choice, since some servers
// may not work without it, and because some JDK's may not
// support SimpleDateFormat or TimeZone correctly.
this.includeDate = true;
if (this.includeDate) {
// We need two separate formatters here, since there is
// no way to get the single digit date (day of month) to
// pad with a space instead of a zero.
this.date1Format = new SimpleDateFormat("MMM d HH:mm:ss ",
Locale.US);
this.date2Format = new SimpleDateFormat("MMM dd HH:mm:ss ",
Locale.US);
this.date1Format.setTimeZone(TimeZone.getDefault());
this.date2Format.setTimeZone(TimeZone.getDefault());
}
}
/**
* Use this method to log your syslog messages. The facility and level are
* the same as their UNIX counterparts, and the Syslog class provides
* constants for these fields. The msg is what is actually logged.
*/
public void syslog(int fac, int pri, String msg) throws SyslogException {
this.syslog(this.boundAddress, this.portNum, fac, pri, msg);
}
/**
* Use this method to log your syslog messages. The facility and level are
* the same as their UNIX counterparts, and the Syslog class provides
* constants for these fields. The msg is what is actually logged.
*/
public void syslog(InetAddress addr, int fac, int pri, String msg)
throws SyslogException {
this.syslog(addr, this.portNum, fac, pri, msg);
}
/**
* Use this method to log your syslog messages. The facility and level are
* the same as their UNIX counterparts, and the Syslog class provides
* constants for these fields. The msg is what is actually logged.
*/
public void syslog(String hostname, int fac, int pri, String msg)
throws SyslogException {
try {
InetAddress address = InetAddress.getByName(hostname);
this.syslog(address, this.portNum, fac, pri, msg);
} catch (UnknownHostException ex) {
String message = "error locating host named '" + hostname + "': "
+ ex.getMessage();
throw new SyslogException(message);
}
}
/**
* Use this method to log your syslog messages. The facility and level are
* the same as their UNIX counterparts, and the Syslog class provides
* constants for these fields. The msg is what is actually logged.
*/
public void syslog(String hostname, int port, int fac, int pri, String msg)
throws SyslogException {
try {
InetAddress address = InetAddress.getByName(hostname);
this.syslog(address, port, fac, pri, msg);
} catch (UnknownHostException ex) {
String message = "error locating host named '" + hostname + "': "
+ ex.getMessage();
throw new SyslogException(message);
}
}
/**
* Use this method to log your syslog messages. The facility and level are
* the same as their UNIX counterparts, and the Syslog class provides
* constants for these fields. The msg is what is actually logged.
*/
public void syslog(InetAddress addr, int port, int fac, int pri, String msg)
throws SyslogException {
int pricode;
int length;
int idx, sidx, nidx;
StringBuffer buffer;
byte[] data;
byte[] sBytes;
byte[] numbuf = new byte[32];
String nmObj;
String strObj;
pricode = SyslogDefs.computeCode(fac, pri);
Integer priObj = new Integer(pricode);
if (this.logName != null) {
nmObj = new String(this.logName);
} else {
nmObj = new String(Thread.currentThread().getName());
}
length = 4 + nmObj.length() + msg.length() + 1;
length += (pricode > 99) ? 3 : ((pricode > 9) ? 2 : 1);
String dStr = null;
if (this.includeDate) {
// See note above on why we have two formats...
Calendar now = Calendar.getInstance();
if (now.get(Calendar.DAY_OF_MONTH) < 10)
dStr = this.date1Format.format(now.getTime());
else
dStr = this.date2Format.format(now.getTime());
length += dStr.length();
}
data = new byte[length];
idx = 0;
data[idx++] = '<';
strObj = priObj.toString(priObj.intValue());
sBytes = strObj.getBytes();
System.arraycopy(sBytes, 0, data, idx, sBytes.length);
idx += sBytes.length;
data[idx++] = '>';
if (this.includeDate) {
sBytes = dStr.getBytes();
System.arraycopy(sBytes, 0, data, idx, sBytes.length);
idx += sBytes.length;
}
sBytes = nmObj.getBytes();
System.arraycopy(sBytes, 0, data, idx, sBytes.length);
idx += sBytes.length;
data[idx++] = ':';
data[idx++] = ' ';
sBytes = msg.getBytes();
System.arraycopy(sBytes, 0, data, idx, sBytes.length);
idx += sBytes.length;
data[idx] = 0;
DatagramPacket packet = new DatagramPacket(data, length, addr, port);
try {
socket.send(packet);
} catch (IOException ex) {
String message = "error sending message: '" + ex.getMessage() + "'";
System.err.println(message);
throw new SyslogException(message);
}
if ((this.flags & SyslogDefs.LOG_PERROR) != 0) {
if (this.logName != null) {
System.err.print(this.logName + ": ");
} else {
System.err.print(Thread.currentThread().getName() + ": ");
}
System.err.println(msg);
}
}
}