下面列出了怎么用org.slf4j.spi.LocationAwareLogger的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Logger getLogger(String name) {
final Logger logger = loggerFactory.getLogger(name);
if (logger instanceof LocationAwareLogger) {
return new LevelledLocationAwareLoggerWrapper(
(LocationAwareLogger) logger) {
@Override
protected int getLevel() {
return LevelledLoggerFactoryWrapper.this.getLevel();
}
};
} else {
return new LevelledLoggerWrapper(logger) {
@Override
protected int getLevel() {
return LevelledLoggerFactoryWrapper.this.getLevel();
}
};
}
}
public Slf4jImpl(Class<?> clazz) {
Logger logger = LoggerFactory.getLogger(clazz);
if (logger instanceof LocationAwareLogger) {
try {
// check for slf4j >= 1.6 method signature
logger.getClass().getMethod("log", Marker.class, String.class, int.class,
String.class, Object[].class, Throwable.class);
log = new Slf4jLocationAwareLoggerImpl((LocationAwareLogger) logger);
return;
} catch (SecurityException | NoSuchMethodException e) {
// fail-back to Slf4jLoggerImpl
}
}
// Logger is not LocationAwareLogger or slf4j version < 1.6
log = new Slf4jLoggerImpl(logger);
}
@Override
public void log(Marker marker, String callerFQCN, int level, String msg, Object[] argArray, Throwable t) {
Level log4jLevel;
switch (level) {
case LocationAwareLogger.TRACE_INT:
log4jLevel = traceCapable ? Level.TRACE : Level.DEBUG;
break;
case LocationAwareLogger.DEBUG_INT:
log4jLevel = Level.DEBUG;
break;
case LocationAwareLogger.INFO_INT:
log4jLevel = Level.INFO;
break;
case LocationAwareLogger.WARN_INT:
log4jLevel = Level.WARN;
break;
case LocationAwareLogger.ERROR_INT:
log4jLevel = Level.ERROR;
break;
default:
throw new IllegalStateException("Level number " + level + " is not recognized.");
}
logger.log(callerFQCN, log4jLevel, msg, t);
}
private int convertLevel(final Level level) {
switch (level.getStandardLevel()) {
case DEBUG :
return LocationAwareLogger.DEBUG_INT;
case TRACE :
return LocationAwareLogger.TRACE_INT;
case INFO :
return LocationAwareLogger.INFO_INT;
case WARN :
return LocationAwareLogger.WARN_INT;
case ERROR :
return LocationAwareLogger.ERROR_INT;
default :
return LocationAwareLogger.ERROR_INT;
}
}
public Slf4jLogger(org.slf4j.Logger logger) {
if (logger instanceof LocationAwareLogger) {
locationAwareLogger = (LocationAwareLogger) logger;
} else {
locationAwareLogger = null;
}
this.logger = logger;
}
@Override
public void info(String format, Object... args) {
if (isInfoEnabled()) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, args);
log.log(null, callerFQCN, LocationAwareLogger.INFO_INT, ft.getMessage(), NULL_ARGS, ft.getThrowable());
}
}
@Override
public void debug(Throwable e) {
if (locationAwareLogger != null) {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, e.getMessage(), null, e);
return;
}
logger.debug(e.getMessage(), e);
}
@Override
public void info(String msg) {
if (locationAwareLogger != null) {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.INFO_INT, msg, null, null);
return;
}
logger.info(msg);
}
@Override
public void warn(String msg, Throwable e) {
if (locationAwareLogger != null) {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.WARN_INT, msg, null, e);
return;
}
logger.warn(msg, e);
}
@Override
public void error(Throwable e) {
if (locationAwareLogger != null) {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.ERROR_INT, e.getMessage(), null, e);
return;
}
logger.error(e.getMessage(), e);
}
@Override
protected void internalDebug(String msg) {
if (lALogger != null) {
lALogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, null);
} else {
logger.debug(msg);
}
}
@Override
public void log(Marker m, String aFqcn, Level level, String msg_with_params, Throwable thrown) {
m = (m == null)
? getMarkerForLevel(level)
: m;
if (isLocationCapable) { // slf4j simple logger is not
((org.slf4j.spi.LocationAwareLogger)logger).log(m, aFqcn, getSlf4jLevel(level), msg_with_params, null, thrown);
} else {
switch(level.toInteger()) {
case Level.SEVERE_INT:
// all of these calls to MessageFormat are to the java.text.MessageFormat
// to do {n} style format substitution
logger.error(m, msg_with_params, thrown);
break;
case Level.WARNING_INT:
logger.warn(m, msg_with_params, thrown);
break;
case Level.INFO_INT:
logger.info(m, msg_with_params, thrown);
break;
case Level.CONFIG_INT:
logger.info(m, msg_with_params, thrown);
break;
case Level.FINE_INT:
logger.debug(m, msg_with_params, thrown);
break;
case Level.FINER_INT:
logger.trace(m, msg_with_params, thrown);
break;
case Level.FINEST_INT:
logger.trace(m, msg_with_params, thrown);
break;
default: Misc.internalError();
}
}
}
public void error(String msg, Throwable t)
{
msg = SecretDetector.maskSecrets(msg);
if (isLocationAwareLogger)
{
((LocationAwareLogger) slf4jLogger).log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, null, t);
}
else
{
slf4jLogger.error(msg, t);
}
}
public Slf4jLogger(final String name, final String resourceBundleName) {
super(name, resourceBundleName);
logger = org.slf4j.LoggerFactory.getLogger(name);
if (logger instanceof LocationAwareLogger) {
locationAwareLogger = (LocationAwareLogger) logger;
}
}
public static int getSlf4jLevel(Level level) {
switch(level.toInteger()) {
case Level.SEVERE_INT: return LocationAwareLogger.ERROR_INT;
case Level.WARNING_INT: return LocationAwareLogger.WARN_INT;
case Level.INFO_INT: return LocationAwareLogger.INFO_INT;
case Level.CONFIG_INT: return LocationAwareLogger.INFO_INT;
case Level.FINE_INT: return LocationAwareLogger.DEBUG_INT;
case Level.FINER_INT: return LocationAwareLogger.TRACE_INT;
case Level.FINEST_INT: return LocationAwareLogger.TRACE_INT;
}
Misc.internalError();
return LocationAwareLogger.ERROR_INT; //ignored, just here for compile error avoidance
}
@Override
public void debug(String format, Object... args) {
if (isDebugEnabled()) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, args);
log.log(null, callerFQCN, LocationAwareLogger.DEBUG_INT, ft.getMessage(), NULL_ARGS, ft.getThrowable());
}
}
@Override
protected void internalWarn(String msg, Throwable cause) {
if (lALogger != null) {
lALogger.log(null, FQCN, LocationAwareLogger.WARN_INT, msg, null, cause);
} else {
logger.warn(msg);
}
}
@Override
public void warn(String format, Object... args) {
if (isWarnEnabled()) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, args);
log.log(null, callerFQCN, LocationAwareLogger.WARN_INT, ft.getMessage(), NULL_ARGS, ft.getThrowable());
}
}
public void debug(String msg)
{
msg = SecretDetector.maskSecrets(msg);
if (isLocationAwareLogger)
{
((LocationAwareLogger) slf4jLogger).log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, null);
}
else
{
slf4jLogger.debug(msg);
}
}
@Override
public void error(String format, Object... args) {
if (isErrorEnabled()) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, args);
JbootExceptionHolder.hold(ft.getMessage(), ft.getThrowable());
log.log(null, callerFQCN, LocationAwareLogger.ERROR_INT, ft.getMessage(), NULL_ARGS, ft.getThrowable());
}
}
@Override
public Log getLog(String name) {
if (useJdkLogger) {
return new JdkLogger(name);
}
Logger log = LoggerFactory.getLogger(name);
return log instanceof LocationAwareLogger ? new Slf4jLogger((LocationAwareLogger) log) : new Slf4jSimpleLogger(log);
}
@Override
public void trace(String s) {
logger.log(MARKER, FQCN, LocationAwareLogger.TRACE_INT, s, null, null);
}
public SLF4JImpl(String loggerName){
this.log = (LocationAwareLogger) LoggerFactory.getLogger(loggerName);
}
public void warn(Marker marker, String format, Object arg1, Object arg2) {
if (getLevel() <= LocationAwareLogger.WARN_INT) {
delegate.warn(marker, format, arg1, arg2);
}
}
public SLF4JLogger(final String name, final org.slf4j.Logger logger) {
super(name);
this.logger = logger;
this.locationAwareLogger = logger instanceof LocationAwareLogger ? (LocationAwareLogger) logger : null;
}
public void error(Marker marker, String msg) {
if (getLevel() <= LocationAwareLogger.ERROR_INT) {
delegate.error(marker, msg);
}
}
LevelledLocationAwareLoggerWrapper(LocationAwareLogger logger) {
super(logger);
this.locationAwareDelegate = logger;
}
@Override
public void error(final String message) {
logger.log(null, FQNC, LocationAwareLogger.ERROR_INT, message, null, null);
incrementError();
}
@Override
protected void internalLogFormatted(final String msg, final LogRecord record) {
final Level level = record.getLevel();
final Throwable t = record.getThrown();
final Handler[] targets = getHandlers();
if (targets != null) {
for (final Handler h : targets) {
h.publish(record);
}
}
if (!getUseParentHandlers()) {
return;
}
/*
* As we can not use a "switch ... case" block but only a "if ... else if ..." block, the order of the
* comparisons is important. We first try log level FINE then INFO, WARN, FINER, etc
*/
if (Level.FINE.equals(level)) {
if (locationAwareLogger == null) {
logger.debug(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
}
} else if (Level.INFO.equals(level)) {
if (locationAwareLogger == null) {
logger.info(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.INFO_INT, msg, null, t);
}
} else if (Level.WARNING.equals(level)) {
if (locationAwareLogger == null) {
logger.warn(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.WARN_INT, msg, null, t);
}
} else if (Level.FINER.equals(level)) {
if (locationAwareLogger == null) {
logger.trace(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
}
} else if (Level.FINEST.equals(level)) {
if (locationAwareLogger == null) {
logger.trace(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.TRACE_INT, msg, null, t);
}
} else if (Level.ALL.equals(level)) {
// should never occur, all is used to configure java.util.logging
// but not accessible by the API Logger.xxx() API
if (locationAwareLogger == null) {
logger.error(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, null, t);
}
} else if (Level.SEVERE.equals(level)) {
if (locationAwareLogger == null) {
logger.error(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, null, t);
}
} else if (Level.CONFIG.equals(level)) {
if (locationAwareLogger == null) {
logger.debug(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
}
}
// don't log if Level.OFF
}
@Override
public void trace(final String message, final Object... args) {
logger.log(null, FQNC, LocationAwareLogger.TRACE_INT, message, args, null);
incrementWarn();
}