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.log;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    
019    import org.apache.log4j.AppenderSkeleton;
020    import org.apache.log4j.Level;
021    import org.apache.log4j.spi.LoggingEvent;
022    import org.apache.log4j.spi.ThrowableInformation;
023    
024    /**
025     * @author William Newbury
026     */
027    public class LogAssertionAppender extends AppenderSkeleton {
028    
029            public static final LogAssertionAppender INSTANCE =
030                    new LogAssertionAppender();
031    
032            @Override
033            public void close() {
034            }
035    
036            @Override
037            public boolean requiresLayout() {
038                    return false;
039            }
040    
041            @Override
042            protected void append(LoggingEvent loggingEvent) {
043                    Level level = loggingEvent.getLevel();
044    
045                    if (level.equals(Level.ERROR) || level.equals(Level.FATAL)) {
046                            StringBundler sb = new StringBundler(6);
047    
048                            sb.append("{level=");
049                            sb.append(loggingEvent.getLevel());
050                            sb.append(", loggerName=");
051                            sb.append(loggingEvent.getLoggerName());
052                            sb.append(", message=");
053                            sb.append(loggingEvent.getMessage());
054    
055                            ThrowableInformation throwableInformation =
056                                    loggingEvent.getThrowableInformation();
057    
058                            LogAssertionTestRule.caughtFailure(
059                                    new AssertionError(
060                                            sb.toString(), throwableInformation.getThrowable()));
061                    }
062            }
063    
064    }