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.Jdk14LogImpl;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.log.LogWrapper;
021    
022    import java.util.List;
023    import java.util.concurrent.CopyOnWriteArrayList;
024    import java.util.logging.Handler;
025    import java.util.logging.Level;
026    import java.util.logging.LogRecord;
027    import java.util.logging.Logger;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class JDKLoggerTestUtil {
033    
034            public static List<LogRecord> configureJDKLogger(String name, Level level) {
035                    LogWrapper logWrapper = (LogWrapper)LogFactoryUtil.getLog(name);
036    
037                    Log log = logWrapper.getWrappedLog();
038    
039                    if (!(log instanceof Jdk14LogImpl)) {
040                            throw new IllegalStateException(
041                                    "Log " + name + " is not a JDK logger");
042                    }
043    
044                    Jdk14LogImpl jdk14LogImpl = (Jdk14LogImpl)log;
045    
046                    Logger logger = jdk14LogImpl.getWrappedLogger();
047    
048                    for (Handler handler : logger.getHandlers()) {
049                            logger.removeHandler(handler);
050                    }
051    
052                    logger.setLevel(level);
053                    logger.setUseParentHandlers(false);
054    
055                    CaptureHandler captureHandler = new CaptureHandler();
056    
057                    logger.addHandler(captureHandler);
058    
059                    return captureHandler._logRecords;
060            }
061    
062            private static class CaptureHandler extends Handler {
063    
064                    @Override
065                    public void close() throws SecurityException {
066                            _logRecords.clear();
067                    }
068    
069                    @Override
070                    public void flush() {
071                            _logRecords.clear();
072                    }
073    
074                    @Override
075                    public boolean isLoggable(LogRecord logRecord) {
076                            return false;
077                    }
078    
079                    @Override
080                    public void publish(LogRecord logRecord) {
081                            _logRecords.add(logRecord);
082                    }
083    
084                    private List<LogRecord> _logRecords =
085                            new CopyOnWriteArrayList<LogRecord>();
086    
087            }
088    
089            static {
090    
091                    // See LPS-32051 and LPS-32471
092    
093                    LogFactoryUtil.getLog(JDKLoggerTestUtil.class);
094            }
095    
096    }