Fields Summary |
---|
public static final int | LOG_KERNKernel messages |
public static final int | LOG_USERRandom user-level messages |
public static final int | LOG_MAILMail system |
public static final int | LOG_DAEMONSystem daemons |
public static final int | LOG_AUTHsecurity/authorization messages |
public static final int | LOG_SYSLOGmessages generated internally by syslogd |
public static final int | LOG_LPRline printer subsystem |
public static final int | LOG_NEWSnetwork news subsystem |
public static final int | LOG_UUCPUUCP subsystem |
public static final int | LOG_CRONclock daemon |
public static final int | LOG_AUTHPRIVsecurity/authorization messages (private) |
public static final int | LOG_FTPftp daemon |
public static final int | LOG_LOCAL0reserved for local use |
public static final int | LOG_LOCAL1reserved for local use |
public static final int | LOG_LOCAL2reserved for local use |
public static final int | LOG_LOCAL3reserved for local use |
public static final int | LOG_LOCAL4reserved for local use |
public static final int | LOG_LOCAL5reserved for local use |
public static final int | LOG_LOCAL6reserved for local use |
public static final int | LOG_LOCAL7reserved for local use |
protected static final int | SYSLOG_HOST_OI |
protected static final int | FACILITY_OI |
static final String | TAB |
int | syslogFacility |
String | facilityStr |
boolean | facilityPrinting |
SyslogQuietWriter | sqw |
String | syslogHost |
private boolean | headerIf true, the appender will generate the HEADER (timestamp and host name)
part of the syslog packet. |
private final SimpleDateFormat | dateFormatDate format used if header = true. |
private String | localHostnameHost name used to identify messages from this appender. |
private boolean | layoutHeaderCheckedSet to true after the header of the layout has been sent or if it has none. |
Methods Summary |
---|
public void | activateOptions()This method returns immediately as options are activated when they
are set.
if (header) {
getLocalHostname();
}
if (layout != null && layout.getHeader() != null) {
sendLayoutMessage(layout.getHeader());
}
layoutHeaderChecked = true;
|
public void | append(org.apache.log4j.spi.LoggingEvent event)
if(!isAsSevereAsThreshold(event.getLevel()))
return;
// We must not attempt to append if sqw is null.
if(sqw == null) {
errorHandler.error("No syslog host is set for SyslogAppedender named \""+
this.name+"\".");
return;
}
if (!layoutHeaderChecked) {
if (layout != null && layout.getHeader() != null) {
sendLayoutMessage(layout.getHeader());
}
layoutHeaderChecked = true;
}
String hdr = getPacketHeader(event.timeStamp);
String packet = layout.format(event);
if(facilityPrinting || hdr.length() > 0) {
StringBuffer buf = new StringBuffer(hdr);
if(facilityPrinting) {
buf.append(facilityStr);
}
buf.append(packet);
packet = buf.toString();
}
sqw.setLevel(event.getLevel().getSyslogEquivalent());
//
// if message has a remote likelihood of exceeding 1024 bytes
// when encoded, consider splitting message into multiple packets
if (packet.length() > 256) {
splitPacket(hdr, packet);
} else {
sqw.write(packet);
}
if (layout.ignoresThrowable()) {
String[] s = event.getThrowableStrRep();
if (s != null) {
for(int i = 0; i < s.length; i++) {
if (s[i].startsWith("\t")) {
sqw.write(hdr+TAB+s[i].substring(1));
} else {
sqw.write(hdr+s[i]);
}
}
}
}
|
public synchronized void | close()Release any resources held by this SyslogAppender.
closed = true;
if (sqw != null) {
try {
if (layoutHeaderChecked && layout != null && layout.getFooter() != null) {
sendLayoutMessage(layout.getFooter());
}
sqw.close();
sqw = null;
} catch(java.io.IOException ex) {
sqw = null;
}
}
|
public java.lang.String | getFacility()Returns the value of the Facility option.
return getFacilityString(syslogFacility);
|
public static int | getFacility(java.lang.String facilityName)Returns the integer value corresponding to the named syslog
facility, or -1 if it couldn't be recognized.
if(facilityName != null) {
facilityName = facilityName.trim();
}
if("KERN".equalsIgnoreCase(facilityName)) {
return LOG_KERN;
} else if("USER".equalsIgnoreCase(facilityName)) {
return LOG_USER;
} else if("MAIL".equalsIgnoreCase(facilityName)) {
return LOG_MAIL;
} else if("DAEMON".equalsIgnoreCase(facilityName)) {
return LOG_DAEMON;
} else if("AUTH".equalsIgnoreCase(facilityName)) {
return LOG_AUTH;
} else if("SYSLOG".equalsIgnoreCase(facilityName)) {
return LOG_SYSLOG;
} else if("LPR".equalsIgnoreCase(facilityName)) {
return LOG_LPR;
} else if("NEWS".equalsIgnoreCase(facilityName)) {
return LOG_NEWS;
} else if("UUCP".equalsIgnoreCase(facilityName)) {
return LOG_UUCP;
} else if("CRON".equalsIgnoreCase(facilityName)) {
return LOG_CRON;
} else if("AUTHPRIV".equalsIgnoreCase(facilityName)) {
return LOG_AUTHPRIV;
} else if("FTP".equalsIgnoreCase(facilityName)) {
return LOG_FTP;
} else if("LOCAL0".equalsIgnoreCase(facilityName)) {
return LOG_LOCAL0;
} else if("LOCAL1".equalsIgnoreCase(facilityName)) {
return LOG_LOCAL1;
} else if("LOCAL2".equalsIgnoreCase(facilityName)) {
return LOG_LOCAL2;
} else if("LOCAL3".equalsIgnoreCase(facilityName)) {
return LOG_LOCAL3;
} else if("LOCAL4".equalsIgnoreCase(facilityName)) {
return LOG_LOCAL4;
} else if("LOCAL5".equalsIgnoreCase(facilityName)) {
return LOG_LOCAL5;
} else if("LOCAL6".equalsIgnoreCase(facilityName)) {
return LOG_LOCAL6;
} else if("LOCAL7".equalsIgnoreCase(facilityName)) {
return LOG_LOCAL7;
} else {
return -1;
}
|
public boolean | getFacilityPrinting()Returns the value of the FacilityPrinting option.
return facilityPrinting;
|
public static java.lang.String | getFacilityString(int syslogFacility)Returns the specified syslog facility as a lower-case String,
e.g. "kern", "user", etc.
switch(syslogFacility) {
case LOG_KERN: return "kern";
case LOG_USER: return "user";
case LOG_MAIL: return "mail";
case LOG_DAEMON: return "daemon";
case LOG_AUTH: return "auth";
case LOG_SYSLOG: return "syslog";
case LOG_LPR: return "lpr";
case LOG_NEWS: return "news";
case LOG_UUCP: return "uucp";
case LOG_CRON: return "cron";
case LOG_AUTHPRIV: return "authpriv";
case LOG_FTP: return "ftp";
case LOG_LOCAL0: return "local0";
case LOG_LOCAL1: return "local1";
case LOG_LOCAL2: return "local2";
case LOG_LOCAL3: return "local3";
case LOG_LOCAL4: return "local4";
case LOG_LOCAL5: return "local5";
case LOG_LOCAL6: return "local6";
case LOG_LOCAL7: return "local7";
default: return null;
}
|
public final boolean | getHeader()If true, the appender will generate the HEADER part (that is, timestamp and host name)
of the syslog packet. Default value is false for compatibility with existing behavior,
however should be true unless there is a specific justification.
return header;
|
private java.lang.String | getLocalHostname()Get the host name used to identify this appender.
if (localHostname == null) {
try {
InetAddress addr = InetAddress.getLocalHost();
localHostname = addr.getHostName();
} catch (UnknownHostException uhe) {
localHostname = "UNKNOWN_HOST";
}
}
return localHostname;
|
private java.lang.String | getPacketHeader(long timeStamp)Gets HEADER portion of packet.
if (header) {
StringBuffer buf = new StringBuffer(dateFormat.format(new Date(timeStamp)));
// RFC 3164 says leading space, not leading zero on days 1-9
if (buf.charAt(4) == '0") {
buf.setCharAt(4, ' ");
}
buf.append(getLocalHostname());
buf.append(' ");
return buf.toString();
}
return "";
|
public java.lang.String | getSyslogHost()Returns the value of the SyslogHost option.
return syslogHost;
|
private void | initSyslogFacilityStr()
facilityStr = getFacilityString(this.syslogFacility);
if (facilityStr == null) {
System.err.println("\"" + syslogFacility +
"\" is an unknown syslog facility. Defaulting to \"USER\".");
this.syslogFacility = LOG_USER;
facilityStr = "user:";
} else {
facilityStr += ":";
}
|
public boolean | requiresLayout()The SyslogAppender requires a layout. Hence, this method returns
true .
return true;
|
private void | sendLayoutMessage(java.lang.String msg)Set header or footer of layout.
if (sqw != null) {
String packet = msg;
String hdr = getPacketHeader(new Date().getTime());
if(facilityPrinting || hdr.length() > 0) {
StringBuffer buf = new StringBuffer(hdr);
if(facilityPrinting) {
buf.append(facilityStr);
}
buf.append(msg);
packet = buf.toString();
}
sqw.setLevel(6);
sqw.write(packet);
}
|
public void | setFacility(java.lang.String facilityName)Set the syslog facility. This is the Facility option.
The facilityName parameter must be one of the
strings KERN, USER, MAIL, DAEMON, AUTH, SYSLOG, LPR, NEWS, UUCP,
CRON, AUTHPRIV, FTP, LOCAL0, LOCAL1, LOCAL2, LOCAL3, LOCAL4,
LOCAL5, LOCAL6, LOCAL7. Case is unimportant.
if(facilityName == null)
return;
syslogFacility = getFacility(facilityName);
if (syslogFacility == -1) {
System.err.println("["+facilityName +
"] is an unknown syslog facility. Defaulting to [USER].");
syslogFacility = LOG_USER;
}
this.initSyslogFacilityStr();
// If there is already a sqw, make it use the new facility.
if(sqw != null) {
sqw.setSyslogFacility(this.syslogFacility);
}
|
public void | setFacilityPrinting(boolean on)If the FacilityPrinting option is set to true, the printed
message will include the facility name of the application. It is
false by default.
facilityPrinting = on;
|
public final void | setHeader(boolean val)Returns whether the appender produces the HEADER part (that is, timestamp and host name)
of the syslog packet.
header = val;
|
public void | setSyslogHost(java.lang.String syslogHost)The SyslogHost option is the name of the the syslog host
where log output should go. A non-default port can be specified by
appending a colon and port number to a host name,
an IPv4 address or an IPv6 address enclosed in square brackets.
WARNING If the SyslogHost is not set, then this appender
will fail.
this.sqw = new SyslogQuietWriter(new SyslogWriter(syslogHost),
syslogFacility, errorHandler);
//this.stp = new SyslogTracerPrintWriter(sqw);
this.syslogHost = syslogHost;
|
private void | splitPacket(java.lang.String header, java.lang.String packet)
int byteCount = packet.getBytes().length;
//
// if packet is less than RFC 3164 limit
// of 1024 bytes, then write it
// (must allow for up 5to 5 characters in the PRI section
// added by SyslogQuietWriter)
if (byteCount <= 1019) {
sqw.write(packet);
} else {
int split = header.length() + (packet.length() - header.length())/2;
splitPacket(header, packet.substring(0, split) + "...");
splitPacket(header, header + "..." + packet.substring(split));
}
|