1 20 package org.objectweb.modfact.jmi.logging; 21 22 import java.io.IOException ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.Properties ; 26 27 import org.objectweb.util.monolog.Monolog; 28 import org.objectweb.util.monolog.api.Logger; 29 import org.objectweb.util.monolog.api.BasicLevel; 30 import org.objectweb.util.monolog.api.LoggerFactory; 31 32 39 public class ModFactLogger { 40 41 42 private Logger logger; 43 44 45 private static LoggerFactory loggerFactory = getLoggerFactory(); 46 47 48 private static boolean isQuiet = false; 49 50 51 private static String configFile; 52 53 57 public ModFactLogger(String name) { 58 if (loggerFactory != null) 59 logger = loggerFactory.getLogger(name); 60 } 61 62 67 public void log(int level, String message) { 68 if (logger != null && !isQuiet) { 69 switch (level) { 70 case Level.FATAL : 71 logger.log(BasicLevel.FATAL, message); 72 break; 73 case Level.ERROR : 74 logger.log(BasicLevel.ERROR, message); 75 break; 76 case Level.WARN : 77 logger.log(BasicLevel.WARN, message); 78 break; 79 case Level.INFO : 80 logger.log(BasicLevel.INFO, message); 81 break; 82 case Level.FINE : 83 logger.log(BasicLevel.DEBUG, message); 84 break; 85 case Level.FINER : 86 logger.log(BasicLevel.INHERIT, message); 87 break; 88 } 89 } 90 } 91 92 97 public static ModFactLogger getLogger(String name) { 98 return new ModFactLogger(name); 99 } 100 101 104 public static void init() { 105 configFile = System.getProperty(Monolog.MONOLOG_FILE_NAME, Monolog.DEFAULT_MONOLOG_FILE); 106 Monolog.init(); 107 } 108 109 119 public static LoggerFactory getLoggerFactory() { 120 init(); 121 Properties props = new Properties (); 123 String loggerFactoryName = null; 124 try { 125 props.load(new java.io.FileInputStream (configFile)); 126 loggerFactoryName = props.getProperty("log.config.classname", null); 127 } catch (IOException ioe) { 128 try { 130 String url = System.getProperty("org.objectweb.modfact.resourceURL")+"/"+configFile; 131 URL webPath = new URL (url); 132 props.load(webPath.openStream()); 133 loggerFactoryName = props.getProperty("log.config.classname", null); 134 } 135 catch (MalformedURLException ex) { 136 } 137 catch (IOException ex) { 138 } 139 } 140 141 if (loggerFactoryName == null) { 142 loggerFactoryName = System.getProperty("log.config.classname", 143 "org.objectweb.util.monolog.wrapper.javaLog.LoggerFactory"); 144 } 145 try { 146 LoggerFactory lf = (LoggerFactory) Class.forName(loggerFactoryName).newInstance(); 147 return lf; 148 } catch (Exception e) { 149 System.err.println("The logger factory class name is not correct."); 150 return null; 151 } 152 } 153 154 159 public static void setQuiet (boolean quietMode) { 160 isQuiet = quietMode; 161 } 162 163 } 164 | Popular Tags |