org.grazy.experimental.graspectj.MethodLogger.java Source code

Java tutorial

Introduction

Here is the source code for org.grazy.experimental.graspectj.MethodLogger.java

Source

/*
 * Copyright 2014 David Leoni .
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.grazy.experimental.graspectj;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;

/**
 *
 * @author David Leoni
 */
@Aspect
public class MethodLogger {

    private static final Logger logger = Logger.getLogger("blabla");

    @Around("execution(* *(..)) && @annotation(Loggable)")
    public Object around(ProceedingJoinPoint point) {
        long start = System.currentTimeMillis();
        Object result;
        try {
            result = point.proceed();

            logger.log(Level.INFO, "source location: {0}", point.getSourceLocation().toString());
            logger.log(Level.INFO, "#%s(%s): %s in %[msec]s{0}{1}{2}{3}",
                    new Object[] { MethodSignature.class.cast(point.getSignature()).getMethod().getName(),
                            point.getArgs(), result, System.currentTimeMillis() - start });
            return result;
        } catch (Throwable ex) {
            Logger.getLogger(MethodLogger.class.getName()).log(Level.SEVERE, null, ex);
        }
        throw new RuntimeException("ma non scassare la minchia");
    }
}