Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2013 smartics, Kronseder & Reiner GmbH
   *
   * 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.
  */
 package com.redhat.rcm.maven.plugin.buildmetadata.util;
 
 
Utility class for configuring loggers in Maven.

Author(s):
Robert Reiner
Version:
$Revision:591 $
 
 public final class LoggingUtils
 {
   // ********************************* Fields *********************************
 
   // --- constants ------------------------------------------------------------
 
   // --- members --------------------------------------------------------------
 
   // ****************************** Initializer *******************************
 
   // ****************************** Constructors ******************************
 
  
Utility class pattern.
 
   private LoggingUtils()
   {
   }
 
   // ****************************** Inner Classes *****************************
 
   // ********************************* Methods ********************************
 
   // --- init -----------------------------------------------------------------
 
   // --- get&set --------------------------------------------------------------
 
   // --- business -------------------------------------------------------------
 
  
Configures the Java logging system.

Parameters:
log the Maven logger.
logLevel the log4j level to log.
Throws:
java.lang.NullPointerException if log is null.
 
   public static void configureLogger(final Log logfinal String logLevel)
     throws NullPointerException
   {
     final String newLevel;
     if (!isBlank(logLevel))
     {
       newLevel = logLevel;
       if (log.isDebugEnabled())
       {
         log.debug("Using configured level " + newLevel);
       }
     }
     else
     {
       newLevel = determineLevel(log);
       if (log.isDebugEnabled())
       {
         log.debug("Using Maven level " + newLevel);
       }
     }
     configure(lognewLevel);
   }
 
   private static void configure(final Log logfinal String newLevel)
   {
     if (isAvailable("org.apache.log4j.Logger"))
     {
       runLog4jConfigure(lognewLevel);
     }
 
     new SunLoggerConfigurator().configure(lognewLevel);
   }
 
   private static void runLog4jConfigure(final Log logfinal String newLevel)
   {
    try
    {
      final Class<?> clazz =
          Class.forName("com.redhat.rcm.maven.util.log4j.Log4jConfigurator");
      final Object instance = clazz.newInstance();
      final Method method =
          clazz.getMethod("configure"Log.classString.class);
      method.invoke(instancelognewLevel);
    }
    catch (final Exception e)
    {
      if (log.isWarnEnabled())
      {
        log.warn("Cannot configure log4j logger."e);
      }
    }
  }
  private static boolean isAvailable(final String className)
  {
    try
    {
      Class.forName(className);
      return true;
    }
    catch (final Exception e)
    {
      return false;
    }
  }

  
Checks if the given string is blank or not.

Implemented to not require to include commons-lang.

Parameters:
value the value to check.
Returns:
true if the value is null, empty or contains only whitespaces.
  private static boolean isBlank(final String logLevel)
  {
    return (logLevel == null || "".equals(logLevel.trim()));
  }

  
Determines the level of the Maven logger.

Parameters:
log the Maven logger to request the log level from.
Returns:
the log level for Java logging as java.lang.String.
  private static String determineLevel(final Log log)
  {
    if (log.isDebugEnabled())
    {
      return ..toString();
    }
    else if (log.isInfoEnabled())
    {
      return ..toString();
    }
    else if (log.isWarnEnabled())
    {
      return ..toString();
    }
    else
    {
      return ..toString();
    }
  }
  // --- object basics --------------------------------------------------------
New to GrepCode? Check out our FAQ X