All Downloads are FREE. Search and download functionalities are using the official Maven repository.

im.aop.loggers.util.LoggerUtil Maven / Gradle / Ivy

Go to download

A handy and configurable sets of annotation-based loggers for Spring Boot that can log every execution of a method when entering or exiting normally or abnormally, without you writing a single line of code using aspect-oriented programming (AOP)

The newest version!
package im.aop.loggers.util;

import im.aop.loggers.Level;
import org.aspectj.lang.JoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class LoggerUtil {

  private LoggerUtil() {
  }

  public static Logger getLogger(final Class declaringClass, final JoinPoint joinPoint) {
    return LoggerFactory.getLogger(
        declaringClass == null || declaringClass == void.class
            ? joinPoint.getSignature().getDeclaringType()
            : declaringClass);
  }

  public static boolean isEnabled(final Logger logger, final Level level) {
    switch (level) {
      case TRACE:
        return logger.isTraceEnabled();
      case DEBUG:
        return logger.isDebugEnabled();
      case WARN:
        return logger.isWarnEnabled();
      case INFO:
        return logger.isInfoEnabled();
      case ERROR:
        return logger.isErrorEnabled();
      default:
        return false;
    }
  }

  public static void log(final Logger logger, final Level level, final String message) {
    switch (level) {
      case TRACE:
        logger.trace(message);
        break;
      case DEBUG:
        logger.debug(message);
        break;
      case WARN:
        logger.warn(message);
        break;
      case INFO:
        logger.info(message);
        break;
      case ERROR:
        logger.error(message);
        break;
      default:
        break;
    }
  }

  public static void logException(
      final Logger logger, final Level level, final String message, final Throwable exception) {
    switch (level) {
      case TRACE:
        logger.trace(message, exception);
        break;
      case DEBUG:
        logger.debug(message, exception);
        break;
      case WARN:
        logger.warn(message, exception);
        break;
      case INFO:
        logger.info(message, exception);
        break;
      case ERROR:
        logger.error(message, exception);
        break;
      default:
        break;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy