1 16 package org.directwebremoting.util; 17 18 import javax.servlet.http.HttpServlet ; 19 20 25 public class ServletLoggingOutput implements LoggingOutput 26 { 27 30 public void debug(String message) 31 { 32 log(LEVEL_DEBUG, message, null); 33 } 34 35 38 public void info(String message) 39 { 40 log(LEVEL_INFO, message, null); 41 } 42 43 46 public void warn(String message) 47 { 48 log(LEVEL_WARN, message, null); 49 } 50 51 54 public void warn(String message, Throwable th) 55 { 56 log(LEVEL_WARN, message, th); 57 } 58 59 62 public void error(String message) 63 { 64 log(LEVEL_ERROR, message, null); 65 } 66 67 70 public void error(String message, Throwable th) 71 { 72 log(LEVEL_ERROR, message, th); 73 } 74 75 78 public void fatal(String message) 79 { 80 log(LEVEL_FATAL, message, null); 81 } 82 83 86 public void fatal(String message, Throwable th) 87 { 88 log(LEVEL_FATAL, message, th); 89 } 90 91 97 private static void log(int loglevel, String message, Throwable th) 98 { 99 if (loglevel >= level) 100 { 101 HttpServlet servlet = (HttpServlet ) servlets.get(); 102 if (servlet != null) 103 { 104 if (th == null) 106 { 107 servlet.log(message); 108 } 109 else 110 { 111 servlet.log(message, th); 112 } 113 } 114 else 115 { 116 if (message != null) 117 { 118 System.out.println(message); 119 } 120 121 if (th != null) 122 { 123 th.printStackTrace(); 124 } 125 } 126 } 127 } 128 129 133 public static void setExecutionContext(HttpServlet servlet) 134 { 135 servlets.set(servlet); 136 } 137 138 141 public static void unsetExecutionContext() 142 { 143 servlets.set(null); 144 } 145 146 150 public static void setLevel(String logLevel) 151 { 152 if (logLevel.equalsIgnoreCase("FATAL")) 153 { 154 setLevel(LEVEL_FATAL); 155 } 156 else if (logLevel.equalsIgnoreCase("ERROR")) 157 { 158 setLevel(LEVEL_ERROR); 159 } 160 else if (logLevel.equalsIgnoreCase("WARN")) 161 { 162 setLevel(LEVEL_WARN); 163 } 164 else if (logLevel.equalsIgnoreCase("INFO")) 165 { 166 setLevel(LEVEL_INFO); 167 } 168 else if (logLevel.equalsIgnoreCase("DEBUG")) 169 { 170 setLevel(LEVEL_DEBUG); 171 } 172 else 173 { 174 throw new IllegalArgumentException ("Unknown log level: " + logLevel); 175 } 176 } 177 178 181 public boolean isDebugEnabled() 182 { 183 return level == LEVEL_DEBUG; 184 } 185 186 189 public static void setLevel(int level) 190 { 191 ServletLoggingOutput.level = level; 192 } 193 194 197 public static int getLevel() 198 { 199 return level; 200 } 201 202 205 private static final ThreadLocal servlets = new ThreadLocal (); 206 207 210 private static int level = LEVEL_WARN; 211 } 212 | Popular Tags |