1 23 package com.lutris.logging; 24 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.util.Hashtable ; 29 30 import org.apache.log4j.BasicConfigurator; 31 import org.apache.log4j.ConsoleAppender; 32 import org.apache.log4j.Level; 33 import org.apache.log4j.Logger; 34 import org.apache.log4j.PatternLayout; 35 import org.apache.log4j.RollingFileAppender; 36 import org.apache.log4j.spi.LoggerRepository; 37 import org.apache.log4j.xml.DOMConfigurator; 38 39 import com.lutris.util.Config; 40 import com.lutris.util.ConfigException; 41 42 57 public class Log4jLogger extends com.lutris.logging.Logger { 58 59 64 private Hashtable levelNumbers = new Hashtable (); 65 66 70 protected String [] levelNames = new String [MAX_STD_LEVEL * 2]; 71 protected int numLevels = 0; 72 73 77 protected boolean[] enabledLevelFlags = null; 78 79 83 protected boolean[] logFileLevelFlags = null; 84 85 91 protected boolean[] stderrLevelFlags = null; 92 93 96 protected String DEFAULT_LOG_CONFIG_FILE = "log4j.xml"; 97 98 101 File activeLogFile; 102 103 106 PrintWriter logFileStream; 107 108 111 PrintWriter stderrStream; 112 113 117 private Hashtable logChannels = new Hashtable (); 118 119 String defaultSeparatorLine =""; 120 Hashtable appSeparatorLine = new Hashtable (); 121 122 128 public Log4jLogger(boolean makeCentral) { 129 int level; 130 131 for (level = 0; level <= MAX_STD_LEVEL; level++) { 132 String name = standardLevelNames[level]; 133 134 levelNumbers.put(name, new Integer (level)); 135 levelNames[level] = name; 136 } 137 numLevels = level; 138 if (makeCentral) { 139 centralLogger = this; 140 } 141 } 142 143 149 private int getMaxLevel(String [] levels) { 150 int levelNum; 151 int maxLevelNum = 0; 152 153 for (int idx = 0; idx < levels.length; idx++) { 154 levelNum = getLevel(levels[idx]); 155 if (levelNum > maxLevelNum) { 156 maxLevelNum = levelNum; 157 } 158 } 159 return maxLevelNum; 160 } 161 162 169 private boolean[] getLevelStateArray(String [] levels, int maxLevelNum) { 170 int levelNum; 171 boolean[] levelNums = new boolean[maxLevelNum + 1]; 173 174 for (int idx = 0; idx < levels.length; idx++) { 175 levelNums[getLevel(levels[idx])] = true; 176 } 177 return levelNums; 178 } 179 180 192 public synchronized void configure(String log4jConfFile) 193 throws ConfigException { 194 try { 195 DOMConfigurator.configure(log4jConfFile); 196 } catch (javax.xml.parsers.FactoryConfigurationError fce) { 197 throw new ConfigException("Cannot configure Log4jLogger:", 198 fce); 199 } 200 201 } 202 203 207 static private void configureLogger() { 208 BasicConfigurator.configure(); 210 Logger root = Logger.getRootLogger(); 211 212 root.removeAllAppenders(); 214 LoggerRepository repository = root.getLoggerRepository(); 215 216 repository.setThreshold(Level.INFO); 218 RollingFileAppender multiserverAppender = null; 219 RollingFileAppender accessAppender = null; 220 ConsoleAppender conAppender = null; 222 String enhydraLogs = System.getProperty("enhydra.home") 224 + File.separator + "logs" + File.separator; 225 String multiserverLogFile = enhydraLogs + "multiserver.log"; 226 String accessLogFile = enhydraLogs + "access.log"; 227 228 try { 229 230 239 multiserverAppender = new RollingFileAppender(new PatternLayout("%d{ISO8601}: [%t], %C(1) %p, %c: %m%n"), 240 multiserverLogFile); 241 accessAppender = new RollingFileAppender(new PatternLayout("%m%n"), 242 accessLogFile); 243 conAppender = new ConsoleAppender(new PatternLayout("%d{ISO8601}: [%t], %C(1) %p, %c: %m%n")); 244 } catch (IOException e) { 245 e.printStackTrace(); 246 } 247 root.addAppender(multiserverAppender); 248 Logger requestLogger = Logger.getLogger("REQUEST"); 250 251 requestLogger.setAdditivity(false); 253 requestLogger.addAppender(accessAppender); 254 Logger sysOutLogger = Logger.getLogger("SysOut"); 255 256 sysOutLogger.setAdditivity(false); 257 sysOutLogger.addAppender(conAppender); 258 } 259 260 263 private synchronized Log4jLogChannel createChannel(String facility) { 264 265 String sepLine= (String )appSeparatorLine.get(facility); 266 if(sepLine==null) 267 sepLine=defaultSeparatorLine; 268 Log4jLogChannel channel 269 = (Log4jLogChannel) logChannels.get(facility); 270 271 if (channel == null) { 272 channel = new Log4jLogChannel(facility, 273 Logger.getLogger(facility),sepLine); 274 logChannels.put(facility, channel); 275 } 276 277 return channel; 278 } 279 280 286 public LogChannel getChannel(String facility) { 287 288 289 Log4jLogChannel channel 290 = (Log4jLogChannel) logChannels.get(facility); 291 292 if (channel == null) { 293 channel = createChannel(facility); 295 } 296 return channel; 297 } 298 299 302 private synchronized Integer createLevel(String level) { 303 Integer intLevel = (Integer ) levelNumbers.get(level); 304 305 if (intLevel == null) { 306 intLevel = new Integer (numLevels); 307 levelNames[numLevels] = level; 308 levelNumbers.put(level, intLevel); 309 numLevels++; 310 } 311 return intLevel; 312 } 313 314 321 public synchronized int getLevel(String level) { 322 Integer intLevel = (Integer ) levelNumbers.get(level); 323 324 if (intLevel == null) { 325 intLevel = createLevel(level); 327 } 328 return intLevel.intValue(); 329 } 330 331 337 public String getLevelName(int level) { 338 if ((level >= 0) && (level < numLevels)) { 339 return levelNames[level]; 340 } else { 341 return null; 342 } 343 } 344 345 350 public void configure(Config logConfig) throws ConfigException { 351 352 String fileName = null; 353 File logConfigFile = null; 354 if (logConfig.containsKey("Log4j")) { 355 fileName = logConfig.getString("Log4j"); 356 logConfigFile = new File (fileName); 357 if (!logConfigFile.exists()){ 358 fileName = logConfig.getConfigFile().getFile().getParent()+File.separator+fileName; 359 logConfigFile = new File (fileName); 360 if (!logConfigFile.exists()){ 361 fileName = logConfig.getConfigFile().getFile().getParent(); 362 if (null != fileName) { 363 fileName += File.separator + DEFAULT_LOG_CONFIG_FILE; 364 } 365 } 366 } 367 368 } else if (null != logConfig.getConfigFile()) { 369 fileName = logConfig.getConfigFile().getFile().getParent(); 370 if (null != fileName) { 371 fileName += File.separator + DEFAULT_LOG_CONFIG_FILE; 372 } 373 } else { 374 throw new ConfigException("Cannot configure logger. Config file is null."); 375 } 376 if (logConfig.containsKey("SeparatorLine")) { 377 378 String app=logConfig.getString("Server.AppClass"); 379 String separator = logConfig.getString("SeparatorLine"); 380 int index =app.lastIndexOf("."); 381 String temp=app.substring(index); 382 appSeparatorLine.put(temp.substring(1) ,separator); 383 } 384 configure(fileName); 385 } 386 } 387 | Popular Tags |