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