1 package com.protomatter.syslog; 2 3 52 53 import java.io.PrintWriter ; 54 import java.util.*; 55 import java.text.*; 56 import java.net.*; 57 import java.io.*; 58 59 import com.protomatter.util.*; 60 61 68 public class SimpleSyslogMailSubjectFormatter 69 implements SyslogMailSubjectFormatter 70 { 71 private static String DEBUG = "DEBUG".intern(); 72 private static String INFO = "INFO".intern(); 73 private static String WARNING = "WARNING".intern(); 74 private static String ERROR = "ERROR".intern(); 75 private static String FATAL = "FATAL".intern(); 76 private static String UNKNOWN_LEVEL = "????".intern(); 77 78 private boolean showCaller = true; 79 private boolean showChannel = false; 80 private boolean showThreadName = false; 81 private boolean showHostName = false; 82 83 86 public SimpleSyslogMailSubjectFormatter() 87 { 88 super(); 89 } 90 91 94 public String formatMessageSubject(SyslogMessage message) 95 { 96 StringBuffer b = new StringBuffer (64); 97 b.append(getStringForLevel(message.level)); 98 b.append(": "); 99 100 if (showChannel) 101 { 102 b.append("["); 103 if (message.channel.equals(Syslog.ALL_CHANNEL)) 104 b.append("ALL_CHANNEL"); 105 else if (message.channel.equals(Syslog.DEFAULT_CHANNEL)) 106 b.append("DEFAULT_CHANNEL"); 107 else 108 b.append(message.channel); 109 b.append("] "); 110 } 111 112 if (showHostName) 113 { 114 b.append("["); 115 b.append(StringUtil.pad(getHostname(message.host), 10)); 116 b.append("] "); 117 } 118 119 if (showThreadName) 120 { 121 b.append("["); 122 b.append(message.threadName); 123 b.append("] "); 124 } 125 if (showCaller) 126 { 127 b.append(trimFromLastPeriod(message.loggerClassname)); 128 } 129 130 if (message.msg != null) 131 { 132 b.append(": "); 133 b.append(message.msg); 134 } 135 return b.toString(); 136 } 137 138 protected String getStringForLevel(int level) 139 { 140 switch (level) 141 { 142 case Syslog.DEBUG: return DEBUG; 143 case Syslog.INFO: return INFO; 144 case Syslog.WARNING: return WARNING; 145 case Syslog.ERROR: return ERROR; 146 case Syslog.FATAL: return FATAL; 147 default: return UNKNOWN_LEVEL; 148 } 149 } 150 151 protected String getHostname(InetAddress host) 152 { 153 String ip = host.getHostAddress(); 154 String name = host.getHostName(); 155 if (ip.equals(name)) 156 return ip; 157 158 int idx = name.indexOf("."); 159 if (idx == -1) 160 return name; 161 return name.substring(0, idx); 162 } 163 164 167 public void setShowHostName(boolean showHostName) 168 { 169 this.showHostName = showHostName; 170 } 171 172 175 public boolean getShowHostName() 176 { 177 return this.showHostName; 178 } 179 180 183 public void setShowThreadName(boolean showThreadName) 184 { 185 this.showThreadName = showThreadName; 186 } 187 188 191 public boolean getShowThreadName() 192 { 193 return this.showThreadName; 194 } 195 196 199 public void setShowCaller(boolean showCaller) 200 { 201 this.showCaller = showCaller; 202 } 203 204 207 public boolean getShowCaller() 208 { 209 return this.showCaller; 210 } 211 212 215 public void setShowChannel(boolean showChannel) 216 { 217 this.showChannel = showChannel; 218 } 219 220 223 public boolean getShowChannel() 224 { 225 return this.showChannel; 226 } 227 228 231 protected String trimFromLastPeriod(String s) 232 { 233 int pos = s.lastIndexOf('.'); 234 if (pos >= 0) 235 return s.substring(pos+1); 236 else 237 return s; 238 } 239 } 240 | Popular Tags |