1 15 package org.webdocwf.util.loader.logging; 16 17 import java.util.*; 18 import java.io.*; 19 import org.webdocwf.util.loader.BufferOctopusClass; 20 21 import java.util.Hashtable ; 22 import java.io.*; 23 import org.apache.log4j.BasicConfigurator; 24 import org.apache.log4j.ConsoleAppender; 25 import org.apache.log4j.Level; 26 import org.apache.log4j.Logger; 27 import org.apache.log4j.PatternLayout; 28 import org.apache.log4j.RollingFileAppender; 29 import org.apache.log4j.spi.LoggerRepository; 30 import org.apache.log4j.xml.DOMConfigurator; 31 32 public class Log4jLogger 33 extends org.webdocwf.util.loader.logging.Logger{ 34 35 38 File activeLogFile; 39 40 43 PrintWriter logFileStream; 44 45 private boolean[] enabledLogLevels = new boolean[3]; 46 private Hashtable messages = new Hashtable (); 47 private String logMode; 48 private org.apache.log4j.Logger logger; 49 50 54 public Log4jLogger() { 55 centralLogger = this; 56 } 57 58 65 public void configure(String log4jConfFile) throws Exception { 66 67 try { 68 DOMConfigurator.configure(log4jConfFile); 69 this.logger=Logger.getLogger("OctopusLogger"); 70 } 71 catch (javax.xml.parsers.FactoryConfigurationError fce) { 72 throw new Exception ("Cannot configure Log4jLogger! FactoryConfigurationError occured!"); 73 74 } 75 } 76 77 public int getLevel(String level) { 78 if (level.equalsIgnoreCase(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NONE)) 79 return 0; 80 else if (level.equalsIgnoreCase(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NORMAL)) 81 return 1; 82 else 83 return 2; 84 } 85 86 public boolean isEnabled(int level) { 87 return true; 88 } 89 90 public boolean isEnabled(String level) { 91 return true; 93 } 94 95 public void write(int level, String msg) { 96 Level lev; 97 if (this.logger == null) { 98 this.logger = Logger.getLogger("OctopusLogger"); 99 } 100 if(level==1) 101 lev=Level.INFO; 102 else if( level == 2 ) 103 lev=Level.DEBUG; 104 else 105 lev = Level.INFO; 106 107 logger.log(lev, msg); 108 } 109 110 public synchronized void write(String level, String msg) { 111 write(getLevel(level), msg); 112 } 113 114 public synchronized void write(int level, String msg, Throwable throwable) { 115 Date date = new Date(); 116 StringWriter stackBuf = new StringWriter(); 117 throwable.printStackTrace(new PrintWriter(stackBuf)); 118 stackBuf.flush(); 119 120 String errMsg = msg + ":" + " " + throwable.getMessage() + '\n' + stackBuf; 121 this.write(level, errMsg); 122 123 } 124 125 public synchronized void write(String level, String msg, Throwable throwable) { 126 write(getLevel(level), msg, throwable); 127 } 128 129 public void setEnabledLogLevels(String logMode) { 130 } 131 132 public boolean[] getEnabledLogLevels() { 133 return enabledLogLevels; 134 } 135 136 public String getMessage(String key) { 137 if (key != null) { 138 return (String )this.messages.get(key); 139 } else 140 return null; 141 } 142 143 public boolean setMessage(String key, String value) { 144 if (value != null && key != null) { 145 this.messages.put(key, value); 146 return true; 147 } else 148 return false; 149 } 150 151 public boolean writeEcho(String strLogTxt) { 152 if(!this.logMode.equalsIgnoreCase(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NONE)) { 153 this.write(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NORMAL,strLogTxt); 154 BufferOctopusClass.getInstance().writeToBuffer(strLogTxt); 155 return true; 156 } 157 else 158 return false; 159 } 160 161 public void close(){ 162 } 164 } 165 | Popular Tags |