熟悉的一行代码
private static final Log logger = LogFactory.getLog(Application.class);
这一行代码就是使用了LogFactory日志框架,对类Application,进行日志输出。
private static final 这里表示虚拟机启动后就创建一个最终的日志对象Log logger
创建对象的方式
public static Log getLog(Class clazz) throws LogConfigurationException {
return getFactory().getInstance(clazz);
}
public static LogFactory getFactory() throws LogConfigurationException {
// Identify the class loader we will be using
ClassLoader contextClassLoader = getContextClassLoaderInternal();
if (contextClassLoader == null) {
// This is an odd enough situation to report about. This
// output will be a nuisance on JDK1.1, as the system
// classloader is null in that environment.
if (isDiagnosticsEnabled()) {
logDiagnostic("Context classloader is null.");
}
}
// Return any previously registered factory for this class loader
LogFactory factory = getCachedFactory(contextClassLoader);
if (factory != null) {
return factory;
}
if (isDiagnosticsEnabled()) {
logDiagnostic(
"[LOOKUP] LogFactory implementation requested for the first time for context classloader " +
objectId(contextClassLoader));
logHierarchy("[LOOKUP] ", contextClassLoader);
}
这种创建对象的方式也可以运用到其他相似的场景
这里看上去创建的是单例对象,以减少日志调用对内存的使用,使日志调用只需生成一个对象。
Log接口和传参:
public interface Log {
/**
* Logs a message with debug log level.
*
* @param message log this message
*/
void debug(Object message);
/**
* Logs an error with debug log level.
*
* @param message log this message
* @param t log this cause
*/
void debug(Object message, Throwable t);
/**
* Logs a message with error log level.
*
* @param message log this message
*/
void error(Object message);
/**
* Logs an error with error log level.
*
* @param message log this message
* @param t log this cause
*/
void error(Object message, Throwable t);
/**
* Logs a message with fatal log level.
*
* @param message log this message
*/
void fatal(Object message);
/**
* Logs an error with fatal log level.
*
* @param message log this message
* @param t log this cause
*/
void fatal(Object message, Throwable t);
/**
* Logs a message with info log level.
*
* @param message log this message
*/
void info(Object message);
/**
* Logs an error with info log level.
*
* @param message log this message
* @param t log this cause
*/
void info(Object message, Throwable t);
/**
* Is debug logging currently enabled?
* <p>
* Call this method to prevent having to perform expensive operations
* (for example, <code>String</code> concatenation)
* when the log level is more than debug.
*
* @return true if debug is enabled in the underlying logger.
*/
boolean isDebugEnabled();
/**
* Is error logging currently enabled?
* <p>
* Call this method to prevent having to perform expensive operations
* (for example, <code>String</code> concatenation)
* when the log level is more than error.
*
* @return true if error is enabled in the underlying logger.
*/
boolean isErrorEnabled();
/**
* Is fatal logging currently enabled?
* <p>
* Call this method to prevent having to perform expensive operations
* (for example, <code>String</code> concatenation)
* when the log level is more than fatal.
*
* @return true if fatal is enabled in the underlying logger.
*/
boolean isFatalEnabled();
/**
* Is info logging currently enabled?
* <p>
* Call this method to prevent having to perform expensive operations
* (for example, <code>String</code> concatenation)
* when the log level is more than info.
*
* @return true if info is enabled in the underlying logger.
*/
boolean isInfoEnabled();
/**
* Is trace logging currently enabled?
* <p>
* Call this method to prevent having to perform expensive operations
* (for example, <code>String</code> concatenation)
* when the log level is more than trace.
*
* @return true if trace is enabled in the underlying logger.
*/
boolean isTraceEnabled();
/**
* Is warn logging currently enabled?
* <p>
* Call this method to prevent having to perform expensive operations
* (for example, <code>String</code> concatenation)
* when the log level is more than warn.
*
* @return true if warn is enabled in the underlying logger.
*/
boolean isWarnEnabled();
/**
* Logs a message with trace log level.
*
* @param message log this message
*/
void trace(Object message);
/**
* Logs an error with trace log level.
*
* @param message log this message
* @param t log this cause
*/
void trace(Object message, Throwable t);
/**
* Logs a message with warn log level.
*
* @param message log this message
*/
void warn(Object message);
/**
* Logs an error with warn log level.
*
* @param message log this message
* @param t log this cause
*/
void warn(Object message, Throwable t);
}
public interface Log {
/**
* Logs a message with debug log level.
*
* @param message log this message
*/
void debug(Object message);
/**
* Logs an error with debug log level.
*
* @param message log this message
* @param t log this cause
*/
void debug(Object message, Throwable t);
/**
* Logs a message with error log level.
*
* @param message log this message
*/
void error(Object message);
/**
* Logs an error with error log level.
*
* @param message log this message
* @param t log this cause
*/
void error(Object message, Throwable t);
/**
* Logs a message with fatal log level.
*
* @param message log this message
*/
void fatal(Object message);
/**
* Logs an error with fatal log level.
*
* @param message log this message
* @param t log this cause
*/
void fatal(Object message, Throwable t);
/**
* Logs a message with info log level.
*
* @param message log this message
*/
void info(Object message);
/**
* Logs an error with info log level.
*
* @param message log this message
* @param t log this cause
*/
void info(Object message, Throwable t);
/**
* Is debug logging currently enabled?
* <p>
* Call this method to prevent having to perform expensive operations
* (for example, <code>String</code> concatenation)
* when the log level is more than debug.
*
* @return true if debug is enabled in the underlying logger.
*/
boolean isDebugEnabled();
/**
* Is error logging currently enabled?
* <p>
* Call this method to prevent having to perform expensive operations
* (for example, <code>String</code> concatenation)
* when the log level is more than error.
*
* @return true if error is enabled in the underlying logger.
*/
boolean isErrorEnabled();
/**
* Is fatal logging currently enabled?
* <p>
* Call this method to prevent having to perform expensive operations
* (for example, <code>String</code> concatenation)
* when the log level is more than fatal.
*
* @return true if fatal is enabled in the underlying logger.
*/
boolean isFatalEnabled();
/**
* Is info logging currently enabled?
* <p>
* Call this method to prevent having to perform expensive operations
* (for example, <code>String</code> concatenation)
* when the log level is more than info.
*
* @return true if info is enabled in the underlying logger.
*/
boolean isInfoEnabled();
/**
* Is trace logging currently enabled?
* <p>
* Call this method to prevent having to perform expensive operations
* (for example, <code>String</code> concatenation)
* when the log level is more than trace.
*
* @return true if trace is enabled in the underlying logger.
*/
boolean isTraceEnabled();
/**
* Is warn logging currently enabled?
* <p>
* Call this method to prevent having to perform expensive operations
* (for example, <code>String</code> concatenation)
* when the log level is more than warn.
*
* @return true if warn is enabled in the underlying logger.
*/
boolean isWarnEnabled();
/**
* Logs a message with trace log level.
*
* @param message log this message
*/
void trace(Object message);
/**
* Logs an error with trace log level.
*
* @param message log this message
* @param t log this cause
*/
void trace(Object message, Throwable t);
/**
* Logs a message with warn log level.
*
* @param message log this message
*/
void warn(Object message);
/**
* Logs an error with warn log level.
*
* @param message log this message
* @param t log this cause
*/
void warn(Object message, Throwable t);
}
调用示例:
logger.error("bean init error " + clazz == null ? "null" : clazz.getName(), ex);
输出日志的级别分别有:
warn
trace
info
fatal
error
debug
等等