1 package com.protomatter.syslog; 2 3 52 53 import java.net.InetAddress ; 54 import java.util.*; 55 import java.util.logging.*; 56 import java.io.File ; 57 import java.text.MessageFormat ; 58 import com.protomatter.syslog.xml.SyslogXML; 59 import com.protomatter.syslog.SyslogInitException; 60 import com.protomatter.util.ChainedRuntimeException; 61 import com.protomatter.util.StackTraceUtil; 62 63 141 public class SyslogHandler 142 extends Handler 143 { 144 private boolean open = true; 145 private Map levelMap = null; 146 147 private static String CONFIG_FILE_PROPERTY = "SyslogHandler.xml"; 148 149 static 150 { 151 String xmlFile = System.getProperty(CONFIG_FILE_PROPERTY); 152 if (xmlFile != null) 153 { 154 try 155 { 156 SyslogXML.configure(new File (xmlFile)); 157 } 158 catch (SyslogInitException x) 159 { 160 throw new ChainedRuntimeException(Syslog.getResourceString(MessageConstants.CANNOT_CONFIGURE_MESSAGE), x); 161 } 162 } 163 } 164 165 168 public SyslogHandler() 169 { 170 super(); 171 172 resetLevelMap(); 173 } 174 175 214 public void resetLevelMap() 215 { 216 Map levelMap = new HashMap(); 217 levelMap.put(Level.SEVERE, new Integer (Syslog.ERROR)); 218 levelMap.put(Level.WARNING, new Integer (Syslog.WARNING)); 219 levelMap.put(Level.INFO, new Integer (Syslog.INFO)); 220 levelMap.put(Level.CONFIG, new Integer (Syslog.DEBUG)); 221 levelMap.put(Level.FINE, new Integer (Syslog.DEBUG)); 222 levelMap.put(Level.FINER, new Integer (Syslog.DEBUG)); 223 levelMap.put(Level.FINEST, new Integer (Syslog.DEBUG)); 224 setLevelMap(levelMap); 225 } 226 227 230 public void close() 231 { 232 open = false; 233 } 234 235 241 public void flush() 242 { 243 Syslog.flush(); 244 } 245 246 270 public void publish(java.util.logging.LogRecord record) 271 { 272 if (!open) 273 return; 274 275 Filter filter = getFilter(); 277 if (filter != null && !filter.isLoggable(record)) 278 return; 279 280 String message = record.getMessage(); 282 Object params[] = record.getParameters(); 283 if (params != null && (params.length > 0)) 284 { 285 message = MessageFormat.format(message, params); 286 } 287 288 int syslogLevel = getSyslogLevel(record.getLevel()); 290 291 Thread currentThread = Thread.currentThread(); 293 String threadName = currentThread.getName(); 294 295 long messageTime = record.getMillis(); 297 298 Object loggerClass = record.getSourceClassName(); 300 if (loggerClass == null) 301 loggerClass = this; 302 303 Object detail = record.getThrown(); 305 306 String channel = record.getLoggerName(); 308 309 int depth = 4; 315 if ("java.util.logging.Logger".equals(StackTraceUtil.whereAmI(depth).className)) 316 depth++; 317 318 Syslog.log( 320 Syslog.getLocalHostName(), 321 loggerClass, 322 channel, 323 message, 324 detail, 325 syslogLevel, 326 currentThread, 327 threadName, 328 messageTime, 329 depth 330 ); 331 } 332 333 336 private final int getSyslogLevel(java.util.logging.Level level) 337 { 338 return ((Integer )this.levelMap.get(level)).intValue(); 339 } 340 341 347 public Map getLevelMap() 348 { 349 return this.levelMap; 350 } 351 352 358 public void setLevelMap(Map levelMap) 359 { 360 this.levelMap = levelMap; 361 } 362 } 363 | Popular Tags |