001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.test.rule;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.test.rule.callback.LogAssertionTestCallback;
019    
020    import org.apache.log4j.AppenderSkeleton;
021    import org.apache.log4j.Level;
022    import org.apache.log4j.spi.LoggingEvent;
023    import org.apache.log4j.spi.ThrowableInformation;
024    
025    /**
026     * @author William Newbury
027     */
028    public class LogAssertionAppender extends AppenderSkeleton {
029    
030            public static final LogAssertionAppender INSTANCE =
031                    new LogAssertionAppender();
032    
033            @Override
034            public void close() {
035            }
036    
037            @Override
038            public boolean requiresLayout() {
039                    return false;
040            }
041    
042            @Override
043            protected void append(LoggingEvent loggingEvent) {
044                    Level level = loggingEvent.getLevel();
045    
046                    if (level.equals(Level.ERROR) || level.equals(Level.FATAL)) {
047                            StringBundler sb = new StringBundler(6);
048    
049                            sb.append("{level=");
050                            sb.append(loggingEvent.getLevel());
051                            sb.append(", loggerName=");
052                            sb.append(loggingEvent.getLoggerName());
053                            sb.append(", message=");
054                            sb.append(loggingEvent.getMessage());
055    
056                            ThrowableInformation throwableInformation =
057                                    loggingEvent.getThrowableInformation();
058    
059                            LogAssertionTestCallback.caughtFailure(
060                                    new AssertionError(
061                                            sb.toString(), throwableInformation.getThrowable()));
062                    }
063            }
064    
065    }