001    /**
002     * Copyright (c) 2000-2013 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.kernel.test;
016    
017    import com.liferay.portal.kernel.log.LogFactoryUtil;
018    
019    import java.util.List;
020    import java.util.concurrent.CopyOnWriteArrayList;
021    import java.util.logging.Handler;
022    import java.util.logging.Level;
023    import java.util.logging.LogRecord;
024    import java.util.logging.Logger;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class JDKLoggerTestUtil {
030    
031            public static List<LogRecord> configureJDKLogger(String name, Level level) {
032                    Logger logger = Logger.getLogger(name);
033    
034                    for (Handler handler : logger.getHandlers()) {
035                            logger.removeHandler(handler);
036                    }
037    
038                    logger.setLevel(level);
039                    logger.setUseParentHandlers(false);
040    
041                    CaptureHandler captureHandler = new CaptureHandler();
042    
043                    logger.addHandler(captureHandler);
044    
045                    return captureHandler._logRecords;
046            }
047    
048            private static class CaptureHandler extends Handler {
049    
050                    @Override
051                    public void close() throws SecurityException {
052                            _logRecords.clear();
053                    }
054    
055                    @Override
056                    public void flush() {
057                            _logRecords.clear();
058                    }
059    
060                    @Override
061                    public boolean isLoggable(LogRecord logRecord) {
062                            return false;
063                    }
064    
065                    @Override
066                    public void publish(LogRecord logRecord) {
067                            _logRecords.add(logRecord);
068                    }
069    
070                    private List<LogRecord> _logRecords =
071                            new CopyOnWriteArrayList<LogRecord>();
072    
073            }
074    
075            static {
076    
077                    // See LPS-32051 and LPS-32471
078    
079                    LogFactoryUtil.getLog(JDKLoggerTestUtil.class);
080            }
081    
082    }