Java tutorial
/* * This file is part of Benedikt Ritter's utils library. * * Benedikt Ritter's utils library is free software: you can redistribute it * and/or modify it under the terms of the GNU General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Benedikt Ritter's utils library is distributed in the hope that it will * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Benedikt Ritter's utils library. * If not, see <http://www.gnu.org/licenses/>. */ package de.systemoutprintln.util.logging.spring; import java.lang.reflect.Method; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.ThrowsAdvice; /** * Logging intercepter for logging method invocations. * * @author Benedikt Ritter * */ public class LoggingInterceptor implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice { private static Logger fLog = null; public LoggingInterceptor() { } /* * (non-Javadoc) * * @see * org.springframework.aop.AfterReturningAdvice#afterReturning(java.lang * .Object, java.lang.reflect.Method, java.lang.Object[], java.lang.Object) */ @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { fLog = LoggerFactory.getLogger(target.getClass()); fLog.info("Finished method: {} with parameters: {}. Returning: {}", ArrayUtils.toArray(method.getName(), args, target)); } /* * (non-Javadoc) * * @see * org.springframework.aop.MethodBeforeAdvice#before(java.lang.reflect.Method * , java.lang.Object[], java.lang.Object) */ @Override public void before(Method method, Object[] args, Object target) throws Throwable { fLog = LoggerFactory.getLogger(target.getClass()); fLog.info("Entering method: {} with parameters: {}", method.getName(), args); } /** * Method for logging throwables. * * @param method * the method that was invoked. * @param args * the arguments, the method was invoked with. * @param target * the object, on which the method was invoked. * @param throwable * the throwable that was thrown during method invocation. * * @see {@link org.springframework.aop.ThrowsAdvice} */ public void afterThrowing(Method method, Object[] args, Object target, Throwable throwable) { fLog = LoggerFactory.getLogger(target.getClass()); fLog.info("Method invocation resulted in exepction! Method: {} parameters: {} stack trace: {}", ArrayUtils.toArray(method.getName(), args, ExceptionUtils.getRootCauseStackTrace(throwable))); } }