001/* 002// Name 003// $RCSfile: Handler.java,v $ 004// 005// Copyright 006// Confidential Property of Kabira Technologies, Inc. 007// Copyright 2010, 2011 by Kabira Technologies, Inc. 008// All rights reserved. 009// 010// History 011// $Revision: 1.1.2.4 $ $Date: 2011/06/12 15:29:23 $ 012// 013 */ 014package com.kabira.platform.logging; 015 016import java.util.logging.LogRecord; 017import java.util.logging.SimpleFormatter; 018import java.util.logging.Level; 019 020import com.kabira.platform.Transaction; 021import com.kabira.platform.event.Severity; 022 023/** 024 * Default log handler 025 * <p> 026 * Sends log messages to all log event subscribers. 027 */ 028public class Handler extends java.util.logging.Handler 029{ 030 /** 031 * Create a log handler 032 */ 033 public Handler() 034 { 035 m_eventService = new EventService(); 036 setLevel(java.util.logging.Level.ALL); 037 } 038 039 /** 040 * Publish a log record 041 * @param record Log record to publish 042 * <p> 043 * The log record is published synchronously to all 044 * log event subscribers. This method can be called with or without 045 * a transaction. 046 */ 047 @Override 048 public void publish(LogRecord record) 049 { 050 SimpleFormatter sf = new SimpleFormatter(); 051 final String str = sf.format(record); 052 final LogRecord rcd = record; 053 Severity s; 054 int levelValue = rcd.getLevel().intValue(); 055 056 if (levelValue >= Level.SEVERE.intValue()) 057 { 058 s = Severity.Error; 059 } 060 else if (levelValue >= Level.WARNING.intValue()) 061 { 062 s = Severity.Warning; 063 } 064 else if (levelValue >= Level.INFO.intValue()) 065 { 066 s = Severity.Info; 067 } 068 else if (levelValue >= Level.CONFIG.intValue()) 069 { 070 s = Severity.Info; 071 } 072 else 073 { 074 s = Severity.Debug; 075 } 076 077 final int eventId = s.ordinal(); 078 079 // 080 // Handle being called from within and without of a transaction 081 // 082 if (Transaction.isActive()) 083 { 084 m_eventService.send( 085 "handler", 086 eventId, 087 m_eventService, 088 new String[] 089 { 090 str 091 }); 092 } 093 else 094 { 095 // 096 // Start a new transaction to send the event 097 // 098 new Transaction() 099 { 100 @Override 101 public void run() throws Transaction.Rollback 102 { 103 m_eventService.send( 104 "handler", 105 eventId, 106 m_eventService, 107 new String[] 108 { 109 str 110 }); 111 112 } 113 }.execute(); 114 } 115 } 116 117 /** 118 * Flush log handler 119 * <p> 120 * This is a noop. 121 */ 122 @Override 123 public void flush() 124 { 125 // nothing to do. 126 } 127 128 /** 129 * Close the log handler 130 * <p> 131 * This is a noop. 132 */ 133 @Override 134 public void close() 135 { 136 // nothing to do. 137 } 138 private EventService m_eventService; 139}