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.ReflectionUtil;
018    
019    import java.io.Closeable;
020    
021    import java.lang.reflect.Field;
022    
023    import java.util.List;
024    import java.util.concurrent.CopyOnWriteArrayList;
025    
026    import org.apache.log4j.AppenderSkeleton;
027    import org.apache.log4j.Category;
028    import org.apache.log4j.Level;
029    import org.apache.log4j.Logger;
030    import org.apache.log4j.spi.LoggingEvent;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class CaptureAppender extends AppenderSkeleton implements Closeable {
036    
037            public CaptureAppender(Logger logger) {
038                    _logger = logger;
039    
040                    _level = _logger.getLevel();
041    
042                    _parentCategory = logger.getParent();
043    
044                    try {
045                            _parentField.set(_logger, null);
046                    }
047                    catch (Exception e) {
048                            throw new RuntimeException(e);
049                    }
050            }
051    
052            @Override
053            public void close() {
054                    closed = true;
055    
056                    _logger.removeAppender(this);
057    
058                    _logger.setLevel(_level);
059    
060                    try {
061                            _parentField.set(_logger, _parentCategory);
062                    }
063                    catch (Exception e) {
064                            throw new RuntimeException(e);
065                    }
066            }
067    
068            public List<LoggingEvent> getLoggingEvents() {
069                    return _loggingEvents;
070            }
071    
072            @Override
073            public boolean requiresLayout() {
074                    return false;
075            }
076    
077            @Override
078            protected void append(LoggingEvent loggingEvent) {
079                    _loggingEvents.add(loggingEvent);
080            }
081    
082            private static final Field _parentField;
083    
084            static {
085                    try {
086                            _parentField = ReflectionUtil.getDeclaredField(
087                                    Category.class, "parent");
088                    }
089                    catch (Exception e) {
090                            throw new ExceptionInInitializerError(e);
091                    }
092            }
093    
094            private final Level _level;
095            private final Logger _logger;
096            private final List<LoggingEvent> _loggingEvents =
097                    new CopyOnWriteArrayList<>();
098            private final Category _parentCategory;
099    
100    }