001// Copyright 2008, 2011 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.ioc.internal.services;
016
017import org.apache.tapestry5.ioc.services.ExceptionTracker;
018import org.apache.tapestry5.plastic.MethodAdvice;
019import org.apache.tapestry5.plastic.MethodInvocation;
020import org.slf4j.Logger;
021
022public class LoggingAdvice implements MethodAdvice
023{
024    private final MethodLogger methodLogger;
025
026    public LoggingAdvice(Logger logger, ExceptionTracker exceptionTracker)
027    {
028        methodLogger = new MethodLogger(logger, exceptionTracker);
029    }
030
031    @Override
032    public void advise(MethodInvocation invocation)
033    {
034        boolean debug = methodLogger.isDebugEnabled();
035
036        if (!debug)
037        {
038            invocation.proceed();
039            return;
040        }
041
042        methodLogger.entry(invocation);
043
044        try
045        {
046            invocation.proceed();
047        }
048        catch (RuntimeException ex)
049        {
050            methodLogger.fail(invocation, ex);
051
052            throw ex;
053        }
054
055        if (invocation.didThrowCheckedException())
056        {
057            Exception thrown = invocation.getCheckedException(Exception.class);
058
059            methodLogger.fail(invocation, thrown);
060
061            return;
062        }
063
064        methodLogger.exit(invocation);
065    }
066}