1 64 65 70 package com.jcorporate.expresso.core.logging; 71 72 import com.jcorporate.expresso.core.db.DBException; 73 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 74 import com.jcorporate.expresso.services.dbobj.LogEntry; 75 import org.apache.log4j.AppenderSkeleton; 76 import org.apache.log4j.Priority; 77 import org.apache.log4j.helpers.LogLog; 78 import org.apache.log4j.spi.LoggingEvent; 79 80 import java.io.ByteArrayOutputStream ; 81 import java.io.PrintStream ; 82 83 84 89 public class DBAppender 90 extends AppenderSkeleton { 91 private static final int ERROR = Priority.ERROR.toInt(); 92 private static final int WARN = Priority.WARN.toInt(); 93 private static final int INFO = Priority.INFO.toInt(); 94 private static final int DEBUG = Priority.DEBUG.toInt(); 95 private static final String thisClass = "com.jcorporate.expresso.core.logging.DBAppender."; 96 97 100 public DBAppender() { 101 } 102 103 106 public boolean requiresLayout() { 107 return true; 108 } 109 110 115 protected void append(LoggingEvent parm1) { 116 final String myName = "com.jcorporate.expresso.core.logging.DBAppender"; 117 String theMessage = layout.format(parm1); 118 String theColor = "R"; 119 120 int priorityInt = parm1.getLevel().toInt(); 122 123 124 125 if (priorityInt <= DEBUG) { 127 theColor = "G"; 128 } else if (priorityInt <= INFO) { 129 theColor = "B"; 130 } else if (priorityInt <= WARN) { 131 theColor = "Y"; 132 } else if (priorityInt <= ERROR) { 133 theColor = "R"; 134 } 135 136 priorityInt /= 10000; 138 139 try { 140 141 log(priorityInt, parm1.getLoggerName(), theMessage, theColor); 143 144 if (layout.ignoresThrowable() && 148 parm1.getThrowableInformation() != null) { 149 log(parm1.getLoggerName(), 150 parm1.getThrowableInformation().getThrowable()); 151 } 152 } catch (LogException e) { 153 154 LogLog.error(myName + ":Unable to log message '" + theMessage + 156 "' from category '" + parm1.getLoggerName() + "':", e); 157 } 158 } 159 160 167 protected static void log(String objectName, Throwable e) 168 throws LogException { 169 String myName = (thisClass + "log(String, Exception)"); 170 String message = e.getMessage(); 171 172 if (e instanceof DBException) { 173 DBException de = (DBException) e; 174 message = de.getMessage() + ":" + de.getDBMessage(); 175 } 176 try { 177 LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT); 178 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 179 e.printStackTrace(new PrintStream (bos)); 180 myLog.setField("MessageText", bos.toString()); 181 myLog.setField("MessageColor", "R"); 182 myLog.setField("ObjectName", objectName); 183 LogHandler.staticAddToQueue(myLog); 184 } catch (DBException de) { 185 throw new LogException(myName + ":" + de.getMessage()); 186 } 187 } 188 189 190 200 protected static void log(int messageLevel, String objectName, String msg, 201 String color) 202 throws LogException { 203 String myName = (thisClass + "log(int, String, String, " + 204 "String)"); 205 206 try { 207 LogEntry myLog = new LogEntry(SecuredDBObject.SYSTEM_ACCOUNT); 208 myLog.setField("ObjectName", objectName); 209 myLog.setField("MessageText", msg); 210 myLog.setField("MessageColor", color); 211 myLog.setField("MessageLevel", "" + messageLevel); 212 myLog.setTimeStamp(); 213 LogHandler.staticAddToQueue(myLog); 214 } catch (DBException de) { 215 216 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 218 de.printStackTrace(new PrintStream (bos)); 219 LogLog.error(myName + ":Unable to log '" + msg + "'" + 220 bos.toString()); 221 } 222 } 223 224 225 228 public void close() { 229 final String myName = thisClass + "close()"; 230 231 try { 232 LogHandler.flush(); 233 } catch (LogException e) { 234 235 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 237 e.printStackTrace(new PrintStream (bos)); 238 LogLog.error(myName + ":Unable to flush LogHandler '" + 239 e.getMessage() + "'" + bos.toString()); 240 } 241 } 242 243 } 244 | Popular Tags |