1 6 package org.enhydra.logging; 7 8 import java.util.Enumeration ; 9 import java.util.Iterator ; 10 import java.util.Locale ; 11 import java.util.Properties ; 12 import java.util.ResourceBundle ; 13 import java.util.Set ; 14 15 import javax.management.MBeanServer ; 16 import javax.management.MBeanServerFactory ; 17 import javax.management.ObjectName ; 18 19 import org.mortbay.log.Frame; 20 import org.mortbay.log.LogImpl; 21 import org.mortbay.log.LogSink; 22 import org.objectweb.util.monolog.Monolog; 23 import org.objectweb.util.monolog.api.BasicLevel; 24 import org.objectweb.util.monolog.api.Level; 25 import org.objectweb.util.monolog.api.Logger; 26 import org.objectweb.util.monolog.wrapper.common.Configurable; 27 28 import com.lutris.util.ConfigException; 29 30 31 public class MonologSink implements LogSink 32 { 33 private String _options; 34 private transient boolean _started; 35 private org.objectweb.util.monolog.api.LoggerFactory lf; 36 private Logger logger = null; 37 private String path, jonas, fileName; 38 private static ResourceBundle rb = null; 39 public static String PROPERTY_FILE; 40 static ObjectName objectName; 41 42 public MonologSink() throws ConfigException { 43 this("MonologSink"); 44 } 45 46 public MonologSink (String name) throws ConfigException { 47 48 try { 49 configure(); 50 }catch (ConfigException ex) { 51 throw new ConfigException("Cannot configure logger. Logger not initialized."); 52 } 53 54 } 55 56 58 public MonologSink(Logger logger ) { 59 this.logger=logger; 60 } 61 62 63 public void setOptions(String filename) 64 { 65 _options=filename; 66 } 67 68 69 public String getOptions() 70 { 71 return _options; 72 } 73 74 75 public void setLogImpl(LogImpl li){} 76 77 78 public void start() 79 throws Exception 80 { 81 _started=true; 82 } 83 84 85 public void stop() 86 { 87 _started=false; 88 } 89 90 91 public boolean isStarted() 92 { 93 return _started; 94 } 95 96 97 public void log(String tag, 98 Object msg, 99 Frame frame, 100 long time) 101 { 102 String method=frame.getMethod(); 103 int lb=method.indexOf('('); 104 int ld = (lb>0) 105 ?method.lastIndexOf('.',lb) 106 :method.lastIndexOf('.'); 107 if (ld<0) ld=lb; 108 109 String class_name = (ld>0)?method.substring(0,ld):method; 110 112 113 Level priority=BasicLevel.LEVEL_INFO; 114 115 if (LogImpl.DEBUG.equals(tag)) 116 priority=BasicLevel.LEVEL_DEBUG; 117 else if (LogImpl.WARN.equals(tag) || LogImpl.ERROR.equals(tag)) 118 priority=BasicLevel.LEVEL_ERROR; 119 else if (LogImpl.FAIL.equals(tag)) 120 priority=BasicLevel.LEVEL_FATAL; 121 122 if (!logger.isLoggable(priority)) 123 return; 124 125 logger.log(priority, 126 "org.enhydra.logging.MonologSink", 127 ""+msg, 128 null); 129 } 130 131 132 public synchronized void log(String s) 133 { 134 logger.log(BasicLevel.LEVEL_INFO, 135 "org.enhydra.logging.MonologSink", 136 s, 137 null); 138 } 139 140 public synchronized void configure() 141 throws ConfigException { 142 String b = null; 143 String propkey; 144 String propvalue; 145 Properties props = new Properties (); 146 147 try{ 148 findMBeanServer(); 149 findObjectName(); 150 if (objectName != null) 151 PROPERTY_FILE = objectName.getKeyProperty("fname"); 152 rb = ResourceBundle.getBundle(PROPERTY_FILE,new Locale ("en","US"), ClassLoader.getSystemClassLoader()); 154 155 Enumeration enumeration = rb.getKeys(); 156 157 while (enumeration.hasMoreElements()) 158 { 159 160 propkey = (String ) enumeration.nextElement(); 161 propvalue = rb.getString(propkey); 162 props.setProperty(propkey, propvalue); 163 } 164 165 } catch (Exception e){ 166 throw new ConfigException("Logger configuration file could not be found: logger could not be initialized!"); 167 } 168 169 try { 170 171 b = props.getProperty("log.config.classname", null); 173 if (b == null) { 174 throw new ConfigException("Malformed configuration log file:" 175 + " log.config.classname not available"); 176 } 177 178 180 props.put(Configurable.LOG_CONFIGURATION_TYPE, Configurable.PROPERTY); 181 lf = Monolog.getMonologFactory(b); 182 this.logger = lf.getLogger("MonologSink"); 183 184 } 185 catch (Exception e) { 186 throw new ConfigException("Malformed configuration log file:" 187 + " log.config.classname not available"); 188 } 189 190 } 191 192 private MBeanServer findMBeanServer () throws Exception { 193 MBeanServer mBeanServer; 194 try { 195 java.util.ArrayList server = MBeanServerFactory.findMBeanServer(null); 196 if (server == null) { 197 throw new Exception ("MBeansServer not found"); 198 } 199 else { 200 mBeanServer = (MBeanServer )server.get(0); 201 } 203 } catch (Exception e) { 204 throw new com.lutris.appserver.server.session.SessionException(e); 205 } 206 return mBeanServer; 207 } 208 209 212 private void findObjectName () throws Exception { 213 try { 214 objectName = new ObjectName ("jonas:type=service,name=log,*"); 215 MBeanServer mBeanServer = findMBeanServer(); 216 Set mBeans = mBeanServer.queryNames(objectName, null); 217 int l; 218 if ((l = mBeans.size()) > 1) { 219 throw new Exception ("MBean set size not equal 1: " + l); 220 }else if (l == 0){ 221 objectName = null; 222 return; 223 } 224 Iterator i = mBeans.iterator(); 225 objectName = (ObjectName )i.next(); 226 } catch (Exception e) { 227 throw new Exception (e); 228 } 229 return; 230 } 231 232 } | Popular Tags |