1 16 17 18 19 package org.apache.fop.util; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 27 public class CommandLineLogger implements Log { 28 29 public static final int LOG_LEVEL_TRACE = 1; 30 31 public static final int LOG_LEVEL_DEBUG = 2; 32 33 public static final int LOG_LEVEL_INFO = 3; 34 35 public static final int LOG_LEVEL_WARN = 4; 36 37 public static final int LOG_LEVEL_ERROR = 5; 38 39 public static final int LOG_LEVEL_FATAL = 6; 40 41 private int logLevel; 42 private String logName; 43 44 49 public CommandLineLogger(String logName) { 50 this.logName = logName; 51 setLogLevel((String ) LogFactory.getFactory().getAttribute("level")); 52 } 53 54 58 public void setLogLevel(String level) { 59 if ("fatal".equals(level)) { 60 logLevel = LOG_LEVEL_FATAL; 61 } else if ("error".equals(level)) { 62 logLevel = LOG_LEVEL_ERROR; 63 } else if ("warn".equals(level)) { 64 logLevel = LOG_LEVEL_WARN; 65 } else if ("info".equals(level)) { 66 logLevel = LOG_LEVEL_INFO; 67 } else if ("debug".equals(level)) { 68 logLevel = LOG_LEVEL_DEBUG; 69 } else if ("trace".equals(level)) { 70 logLevel = LOG_LEVEL_TRACE; 71 } else { 72 logLevel = LOG_LEVEL_INFO; 73 } 74 } 75 76 79 public final boolean isTraceEnabled() { 80 return logLevel <= LOG_LEVEL_TRACE; 81 } 82 83 86 public final boolean isDebugEnabled() { 87 return logLevel <= LOG_LEVEL_DEBUG; 88 } 89 90 93 public final boolean isInfoEnabled() { 94 return logLevel <= LOG_LEVEL_INFO; 95 } 96 97 100 public final boolean isWarnEnabled() { 101 return logLevel <= LOG_LEVEL_WARN; 102 } 103 104 107 public final boolean isErrorEnabled() { 108 return logLevel <= LOG_LEVEL_ERROR; 109 } 110 111 114 public final boolean isFatalEnabled() { 115 return logLevel <= LOG_LEVEL_FATAL; 116 } 117 118 121 public final void trace(Object message) { 122 if (isTraceEnabled()) { 123 log(LOG_LEVEL_TRACE, message, null); 124 } 125 } 126 127 130 public final void trace(Object message, Throwable t) { 131 if (isTraceEnabled()) { 132 log(LOG_LEVEL_TRACE, message, t); 133 } 134 } 135 136 139 public final void debug(Object message) { 140 if (isDebugEnabled()) { 141 log(LOG_LEVEL_DEBUG, message, null); 142 } 143 } 144 145 148 public final void debug(Object message, Throwable t) { 149 if (isDebugEnabled()) { 150 log(LOG_LEVEL_DEBUG, message, t); 151 } 152 } 153 154 157 public final void info(Object message) { 158 if (isInfoEnabled()) { 159 log(LOG_LEVEL_INFO, message, null); 160 } 161 } 162 163 166 public final void info(Object message, Throwable t) { 167 if (isInfoEnabled()) { 168 log(LOG_LEVEL_INFO, message, t); 169 } 170 } 171 172 175 public final void warn(Object message) { 176 if (isWarnEnabled()) { 177 log(LOG_LEVEL_WARN, message, null); 178 } 179 } 180 181 184 public final void warn(Object message, Throwable t) { 185 if (isWarnEnabled()) { 186 log(LOG_LEVEL_WARN, message, t); 187 } 188 } 189 190 193 public final void error(Object message) { 194 if (isErrorEnabled()) { 195 log(LOG_LEVEL_ERROR, message, null); 196 } 197 } 198 199 202 public final void error(Object message, Throwable t) { 203 if (isErrorEnabled()) { 204 log(LOG_LEVEL_ERROR, message, t); 205 } 206 } 207 208 211 public final void fatal(Object message) { 212 if (isFatalEnabled()) { 213 log(LOG_LEVEL_FATAL, message, null); 214 } 215 } 216 217 220 public final void fatal(Object message, Throwable t) { 221 if (isFatalEnabled()) { 222 log(LOG_LEVEL_FATAL, message, t); 223 } 224 } 225 226 235 protected void log(int type, Object message, Throwable t) { 236 StringBuffer buf = new StringBuffer (); 237 buf.append(String.valueOf(message)); 239 if (t != null) { 240 buf.append("\n"); 241 if (!isDebugEnabled()) { 243 buf.append(t.toString()); 244 buf.append("\n"); 245 } else { 246 java.io.StringWriter sw = new java.io.StringWriter (1024); 247 java.io.PrintWriter pw = new java.io.PrintWriter (sw); 248 t.printStackTrace(pw); 249 pw.close(); 250 buf.append(sw.toString()); 251 } 252 } 253 254 if (type >= LOG_LEVEL_WARN) { 256 System.err.println(buf); 257 } else { 258 System.out.println(buf); 259 } 260 261 } 262 } 263 | Popular Tags |