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    
015    package org.apache.tapestry5.ioc.internal.services;
016    
017    import org.apache.tapestry5.ioc.services.ExceptionTracker;
018    import org.apache.tapestry5.plastic.MethodAdvice;
019    import org.apache.tapestry5.plastic.MethodInvocation;
020    import org.slf4j.Logger;
021    
022    public 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        public void advise(MethodInvocation invocation)
032        {
033            boolean debug = methodLogger.isDebugEnabled();
034    
035            if (!debug)
036            {
037                invocation.proceed();
038                return;
039            }
040    
041            methodLogger.entry(invocation);
042    
043            try
044            {
045                invocation.proceed();
046            }
047            catch (RuntimeException ex)
048            {
049                methodLogger.fail(invocation, ex);
050    
051                throw ex;
052            }
053    
054            if (invocation.didThrowCheckedException())
055            {
056                Exception thrown = invocation.getCheckedException(Exception.class);
057    
058                methodLogger.fail(invocation, thrown);
059    
060                return;
061            }
062    
063            methodLogger.exit(invocation);
064        }
065    }