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.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    }