1 23 package com.lutris.logging; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import java.util.Iterator ; 30 import java.util.Locale ; 31 import java.util.Properties ; 32 import java.util.ResourceBundle ; 33 import java.util.Set ; 34 35 import javax.management.MBeanServerConnection ; 36 import javax.management.ObjectName ; 37 import javax.management.remote.JMXConnector ; 38 import javax.management.remote.JMXConnectorFactory ; 39 import javax.management.remote.JMXServiceURL ; 40 41 import org.objectweb.util.monolog.Monolog; 42 import org.objectweb.util.monolog.api.LoggerFactory; 43 import org.objectweb.util.monolog.wrapper.common.Configurable; 44 45 import com.lutris.util.Config; 46 import com.lutris.util.ConfigException; 47 60 public class MonologLogger extends com.lutris.logging.Logger { 61 62 65 static ObjectName objectName; 66 67 72 private Hashtable levelNumbers = new Hashtable (); 73 74 78 protected String [] levelNames = new String [MAX_STD_LEVEL * 2]; 79 protected int numLevels = 0; 80 81 85 protected boolean[] enabledLevelFlags = null; 86 87 91 protected boolean[] logFileLevelFlags = null; 92 93 99 protected boolean[] stderrLevelFlags = null; 100 101 104 protected String DEFAULT_LOG_CONFIG_FILE = "trace.properties"; 105 106 109 File activeLogFile; 110 111 114 LoggerFactory lf; 115 116 private static ResourceBundle rb = null; 117 public static String PROPERTY_FILE; 118 119 120 124 private Hashtable logChannels = new Hashtable (); 125 126 String defaultSeparatorLine =""; 127 Hashtable appSeparatorLine = new Hashtable (); 128 129 135 public MonologLogger(boolean makeCentral) { 136 int level; 137 138 for (level = 0; level <= MAX_STD_LEVEL; level++) { 139 String name = standardLevelNames[level]; 140 141 levelNumbers.put(name, new Integer (level)); 142 levelNames[level] = name; 143 } 144 numLevels = level; 145 if (makeCentral) { 146 centralLogger = this; 147 } 148 149 } 150 151 157 private int getMaxLevel(String [] levels) { 158 int levelNum; 159 int maxLevelNum = 0; 160 161 for (int idx = 0; idx < levels.length; idx++) { 162 levelNum = getLevel(levels[idx]); 163 if (levelNum > maxLevelNum) { 164 maxLevelNum = levelNum; 165 } 166 } 167 return maxLevelNum; 168 } 169 170 171 172 184 185 public synchronized void configure(String monologConfFile) 186 throws ConfigException { 187 188 if (monologConfFile==null || monologConfFile.length() == 0) { 189 throw new ConfigException( 190 "impossible to configure monolog without configuration file"); 191 } 192 String b = null; 194 Properties p = new Properties (); 195 try { 196 197 p.load(new FileInputStream (monologConfFile)); 198 b = p.getProperty("log.config.classname", null); 200 201 if (b == null) { 202 throw new ConfigException("Malformed configuration log file:" 203 + " log.config.classname not available"); 204 } 205 206 lf = (LoggerFactory) Class.forName(b).newInstance(); 208 p.put(Configurable.LOG_CONFIGURATION_TYPE, Configurable.PROPERTY); 210 p.put(Configurable.LOG_CONFIGURATION_FILE, monologConfFile); 211 213 ((Configurable) lf).configure(p); 214 215 216 } 218 catch (Exception e) { 219 throw new ConfigException("Malformed configuration log file:" 220 + " log.config.classname not available"); 221 } 222 } 223 224 public synchronized void configure() 225 throws ConfigException { 226 String b = null; 227 String propkey; 228 String propvalue; 229 Properties props = new Properties (); 230 231 233 234 try{ 235 findObjectName(); 236 if (objectName != null) 237 PROPERTY_FILE = objectName.getKeyProperty("fname"); 238 rb = ResourceBundle.getBundle(PROPERTY_FILE,new Locale ("en","US"), ClassLoader.getSystemClassLoader()); 239 240 Enumeration enumeration = rb.getKeys(); 241 242 243 while (enumeration.hasMoreElements()) 244 { 245 246 propkey = (String ) enumeration.nextElement(); 247 propvalue = rb.getString(propkey); 248 props.setProperty(propkey, propvalue); 249 } 250 251 252 } catch (Exception e){ 253 throw new ConfigException("Logger configuration file could not be found: logger could not be initialized!"); 254 } 255 256 try { 257 258 b = props.getProperty("log.config.classname", null); 260 if (b == null) { 261 throw new ConfigException("Malformed configuration log file:" 262 + " log.config.classname not available"); 263 } 264 265 267 props.put(Configurable.LOG_CONFIGURATION_TYPE, Configurable.PROPERTY); 268 lf = Monolog.getMonologFactory(b); 269 270 } 272 catch (Exception e) { 273 throw new ConfigException("Malformed configuration log file:" 274 + " log.config.classname not available"); 275 } 276 277 } 278 279 280 283 private synchronized MonologLogChannel createChannel(String facility) { 284 285 String sepLine= (String )appSeparatorLine.get(facility); 286 if(sepLine==null) 287 sepLine=defaultSeparatorLine; 288 289 MonologLogChannel channel 290 = (MonologLogChannel) logChannels.get(facility); 291 292 if (channel == null) { 293 channel = new MonologLogChannel(facility, 294 lf.getLogger(facility),sepLine); 295 logChannels.put(facility, channel); 296 } 297 return channel; 298 } 299 300 306 public LogChannel getChannel(String facility) { 307 MonologLogChannel channel 308 = (MonologLogChannel) logChannels.get(facility); 309 310 if (channel == null) { 311 channel = createChannel(facility); 313 } 314 return channel; 315 } 316 317 320 private synchronized Integer createLevel(String level) { 321 Integer intLevel = (Integer ) levelNumbers.get(level); 322 323 if (intLevel == null) { 324 intLevel = new Integer (numLevels); 325 levelNames[numLevels] = level; 326 levelNumbers.put(level, intLevel); 327 numLevels++; 328 } 329 return intLevel; 330 } 331 332 339 public synchronized int getLevel(String level) { 340 Integer intLevel = (Integer ) levelNumbers.get(level); 341 342 if (intLevel == null) { 343 intLevel = createLevel(level); 345 } 346 return intLevel.intValue(); 347 } 348 349 355 public String getLevelName(int level) { 356 if ((level >= 0) && (level < numLevels)) { 357 return levelNames[level]; 358 } else { 359 return null; 360 } 361 } 362 363 368 public void configure(Config logConfig) throws ConfigException { 369 String fileName = null; 370 File logConfigFile = null; 371 if (logConfig.containsKey("Monolog")) { 372 fileName = logConfig.getString("Monolog"); 373 logConfigFile = new File (fileName); 374 if (!logConfigFile.exists()){ 375 fileName = logConfig.getConfigFile().getFile().getParent()+File.separator+fileName; 376 logConfigFile = new File (fileName); 377 if (!logConfigFile.exists()){ 378 fileName = logConfig.getConfigFile().getFile().getParent(); 379 if (null != fileName) { 380 fileName += File.separator + DEFAULT_LOG_CONFIG_FILE; 381 } 382 } 383 } 384 try{ 385 configure(fileName); 386 } catch (Exception ex) { 387 try { 388 configure(); 389 }catch (ConfigException cex) { 390 throw new ConfigException("Cannot configure logger. Config file is null."); 391 } 392 } 393 394 } else { 395 try { 396 configure(); 397 }catch (ConfigException ex) { 398 throw new ConfigException("Cannot configure logger. Logger not initialized."); 399 } 400 } 401 if (logConfig.containsKey("SeparatorLine")) { 402 String app=logConfig.getString("Server.AppClass"); 403 String separator = logConfig.getString("SeparatorLine"); 404 int index =app.lastIndexOf("."); 405 String temp=app.substring(index); 406 appSeparatorLine.put(temp.substring(1) ,separator); 407 } 408 409 } 410 411 412 415 private void findObjectName () throws Exception { 416 JMXConnector connector = null; 417 try { 418 419 String serviceName = "jonas"; 420 421 serviceName = System.getProperty("jonas.name"); 423 if ( serviceName == null) { 424 serviceName = "jonas"; 425 } 426 427 JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi://localhost/jndi/jrmpconnector_" + serviceName); 429 430 connector = JMXConnectorFactory.connect(url); 432 433 MBeanServerConnection connection = connector.getMBeanServerConnection(); 436 437 objectName = new ObjectName (serviceName + ":type=service,name=log,*"); 438 439 Set mBeans = connection.queryNames(objectName, null); 440 int l = mBeans.size(); 441 if ((l) > 1) { 442 throw new Exception ("MBean set size not equal 1: " + l); 443 }else if (l == 0){ 444 objectName = null; 445 if (connector!=null){ 446 try { 447 connector.close(); 448 } catch (Exception exept){} 449 } 450 return; 451 } 452 Iterator i = mBeans.iterator(); 453 objectName = (ObjectName )i.next(); 454 } catch (Exception e) { 455 throw new Exception (e); 456 } finally { 457 if (connector!=null){ 458 try { 459 connector.close(); 460 } catch (Exception exept){} 461 } 462 } 463 return; 464 } 465 466 } 467 468 469 470 | Popular Tags |