1 package org.exoplatform.services.log.impl; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.exoplatform.commons.utils.ExceptionUtil; 9 import org.exoplatform.services.log.LogMessage; 10 import org.exoplatform.container.PortalContainer; 11 15 16 23 public class ExoLog implements Log { 24 static public int FATAL = 0 ; 25 static public int ERROR = 1 ; 26 static public int WARN = 2 ; 27 static public int INFO = 3 ; 28 static public int DEBUG = 4 ; 29 static public int TRACE = 5 ; 30 31 static private Log log_ = LogFactory.getLog("[eXo]"); 32 33 static public int LOG_BUFFER_SIZE = 2000 ; 34 static public int ERROR_BUFFER_SIZE = 1500 ; 35 36 static private List logBuffer_ = new ArrayList (LOG_BUFFER_SIZE * 2) ; 37 static private List errorBuffer_ = new ArrayList (ERROR_BUFFER_SIZE * 2) ; 38 39 private String category_ ; 40 private int level_ ; 41 private String prefix_ ; 42 43 44 public ExoLog(String name, int level) { 45 category_ = name ; 46 level_ = level ; 47 int index = name.lastIndexOf(".") ; 48 String nameSuffix = name ; 49 if (index > 0 ) { 50 nameSuffix = name.substring(index + 1 , name.length()) ; 51 } 52 prefix_ = "[" + nameSuffix + "] " ; 53 } 54 55 public String getLogCategory() { return category_ ; } 56 57 public int getLevel() { return level_ ; } 58 public void setLevel(int level) { level_ = level ; } 59 60 public void trace(Object message) { 61 log_.trace(prefix_ + message); 62 } 63 64 public void trace(Object message, Throwable t) { 65 log_.trace(prefix_ + message, t); 66 } 67 68 public void debug(Object message) { 69 if (level_ >= DEBUG) { 70 log_.debug(prefix_ + message); 71 addLogMessage(new LogMessage(prefix_ , DEBUG, message.toString(), null)) ; 72 } 73 } 74 75 public void debug(Object message, Throwable t) { 76 if (level_ >= DEBUG) { 77 log_.debug(prefix_ + message, t); 78 String strace = ExceptionUtil.getExoStackTrace(t) ; 79 addLogMessage(new LogMessage(prefix_ , DEBUG, message.toString(), strace )) ; 80 } 81 } 82 83 public void info(Object message) { 84 if (level_ >= INFO) { 85 log_.info(prefix_ + message); 86 addLogMessage(new LogMessage(prefix_ , INFO, message.toString(), null)) ; 87 } 88 } 89 90 public void info(Object message, Throwable t) { 91 if (level_ >= INFO) { 92 log_.info(prefix_ + message, t); 93 String strace = ExceptionUtil.getExoStackTrace(t) ; 94 addLogMessage(new LogMessage(prefix_ , INFO, message.toString(), strace )) ; 95 } 96 } 97 98 public void warn(Object message) { 99 if (level_ >= WARN) { 100 log_.warn(prefix_ + message); 101 addLogMessage(new LogMessage(prefix_ , WARN, message.toString(), null )) ; 102 } 103 } 104 105 public void warn(Object message, Throwable t) { 106 if (level_ >= WARN) { 107 log_.warn(prefix_ + message, t); 108 String strace = ExceptionUtil.getExoStackTrace(t) ; 109 addLogMessage(new LogMessage(prefix_ , WARN, message.toString(), strace )) ; 110 } 111 } 112 113 public void error(Object message) { 114 if (level_ >= ERROR) { 115 log_.error(prefix_ + message); 116 addLogMessage(new LogMessage(prefix_ , ERROR, message.toString(), null )) ; 117 PortalContainer pcontainer = PortalContainer.getInstance() ; 118 if(pcontainer != null) { 119 pcontainer.getMonitor().error(message.toString(), null) ; 120 } 121 } 122 } 123 124 public void error(Object message, Throwable t) { 125 if (level_ >= ERROR) { 126 log_.error(prefix_ + message, t); 127 String strace = ExceptionUtil.getExoStackTrace(t) ; 128 addLogMessage(new LogMessage(prefix_ , ERROR, message.toString(), strace )) ; 129 PortalContainer pcontainer = PortalContainer.getInstance() ; 130 if(pcontainer != null) { 131 pcontainer.getMonitor().error(message.toString(), t) ; 132 } 133 } 134 } 135 136 public void fatal(Object message) { 137 if (level_ >= FATAL) { 138 log_.fatal(prefix_ + message); 139 addLogMessage(new LogMessage(prefix_ , FATAL, message.toString(), null)) ; 140 } 141 } 142 143 public void fatal(Object message, Throwable t) { 144 if (level_ >= FATAL) { 145 log_.fatal(prefix_ + message, t); 146 String strace = ExceptionUtil.getExoStackTrace(t) ; 147 addLogMessage(new LogMessage(prefix_ , FATAL, message.toString(), strace )) ; 148 } 149 } 150 151 public final boolean isDebugEnabled() { return level_ >= DEBUG; } 152 153 public final boolean isErrorEnabled() { return level_ >= ERROR; } 154 155 public final boolean isFatalEnabled() { return level_ >= FATAL; } 156 157 public final boolean isInfoEnabled() { return level_ >= INFO; } 158 159 public final boolean isTraceEnabled() { return level_ >= TRACE; } 160 161 public final boolean isWarnEnabled() { return level_ >= WARN; } 162 163 private void addLogMessage(LogMessage lm) { 164 logBuffer_.add(lm) ; 165 if (logBuffer_.size() == LOG_BUFFER_SIZE * 2) { 166 List list = new ArrayList (LOG_BUFFER_SIZE * 2) ; 167 for (int i = LOG_BUFFER_SIZE; i < logBuffer_.size(); i++) { 168 list.add(logBuffer_.get(i)) ; 169 } 170 logBuffer_ = list ; 171 } 172 173 if (lm.getType() == ERROR) { 174 errorBuffer_.add(lm) ; 175 if (errorBuffer_.size() == ERROR_BUFFER_SIZE * 2) { 176 List list = new ArrayList (ERROR_BUFFER_SIZE * 2) ; 177 for (int i = ERROR_BUFFER_SIZE; i < errorBuffer_.size(); i++) { 178 list.add(errorBuffer_.get(i)) ; 179 } 180 errorBuffer_ = list ; 181 } 182 } 183 } 184 185 static public List getLogBuffer() { return logBuffer_ ; } 186 static public List getErrorBuffer() { return errorBuffer_ ; } 187 } | Popular Tags |