1 19 package org.apache.avalon.excalibur.logger; 20 21 import org.apache.avalon.framework.logger.Logger; 22 import javax.servlet.ServletConfig ; 23 import javax.servlet.ServletContext ; 24 25 35 36 public class ServletLogger implements Logger 37 { 38 39 public static final int LEVEL_DEBUG = 0; 40 41 42 public static final int LEVEL_INFO = 1; 43 44 45 public static final int LEVEL_WARN = 2; 46 47 48 public static final int LEVEL_ERROR = 3; 49 50 51 public static final int LEVEL_FATAL = 4; 52 53 54 public static final int LEVEL_DISABLED = 5; 55 56 private final ServletContext m_servletContext; 57 private final int m_logLevel; 58 private final String m_prompt; 59 60 63 public ServletLogger( final ServletConfig servletConfig ) 64 { 65 this( servletConfig, LEVEL_DEBUG ); 66 } 67 68 69 private void checkState() 70 { 71 if ( m_servletContext == null ) 72 { 73 throw new NullPointerException ( "servletContext" ); 74 } 75 if ( m_logLevel < LEVEL_DEBUG || m_logLevel > LEVEL_DISABLED ) 76 { 77 throw new IllegalArgumentException ( "Bad logLevel: " + m_logLevel ); 78 } 79 } 80 81 87 public ServletLogger( final ServletContext servletContext, final String prompt, 88 final int logLevel ) 89 { 90 m_servletContext = servletContext; 91 m_logLevel = logLevel; 92 checkState(); 93 m_prompt = prompt; 94 } 95 96 102 public ServletLogger( final ServletConfig servletConfig, final int logLevel ) 103 { 104 m_servletContext = servletConfig.getServletContext(); 105 m_logLevel = logLevel; 106 checkState(); 107 108 final String servletName = servletConfig.getServletName(); 109 110 if ( servletName == null || "".equals( servletName ) ) 111 { 112 m_prompt = "unknown: "; 113 } 114 else 115 { 116 m_prompt = servletName + ": "; 117 } 118 } 119 120 125 public void debug( final String message ) 126 { 127 debug( message, null ); 128 } 129 130 136 public void debug( final String message, final Throwable throwable ) 137 { 138 if( m_logLevel <= LEVEL_DEBUG ) 139 { 140 m_servletContext.log( m_prompt + "[DEBUG] " + message, throwable ); 141 } 142 } 143 144 149 public boolean isDebugEnabled() 150 { 151 return m_logLevel <= LEVEL_DEBUG; 152 } 153 154 159 public void info( final String message ) 160 { 161 info( message, null ); 162 } 163 164 170 public void info( final String message, final Throwable throwable ) 171 { 172 if( m_logLevel <= LEVEL_INFO ) 173 { 174 m_servletContext.log( m_prompt + "[INFO] " + message, throwable ); 175 } 176 } 177 178 183 public boolean isInfoEnabled() 184 { 185 return m_logLevel <= LEVEL_INFO; 186 } 187 188 193 public void warn( final String message ) 194 { 195 warn( message, null ); 196 } 197 198 204 public void warn( final String message, final Throwable throwable ) 205 { 206 if( m_logLevel <= LEVEL_WARN ) 207 { 208 m_servletContext.log( m_prompt + "[WARNING] " + message, throwable ); 209 } 210 } 211 212 217 public boolean isWarnEnabled() 218 { 219 return m_logLevel <= LEVEL_WARN; 220 } 221 222 227 public void error( final String message ) 228 { 229 error( message, null ); 230 } 231 232 238 public void error( final String message, final Throwable throwable ) 239 { 240 if( m_logLevel <= LEVEL_ERROR ) 241 { 242 m_servletContext.log( m_prompt + "[ERROR] " + message, throwable ); 243 } 244 } 245 246 251 public boolean isErrorEnabled() 252 { 253 return m_logLevel <= LEVEL_ERROR; 254 } 255 256 261 public void fatalError( final String message ) 262 { 263 fatalError( message, null ); 264 } 265 266 272 public void fatalError( final String message, final Throwable throwable ) 273 { 274 if( m_logLevel <= LEVEL_FATAL ) 275 { 276 m_servletContext.log( m_prompt + "[FATAL ERROR] " + message, throwable ); 277 } 278 } 279 280 285 public boolean isFatalErrorEnabled() 286 { 287 return m_logLevel <= LEVEL_FATAL; 288 } 289 290 296 public Logger getChildLogger( final String name ) 297 { 298 return this; 299 } 300 } 301 | Popular Tags |