001/*
002 * Copyright 2002-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.aop.aspectj;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021import org.aspectj.bridge.AbortException;
022import org.aspectj.bridge.IMessage;
023import org.aspectj.bridge.IMessage.Kind;
024import org.aspectj.bridge.IMessageHandler;
025
026/**
027 * Implementation of AspectJ's {@link IMessageHandler} interface that
028 * routes AspectJ weaving messages through the same logging system as the
029 * regular Spring messages.
030 *
031 * <p>Pass the option...
032 *
033 * <p><code class="code">-XmessageHandlerClass:org.springframework.aop.aspectj.AspectJWeaverMessageHandler</code>
034 *
035 * <p>to the weaver; for example, specifying the following in a
036 * "{@code META-INF/aop.xml} file:
037 *
038 * <p><code class="code">&lt;weaver options="..."/&gt;</code>
039 *
040 * @author Adrian Colyer
041 * @author Juergen Hoeller
042 * @since 2.0
043 */
044public class AspectJWeaverMessageHandler implements IMessageHandler {
045
046        private static final String AJ_ID = "[AspectJ] ";
047
048        private static final Log logger = LogFactory.getLog("AspectJ Weaver");
049
050
051        @Override
052        public boolean handleMessage(IMessage message) throws AbortException {
053                Kind messageKind = message.getKind();
054                if (messageKind == IMessage.DEBUG) {
055                        if (logger.isDebugEnabled()) {
056                                logger.debug(makeMessageFor(message));
057                                return true;
058                        }
059                }
060                else if (messageKind == IMessage.INFO || messageKind == IMessage.WEAVEINFO) {
061                        if (logger.isInfoEnabled()) {
062                                logger.info(makeMessageFor(message));
063                                return true;
064                        }
065                }
066                else if (messageKind == IMessage.WARNING) {
067                        if (logger.isWarnEnabled()) {
068                                logger.warn(makeMessageFor(message));
069                                return true;
070                        }
071                }
072                else if (messageKind == IMessage.ERROR) {
073                        if (logger.isErrorEnabled()) {
074                                logger.error(makeMessageFor(message));
075                                return true;
076                        }
077                }
078                else if (messageKind == IMessage.ABORT) {
079                        if (logger.isFatalEnabled()) {
080                                logger.fatal(makeMessageFor(message));
081                                return true;
082                        }
083                }
084                return false;
085        }
086
087        private String makeMessageFor(IMessage aMessage) {
088                return AJ_ID + aMessage.getMessage();
089        }
090
091        @Override
092        public boolean isIgnoring(Kind messageKind) {
093                // We want to see everything, and allow configuration of log levels dynamically.
094                return false;
095        }
096
097        @Override
098        public void dontIgnore(Kind messageKind) {
099                // We weren't ignoring anything anyway...
100        }
101
102        @Override
103        public void ignore(Kind kind) {
104                // We weren't ignoring anything anyway...
105        }
106
107}