1 18 19 package org.apache.beehive.netui.tools.testrecorder.shared; 20 21 import java.io.PrintWriter ; 22 import java.io.StringWriter ; 23 24 import org.apache.log4j.*; 25 import org.apache.commons.logging.Log; 26 27 public class Logger implements Log { 28 29 private static final String STRUTS_APPENDER = "commons-logging"; 30 31 static { 32 Category root = Category.getRoot(); 35 36 if ( root.getAppender( STRUTS_APPENDER ) != null ) { 37 root.removeAppender( STRUTS_APPENDER ); 38 } 39 } 40 41 42 public static Logger getInstance( Class clazz ) { 43 return new Logger( clazz ); 44 } 45 46 private org.apache.log4j.Logger logInstance; 47 48 public Logger( Class clazz ) { 49 logInstance = org.apache.log4j.Logger.getLogger( clazz.getName() ); 50 } 51 52 public boolean isDebugEnabled() { 53 return logInstance.isEnabledFor( Level.DEBUG ); 54 } 55 56 public boolean isErrorEnabled() { 57 return logInstance.isEnabledFor( Level.ERROR ); 58 } 59 60 public boolean isFatalEnabled() { 61 return logInstance.isEnabledFor( Level.FATAL ); 62 } 63 64 public boolean isInfoEnabled() { 65 return logInstance.isEnabledFor( Level.INFO ); 66 } 67 68 public boolean isTraceEnabled() { 69 return logInstance.isEnabledFor( Level.DEBUG ); 70 } 71 72 public boolean isWarnEnabled() { 73 return logInstance.isEnabledFor( Level.WARN ); 74 } 75 76 public void debug( Object message ) { 77 if ( logInstance.isEnabledFor( Level.DEBUG ) ) { 78 logInstance.debug( message ); 79 } 80 } 81 82 public void debug( Object message, Throwable t ) { 83 if ( logInstance.isEnabledFor( Level.DEBUG ) ) { 84 logInstance.debug( format( message, t ) ); 85 } 86 } 87 88 public void trace( Object message ) { 89 if ( logInstance.isEnabledFor( Level.DEBUG ) ) { 90 logInstance.debug( message ); 91 } 92 } 93 94 public void trace( Object message, Throwable t ) { 95 if ( logInstance.isEnabledFor( Level.DEBUG ) ) { 96 logInstance.debug( format( message, t ) ); 97 } 98 } 99 100 public void info( Object message ) { 101 if ( logInstance.isEnabledFor( Level.INFO ) ) { 102 logInstance.info( message ); 103 } 104 } 105 106 public void info( Object message, Throwable t ) { 107 if ( logInstance.isEnabledFor( Level.INFO ) ) { 108 logInstance.info( format( message, t ) ); 109 } 110 } 111 112 public void warn( Object message ) { 113 if ( logInstance.isEnabledFor( Level.WARN ) ) { 114 logInstance.warn( message ); 115 } 116 } 117 118 public void warn( Object message, Throwable t ) { 119 if ( logInstance.isEnabledFor( Level.WARN ) ) { 120 logInstance.warn( format( message, t ) ); 121 } 122 } 123 124 public void error( Object message ) { 125 if ( logInstance.isEnabledFor( Level.ERROR ) ) { 126 logInstance.error( message ); 127 } 128 } 129 130 public void error( Object message, Throwable t ) { 131 if ( logInstance.isEnabledFor( Level.ERROR ) ) { 132 logInstance.error( format( message, t ) ); 133 } 134 } 135 136 public void fatal( Object message ) { 137 if ( logInstance.isEnabledFor( Level.FATAL ) ) { 138 logInstance.fatal( message ); 139 } 140 } 141 142 public void fatal( Object message, Throwable t ) { 143 if ( logInstance.isEnabledFor( Level.FATAL ) ) { 144 logInstance.fatal( format( message, t ) ); 145 } 146 } 147 148 public static String format( final Object obj, final Throwable t ) { 149 if ( t == null ) { 150 return obj.toString(); 151 } 152 Throwable ex = t; 153 StringBuffer sb = new StringBuffer ( 512 ); 154 sb.append( "object( " + obj + " )\n" ); 155 while ( ex != null ) { 156 sb.append( format( ex ) + "\n"); 157 ex = ex.getCause(); 158 } 159 return sb.toString(); 160 } 161 162 public static String format( Throwable t ) { 163 if ( t == null ) { 164 return "NULL"; 165 } 166 StringWriter sw = new StringWriter (); 167 t.printStackTrace( new PrintWriter ( sw ) ); 168 return "throwable: msg( " + t.getMessage() + " )\ntoString( " + t.toString() + 169 " )\nstack trace:\n" + sw.toString(); 170 } 171 172 } 173 | Popular Tags |