1 /* 2 ~ Copyright 2006-2007 Nicolas De Loof. 3 ~ 4 ~ Licensed under the Apache License, Version 2.0 (the "License"); 5 ~ you may not use this file except in compliance with the License. 6 ~ You may obtain a copy of the License at 7 ~ 8 ~ http://www.apache.org/licenses/LICENSE-2.0 9 ~ 10 ~ Unless required by applicable law or agreed to in writing, software 11 ~ distributed under the License is distributed on an "AS IS" BASIS, 12 ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 ~ See the License for the specific language governing permissions and 14 ~ limitations under the License. 15 */ 16 package org.jmonit.log; 17 18 import static org.jmonit.util.RuntimeAdaptable.isPresentInClasspath; 19 20 /** 21 * Facade API to any logging framework. 22 * <p> 23 * commons-logging provides such an abstraction layer, but introduces some 24 * classloader issues that many user complain. To avoid dependency on this 25 * librairy, this thin layer can wrap commons-logging if no other supported 26 * logging framework is found. 27 * 28 * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a> 29 */ 30 public abstract class Log 31 { 32 /** Indicates log4j present in classpath */ 33 private static boolean log4j = isPresentInClasspath( "org.apache.log4j.Logger" ); 34 35 /** Indicates jakarta commons-logging present in classpath */ 36 private static boolean commonsLogging = isPresentInClasspath( "org.apache.commons.logging.Log" ); 37 38 /** Indicates Java 1.4 or better runtime environment */ 39 private static boolean java14 = isPresentInClasspath( "java.lang.StackTraceElement" ); 40 41 public static Log getLog( Class clazz ) 42 { 43 if ( System.getProperty( "org.jmonit.log.Log.console" ) != null ) 44 { 45 return new ConsoleLogger( clazz ); 46 } 47 if ( log4j ) 48 { 49 return new Log4jLogger( clazz ); 50 } 51 if ( commonsLogging ) 52 { 53 return new CommonsLoggingLogger( clazz ); 54 } 55 if ( java14 ) 56 { 57 return new Java14Logger( clazz ); 58 } 59 return new NullLogger(); 60 } 61 62 /** 63 * @return true if debug logs are enabled 64 */ 65 public abstract boolean isDebugEnabled(); 66 67 /** 68 * @param message message to be logged 69 */ 70 public abstract void debug( String message ); 71 72 /** 73 * @param message message to be logged 74 */ 75 public abstract void info( String message ); 76 77 /** 78 * @param message message to be logged 79 * @param t Exception to be logged 80 */ 81 public abstract void info( String message, Throwable t ); 82 83 /** 84 * @param message message to be logged 85 */ 86 public abstract void warn( String message ); 87 88 /** 89 * @param message message to be logged 90 * @param t Exception to be logged 91 */ 92 public abstract void warn( String message, Throwable t ); 93 94 /** 95 * @param message message to be logged 96 */ 97 public abstract void error( String message ); 98 99 /** 100 * @param message message to be logged 101 * @param t Exception to be logged 102 */ 103 public abstract void error( String message, Throwable t ); 104 105 }