001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.logging.Level;
023    import java.util.logging.Logger;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class JDKLoggerTestUtil {
029    
030            public static CaptureHandler configureJDKLogger(String name, Level level) {
031                    LogWrapper logWrapper = (LogWrapper)LogFactoryUtil.getLog(name);
032    
033                    Log log = logWrapper.getWrappedLog();
034    
035                    if (!(log instanceof Jdk14LogImpl)) {
036                            throw new IllegalStateException(
037                                    "Log " + name + " is not a JDK logger");
038                    }
039    
040                    Jdk14LogImpl jdk14LogImpl = (Jdk14LogImpl)log;
041    
042                    Logger logger = jdk14LogImpl.getWrappedLogger();
043    
044                    CaptureHandler captureHandler = new CaptureHandler(logger, level);
045    
046                    logger.addHandler(captureHandler);
047    
048                    return captureHandler;
049            }
050    
051            static {
052    
053                    // See LPS-32051 and LPS-32471
054    
055                    LogFactoryUtil.getLog(JDKLoggerTestUtil.class);
056            }
057    
058    }