Java tutorial
package net.maxiebyte.voxel.aspects; /* This file is part of Voxel. Copyright 2015 Maxime Devos Voxel 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. Voxel 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 Voxel. If not, see <http://www.gnu.org/licenses/>. */ import java.lang.reflect.Method; import java.util.EnumMap; import java.util.Map; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static net.maxiebyte.voxel.function.MFunction.*; /** * * @author Maxime Devos */ @Aspect public class LoggableAspect { private final static Map<Loggable.Level, TriConsumer<Logger, String, String>> ENTERING_LEVEL_HANDLERS = new EnumMap<>( Loggable.Level.class); static { ENTERING_LEVEL_HANDLERS.put(Loggable.Level.DEBUG, (logger, pattern, name) -> { logger.debug(pattern, name); }); ENTERING_LEVEL_HANDLERS.put(Loggable.Level.ERROR, (logger, pattern, name) -> { logger.error(pattern, name); }); ENTERING_LEVEL_HANDLERS.put(Loggable.Level.INFO, (logger, pattern, name) -> { logger.info(pattern, name); }); ENTERING_LEVEL_HANDLERS.put(Loggable.Level.TRACE, (logger, pattern, name) -> { logger.trace(pattern, name); }); ENTERING_LEVEL_HANDLERS.put(Loggable.Level.WARN, (logger, pattern, name) -> { logger.warn(pattern, name); }); } private final static Map<Loggable.Level, TriObjectLongConsumer<Logger, String, String>> EXITING_LEVEL_HANDLERS = new EnumMap<>( Loggable.Level.class); static { EXITING_LEVEL_HANDLERS.put(Loggable.Level.DEBUG, (logger, pattern, name, value) -> { logger.debug(pattern, name, ((double) value) / 1000); }); EXITING_LEVEL_HANDLERS.put(Loggable.Level.ERROR, (logger, pattern, name, value) -> { logger.error(pattern, name, ((double) value) / 1000); }); EXITING_LEVEL_HANDLERS.put(Loggable.Level.INFO, (logger, pattern, name, value) -> { logger.info(pattern, name, ((double) value) / 1000); }); EXITING_LEVEL_HANDLERS.put(Loggable.Level.TRACE, (logger, pattern, name, value) -> { logger.trace(pattern, name, ((double) value) / 1000); }); EXITING_LEVEL_HANDLERS.put(Loggable.Level.WARN, (logger, pattern, name, value) -> { logger.warn(pattern, name, ((double) value) / 1000); }); } @Around("execution(* *(..)) && @annotation(net.maxiebyte.voxel.aspects.Loggable)") public Object wrapMethod(ProceedingJoinPoint point) throws Throwable { Method method = ((MethodSignature) point.getSignature()).getMethod(); Loggable loggableAnnotation = method.getAnnotation(Loggable.class); Loggable.Level level = loggableAnnotation.level(); Logger logger = LoggerFactory.getLogger(method.getDeclaringClass()); ENTERING_LEVEL_HANDLERS.get(level).accept(logger, loggableAnnotation.enteringPattern(), method.getName()); long time = System.currentTimeMillis(); Object result = point.proceed(); EXITING_LEVEL_HANDLERS.get(level).accept(logger, loggableAnnotation.exitingPattern(), method.getName(), System.currentTimeMillis() - time); return result; } }