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 33 public class StandardLogger 34 extends Logger { 35 36 39 File activeLogFile; 40 41 44 File logDir; 45 46 49 PrintWriter logFileStream; 50 51 private boolean[] enabledLogLevels = new boolean[3]; 52 private Hashtable messages = new Hashtable(); 53 private String logMode; 54 public RandomAccessFile randomLoggerFile; 55 56 60 public StandardLogger() { 61 centralLogger = this; 62 this.enabledLogLevels[0] = false; 63 this.enabledLogLevels[1] = false; 64 this.enabledLogLevels[2] = false; 65 } 66 67 73 public void configure(String confFilePath) throws Exception { 74 int num = confFilePath.indexOf(";"); 75 String logDir = confFilePath.substring(0, num); 76 String fileName = confFilePath.substring(num + 1); 77 78 String strPi; 79 Calendar calendar = Calendar.getInstance(); 80 Date currentDate = new Date(); 81 calendar.setTime(currentDate); 82 int year, month, iDate, iDay, hours, minutes, seconds; 83 int y, h, min, s; 84 y = calendar.get(Calendar.YEAR); 85 month = calendar.get(Calendar.MONTH); 86 month = month + 1; 87 String strMonth = null; 88 if (month < 10) 89 strMonth = "0" + month; 90 else 91 strMonth = "" + month; 92 93 String [] months = new String [] { 94 "January", "February", "March", "April", "May", "June", "July", 95 "August", "September", "October", "November", "December" 96 }; 97 iDate = calendar.get(Calendar.DAY_OF_MONTH); 98 String strDate = ""; 99 if (iDate < 10) 100 strDate = "0" + iDate; 101 else 102 strDate = "" + iDate; 103 104 iDay = calendar.get(Calendar.DAY_OF_WEEK) - 1; 105 String [] days = new String [] { 106 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 107 "Saturday" 108 }; 109 hours = calendar.get(Calendar.HOUR_OF_DAY); 110 String strHours = ""; 111 if (hours < 10) 112 strHours = "0" + hours; 113 else 114 strHours = "" + hours; 115 116 h = hours + 1; 117 118 minutes = calendar.get(Calendar.MINUTE); 119 String strMinutes = ""; 120 if (minutes < 10) 121 strMinutes = "0" + minutes; 122 else 123 strMinutes = "" + minutes; 124 125 h = hours + 1; 126 127 min = minutes + 1; 128 seconds = calendar.get(Calendar.SECOND); 129 String strSeconds = ""; 130 if (seconds < 10) 131 strSeconds = "0" + seconds; 132 else 133 strSeconds = "" + seconds; 134 135 s = seconds + 1; 136 if (fileName.equalsIgnoreCase("default")){ 137 138 strPi = "LoaderLog" + y + "-" + strMonth + "-" + strDate 139 + "-" + strHours + "-" + strMinutes + "-" + strSeconds + ".txt"; 140 }else if(fileName.equalsIgnoreCase("defaultGenerator")){ 141 strPi = "GeneratorLog" + y + "-" + strMonth + "-" + strDate 142 + "-" + strHours + "-" + strMinutes + "-" + strSeconds + ".txt"; 143 } 144 else 145 strPi = fileName; 146 try { 147 File filea = new File(logDir); 148 this.logDir = filea; 149 filea = new File(filea.getAbsolutePath(), strPi); 152 this.activeLogFile = filea.getAbsoluteFile(); 154 159 } 160 catch (Exception ex) { 161 throw new Exception ("Cannot configure StandardLogger:" + ex.getMessage()); 162 } 163 164 } 165 166 public int getLevel(String level) { 167 if (level.equalsIgnoreCase(Logger.strLOGMODE_NONE)) 168 return 0; 169 else if (level.equalsIgnoreCase(Logger.strLOGMODE_NORMAL)) 170 return 1; 171 else 172 return 2; 173 } 174 175 public boolean isEnabled(int level) { 176 boolean[] enabledLevelsValue = this.getEnabledLogLevels(); 177 if (enabledLevelsValue[level] == true) { 178 return true; 179 } 180 return false; 181 } 182 183 public boolean isEnabled(String level) { 184 return isEnabled(this.getLevel(level)); 185 } 186 187 public void write(int level, String msg) { 188 if (isEnabled(level)) { 189 try { 190 if (this.activeLogFile != null) { 191 if(!this.logDir.exists()) 193 this.logDir.mkdirs(); 194 if( this.randomLoggerFile == null ) 196 this.randomLoggerFile = new RandomAccessFile(this.activeLogFile, "rw"); 197 randomLoggerFile.seek(randomLoggerFile.length()); 199 System.out.println(msg + "\n"); 200 BufferOctopusClass.getInstance().writeToBuffer(msg + "\n"); 201 randomLoggerFile.writeBytes(msg + "\n"); 202 } 203 } 204 catch (Exception e) { 205 BufferOctopusClass.getInstance().writeToBuffer(e.getMessage() + "\n"); 206 e.printStackTrace(); 207 } 208 } 209 } 210 211 public synchronized void write(String level, String msg) { 212 write(getLevel(level), msg); 213 } 214 215 public synchronized void write(int level, String msg, Throwable throwable) { 216 if (isEnabled(level)) { 217 Date date = new Date(); 218 StringWriter stackBuf = new StringWriter(); 219 throwable.printStackTrace(new PrintWriter(stackBuf)); 220 stackBuf.flush(); 221 222 String errMsg = msg + ":" + " " + throwable.getMessage() + '\n' + stackBuf; 223 this.write(level, errMsg); 224 } 225 } 226 227 public synchronized void write(String level, String msg, Throwable throwable) { 228 write(getLevel(level), msg, throwable); 229 } 230 231 public void setEnabledLogLevels(String logMode) { 232 this.logMode = logMode; 233 int level = this.getLevel(logMode); 234 if (level == 0) { 235 this.enabledLogLevels[0] = false; 236 this.enabledLogLevels[1] = false; 237 this.enabledLogLevels[2] = false; 238 } else if (level == 1) { 239 this.enabledLogLevels[0] = true; 240 this.enabledLogLevels[1] = true; 241 this.enabledLogLevels[2] = false; 242 } else { 243 this.enabledLogLevels[0] = true; 244 this.enabledLogLevels[1] = true; 245 this.enabledLogLevels[2] = true; 246 } 247 } 248 249 public boolean[] getEnabledLogLevels() { 250 return enabledLogLevels; 251 } 252 253 public String getMessage(String key) { 254 if (key != null) { 255 return (String )this.messages.get(key); 256 } else 257 return null; 258 } 259 260 public boolean setMessage(String key, String value) { 261 if (value != null && key != null) { 262 this.messages.put(key, value); 263 return true; 264 } else 265 return false; 266 } 267 268 public boolean writeEcho(String strLogTxt) { 269 if (!this.logMode.equalsIgnoreCase(Logger.strLOGMODE_NONE)) { 270 this.write(Logger.strLOGMODE_NORMAL, strLogTxt); 271 BufferOctopusClass.getInstance().writeToBuffer(strLogTxt); 272 return true; 273 } else 274 return false; 275 } 276 277 public void close() { 278 try { 279 if(this.activeLogFile!=null) 280 this.activeLogFile = null; 281 this.randomLoggerFile.close(); 282 } 283 catch (Exception e){ 284 BufferOctopusClass.getInstance().writeToBuffer(e.getMessage()); 285 } 286 } 287 288 289 290 } 291 | Popular Tags |