it.tidalwave.netbeans.util.test.TestLoggerSetup Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* OpenBlueSky - NetBeans Platform Enhancements
* ============================================
*
* Copyright (C) 2007-2010 by Tidalwave s.a.s.
* Project home page: http://openbluesky.kenai.com
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* $Id: TestLoggerSetup.java,v f17245cd751a 2010/03/14 17:14:28 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.netbeans.util.test;
import javax.annotation.Nonnull;
import java.util.logging.Handler;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.io.File;
import java.io.InputStream;
import it.tidalwave.util.logging.SingleLineLogFormatter;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: TestLoggerSetup.java,v f17245cd751a 2010/03/14 17:14:28 fabrizio $
*
**********************************************************************************************************************/
public final class TestLoggerSetup
{
public static void setupLogging (final @Nonnull Class> clazz)
{
try
{
new File("target/logs").mkdirs();
final InputStream is = clazz.getResourceAsStream("log.properties");
if (is != null)
{
System.err.println("NO LOGGING CONFIGURATION");
LogManager.getLogManager().readConfiguration(is);
is.close();
}
//
// The formatter must be set programmatically as the property in the log.properties won't
// be honored. I suspect it is related with NetBeans module classloaders as the formatter
// is loaded inside LogManager by using the SystemClassLoader, which only sees the classpath.
//
final SingleLineLogFormatter formatter = new SingleLineLogFormatter();
Logger rootLogger = Logger.getLogger(TestLoggerSetup.class.getName());
while (rootLogger.getParent() != null)
{
rootLogger = rootLogger.getParent();
}
for (final Handler handler : rootLogger.getHandlers())
{
handler.setFormatter(formatter);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}