下面列出了org.slf4j.Logger#isDebugEnabled ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Advice that logs when a method is entered and exited.
*
* @param joinPoint join point for advice.
* @return result.
* @throws Throwable throws {@link IllegalArgumentException}.
*/
@Around("applicationPackagePointcut() && springBeanPointcut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
Logger log = logger(joinPoint);
if (log.isDebugEnabled()) {
log.debug("Enter: {}() with argument[s] = {}", joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()));
}
try {
Object result = joinPoint.proceed();
if (log.isDebugEnabled()) {
log.debug("Exit: {}() with result = {}", joinPoint.getSignature().getName(), result);
}
return result;
} catch (IllegalArgumentException e) {
log.error("Illegal argument: {} in {}()", Arrays.toString(joinPoint.getArgs()), joinPoint.getSignature().getName());
throw e;
}
}
/**
* Check whether a SLF4J logger is enabled for a certain loglevel.
* If the "logger" or the "level" is null, false is returned.
*/
public static boolean isEnabledFor(Logger logger, Level level) {
boolean res = false;
if (logger != null && level != null) {
switch (level) {
case TRACE:
res = logger.isTraceEnabled();
break;
case DEBUG:
res = logger.isDebugEnabled();
break;
case INFO:
res = logger.isInfoEnabled();
break;
case WARN:
res = logger.isWarnEnabled();
break;
case ERROR:
res = logger.isErrorEnabled();
break;
}
}
return res;
}
@VisibleForTesting
static void debug(Logger logger, String sql, @Nullable Object... args) {
if (!logger.isDebugEnabled()) {
return;
}
if (args.length == 0) {
logger.debug(sql);
return;
}
List<String> argStrings = Lists.newArrayList();
for (Object arg : args) {
if (arg instanceof String) {
argStrings.add('\'' + (String) arg + '\'');
} else if (arg == null) {
argStrings.add("NULL");
} else {
argStrings.add(checkNotNull(arg.toString()));
}
}
if (logger.isDebugEnabled()) {
logger.debug("{} [{}]", sql, Joiner.on(", ").join(argStrings));
}
}
private boolean isLogEnabled(Logger logger, LogLevel level) {
switch (level) {
case TRACE: return logger.isTraceEnabled();
case DEBUG: return logger.isDebugEnabled();
case INFO: return logger.isInfoEnabled();
default: throw new IllegalStateException("Unexpected log level: "+level);
}
}
/**
* Logs the entry to and exit from any method under the configured package.
*
* @param joinPoint
* @return
*/
public Object logAround(final ProceedingJoinPoint joinPoint) throws Throwable { // NOSONAR
// No sonar comment is to skip sonar Throwable violation, which can not
// be avoided
Object ret = joinPoint.proceed();
final Logger targetLogger = LoggingAspect.getLogger(joinPoint.getSourceLocation().getWithinType());
if (targetLogger.isDebugEnabled()) {
targetLogger.debug(LoggingAspect.METHOD_ARGS_RETURN, new String[] { joinPoint.getSignature().getName(),
Arrays.toString(joinPoint.getArgs()), ret != null ? ret.toString() : null });
}
return ret;
}
public static void debug(Class clzz, LogCallable callable) {
Logger logger = loggerMap.get(clzz);
if (logger == null )
return;
if (logger.isDebugEnabled()) {
if (callable == null)
return;
String str = callable.call();
if (StringUtil.isNullOrEmpty(str))
return;
logger.debug(str);
}
}
private static <T> void dolog(final LogLevel level, final Logger log, final String format, Object... value) {
switch (level) {
case TRACE:
if (log.isTraceEnabled())
log.trace(format, value);
break;
case DEBUG:
if (log.isDebugEnabled())
log.debug(format, value);
break;
case INFO:
if (log.isInfoEnabled())
log.info(format, value);
break;
case WARN:
if (log.isWarnEnabled())
log.warn(format, value);
break;
case ERROR:
if (log.isErrorEnabled())
log.error(format, value);
break;
default:
break;
}
}
@Override
public void logBodyString(String bodyString, Logger log) {
if (log.isDebugEnabled()) {
if (bodyString!=null) {
BufferedReader reader = new BufferedReader(new StringReader(bodyString));
reader.lines().forEach(line->log.debug(" "+line));
} else {
log.debug(" ---body-is-null---");
}
}
}
public static void aggregateProcessData(ProcessDataSet data, Logger logger) {
long start = System.nanoTime();
Map<String, List<Process>> processNameToProcesses = DataHelper.getProcessesByName(data, false);
for (List<Process> processes : processNameToProcesses.values()) {
if (processes.size() > 1) {
aggregateProcesses(data, processes, logger);
}
}
if (logger.isDebugEnabled()) {
logger.debug("Aggregated process data for {} in {}ms ", data, (System.nanoTime() - start) / 1000000.0d);
}
}
public static Consumer<Object> ifDebugEnabled(
Logger log, Supplier<String> message) {
return o -> {
if (log.isDebugEnabled()) {
log.debug(message.get(), o);
}
};
}
public void record(final Logger logger, final String msg, final Object... params) {
if (logger.isDebugEnabled()) {
final StringBuilder buf = new StringBuilder();
toString(buf, msg, params);
final String entry = buf.toString();
log(entry);
logger.debug(entry);
} else {
log(msg, params);
}
}
/**
* Format an error message as expected by ANTLR. It is basically the
* same error message that ANTL BaseRecognizer generates with some
* additional data.
* Also used to log debugging information.
* @param log the logger to use at debug time
* @param recognizer the lexer or parser who generated the error
* @param e the exception that occured
* @param superMessage the error message that the super class generated
* @param tokenNames list of token names
* @return a formatted error message
*/
public static String getErrorMessage(
final Logger log,
final BaseRecognizer recognizer,
final RecognitionException e,
final String superMessage,
final String[] tokenNames) {
if (log.isDebugEnabled()) {
List < ? > stack = BaseRecognizer.getRuleInvocationStack(
e, recognizer.getClass().getSuperclass().getName());
String debugMsg = recognizer.getErrorHeader(e)
+ " " + e.getClass().getSimpleName()
+ ": " + superMessage
+ ":";
if (e instanceof NoViableAltException) {
NoViableAltException nvae = (NoViableAltException) e;
debugMsg += " (decision=" + nvae.decisionNumber
+ " state=" + nvae.stateNumber + ")"
+ " decision=<<" + nvae.grammarDecisionDescription + ">>";
} else if (e instanceof UnwantedTokenException) {
UnwantedTokenException ute = (UnwantedTokenException) e;
debugMsg += " (unexpected token=" + toString(ute.getUnexpectedToken(), tokenNames) + ")";
} else if (e instanceof EarlyExitException) {
EarlyExitException eea = (EarlyExitException) e;
debugMsg += " (decision=" + eea.decisionNumber + ")";
}
debugMsg += " ruleStack=" + stack.toString();
log.debug(debugMsg);
}
return makeUserMsg(e, superMessage);
}
public static void createdInputStream(Logger logger, String channelId, String path) {
if (logger != null && logger.isDebugEnabled()) {
logger.debug(String.format(getMessage("log.createdInputStream"), channelId, path)); //$NON-NLS-1$
}
}
public static void debug(String message) {
Logger log = getLogger(getClassName());
if (log.isDebugEnabled()) {
log.debug(message);
}
}
public static void failedToCreatePool(Logger logger, IOException e) {
if (logger != null && logger.isDebugEnabled()) {
logger.debug(getMessage("log.failedToCreatePool"), e); //$NON-NLS-1$
}
}
private static boolean isLevelEnabled(Level logLevel, Logger logger) {
return (logLevel == Level.INFO && logger.isInfoEnabled())
|| (logLevel == Level.WARN && logger.isWarnEnabled())
|| (logLevel == Level.DEBUG && logger.isDebugEnabled());
}
/**
* Grants a lock on the identified resource to the calling thread (blocking
* until it is available).
*
* @return true if the lock was obtained.
*/
public synchronized boolean obtainLock(Connection conn, String lockName) {
lockName = lockName.intern();
Logger log = getLog();
if(log.isDebugEnabled()) {
log.debug(
"Lock '" + lockName + "' is desired by: "
+ Thread.currentThread().getName());
}
if (!isLockOwner(conn, lockName)) {
if(log.isDebugEnabled()) {
log.debug(
"Lock '" + lockName + "' is being obtained: "
+ Thread.currentThread().getName());
}
while (locks.contains(lockName)) {
try {
this.wait();
} catch (InterruptedException ie) {
if(log.isDebugEnabled()) {
log.debug(
"Lock '" + lockName + "' was not obtained by: "
+ Thread.currentThread().getName());
}
}
}
if(log.isDebugEnabled()) {
log.debug(
"Lock '" + lockName + "' given to: "
+ Thread.currentThread().getName());
}
getThreadLocks().add(lockName);
locks.add(lockName);
} else if(log.isDebugEnabled()) {
log.debug(
"Lock '" + lockName + "' already owned by: "
+ Thread.currentThread().getName()
+ " -- but not owner!",
new Exception("stack-trace of wrongful returner"));
}
return true;
}
public static void closedOutputStream(Logger logger, String channelId, String path) {
if (logger != null && logger.isDebugEnabled()) {
logger.debug(String.format(getMessage("log.closedOutputStream"), channelId, path)); //$NON-NLS-1$
}
}
public static void log(final SqlHandlerConfig config, final String name, final PhysicalPlan plan, final Logger logger) throws JsonProcessingException {
if (logger.isDebugEnabled()) {
String planText = plan.unparse(config.getContext().getLpPersistence().getMapper().writer());
logger.debug(name + " : \n" + planText);
}
}
/**
* This is a just a helper method to determine whether request scope
* creation/deletion issues should be logged or not.
*
* @param aLogger
* The logger to check.
* @return <code>true</code> if request scope creation/deletion should be
* logged, <code>false</code> otherwise.
*/
public static boolean debugRequestScopeLifeCycle (@Nonnull final Logger aLogger)
{
// Also debug scopes, if debug logging is enabled
if (aLogger.isDebugEnabled ())
return true;
return (isLifeCycleDebuggingEnabled () || isDebugRequestScopeEnabled ()) && aLogger.isInfoEnabled ();
}