001/* 002 GRANITE DATA SERVICES 003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S. 004 005 This file is part of Granite Data Services. 006 007 Granite Data Services is free software; you can redistribute it and/or modify 008 it under the terms of the GNU Library General Public License as published by 009 the Free Software Foundation; either version 2 of the License, or (at your 010 option) any later version. 011 012 Granite Data Services is distributed in the hope that it will be useful, but 013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License 015 for more details. 016 017 You should have received a copy of the GNU Library General Public License 018 along with this library; if not, see <http://www.gnu.org/licenses/>. 019*/ 020 021package org.granite.logging; 022 023import java.lang.reflect.Constructor; 024 025import org.granite.util.TypeUtil; 026 027/** 028 * @author Franck WOLFF 029 */ 030public abstract class Logger { 031 032 /////////////////////////////////////////////////////////////////////////// 033 // Fields. 034 035 public static final String LOGGER_IMPL_SYSTEM_PROPERTY = "org.granite.logger.impl"; 036 037 private static final boolean log4jAvailable; 038 static { 039 boolean available = false; 040 try { 041 TypeUtil.forName("org.apache.log4j.Logger"); 042 available = true; 043 } catch (Exception e) { 044 } 045 log4jAvailable = available; 046 } 047 048 private final Object loggerImpl; 049 private final LoggingFormatter formatter; 050 051 /////////////////////////////////////////////////////////////////////////// 052 // Constructor. 053 054 protected Logger(Object loggerImpl, LoggingFormatter formatter) { 055 this.loggerImpl = loggerImpl; 056 this.formatter = formatter; 057 } 058 059 /////////////////////////////////////////////////////////////////////////// 060 // Getters. 061 062 protected Object getLoggerImpl() { 063 return loggerImpl; 064 } 065 066 protected LoggingFormatter getFormatter() { 067 return formatter; 068 } 069 070 /////////////////////////////////////////////////////////////////////////// 071 // Static initializers. 072 073 public static Logger getLogger() { 074 return getLogger(new DefaultLoggingFormatter()); 075 } 076 077 public static Logger getLogger(Class<?> clazz) { 078 return getLogger(clazz.getName(), new DefaultLoggingFormatter()); 079 } 080 081 public static Logger getLogger(String name) { 082 return getLogger(name, new DefaultLoggingFormatter()); 083 } 084 085 public static Logger getLogger(LoggingFormatter formatter) { 086 Throwable t = new Throwable(); 087 StackTraceElement[] stes = t.getStackTrace(); 088 if (stes.length < 2) 089 throw new RuntimeException("Illegal instantiation context (stacktrace elements should be of length >= 2)", t); 090 return getLogger(stes[1].getClassName()); 091 } 092 093 public static Logger getLogger(Class<?> clazz, LoggingFormatter formatter) { 094 return getLogger(clazz.getName(), formatter); 095 } 096 097 public static Logger getLogger(String name, LoggingFormatter formatter) { 098 String loggerImplClass = System.getProperty(LOGGER_IMPL_SYSTEM_PROPERTY); 099 if (loggerImplClass != null) { 100 try { 101 Class<? extends Logger> clazz = TypeUtil.forName(loggerImplClass, Logger.class); 102 Constructor<? extends Logger> constructor = clazz.getConstructor(String.class, LoggingFormatter.class); 103 return constructor.newInstance(name, formatter); 104 } catch (Exception e) { 105 throw new RuntimeException( 106 "Could not create instance of: " + loggerImplClass + 107 " (" + LOGGER_IMPL_SYSTEM_PROPERTY + " system property)", e); 108 } 109 } 110 return log4jAvailable ? new Log4jLogger(name, formatter) : new JdkLogger(name, formatter); 111 } 112 113 /////////////////////////////////////////////////////////////////////////// 114 // Logging methods. 115 116 public abstract void info(String message, Object... args); 117 public abstract void info(Throwable t, String message, Object... args); 118 119 public abstract void trace(String message, Object... args); 120 public abstract void trace(Throwable t, String message, Object... args); 121 122 public abstract void warn(String message, Object... args); 123 public abstract void warn(Throwable t, String message, Object... args); 124 125 public abstract void debug(String message, Object... args); 126 public abstract void debug(Throwable t, String message, Object... args); 127 128 public abstract void error(String message, Object... args); 129 public abstract void error(Throwable t, String message, Object... args); 130 131 public abstract void fatal(String message, Object... args); 132 public abstract void fatal(Throwable t, String message, Object... args); 133 134 /////////////////////////////////////////////////////////////////////////// 135 // Configuration. 136 137 public abstract void setLevel(Level level); 138 139 public abstract boolean isDebugEnabled(); 140 141 public abstract boolean isErrorEnabled(); 142 143 public abstract boolean isFatalEnabled(); 144 145 public abstract boolean isInfoEnabled(); 146 147 public abstract boolean isTraceEnabled(); 148 149 public abstract boolean isWarnEnabled(); 150}