View Javadoc

1   /*
2    * Copyright 2006-2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.util;
17  
18  import java.lang.reflect.Method;
19  import java.util.logging.Level;
20  
21  import org.apache.maven.plugin.logging.Log;
22  
23  /**
24   * Utility class for configuring loggers in Maven.
25   *
26   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
27   * @version $Revision:591 $
28   */
29  public final class LoggingUtils
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    // --- members --------------------------------------------------------------
36  
37    // ****************************** Initializer *******************************
38  
39    // ****************************** Constructors ******************************
40  
41    /**
42     * Utility class pattern.
43     */
44    private LoggingUtils()
45    {
46    }
47  
48    // ****************************** Inner Classes *****************************
49  
50    // ********************************* Methods ********************************
51  
52    // --- init -----------------------------------------------------------------
53  
54    // --- get&set --------------------------------------------------------------
55  
56    // --- business -------------------------------------------------------------
57  
58    /**
59     * Configures the Java logging system.
60     *
61     * @param log the Maven logger.
62     * @throws NullPointerException if <code>log</code> is <code>null</code>.
63     */
64    public static void configureLogger(final Log log, final String logLevel)
65    {
66      final String newLevel;
67      if (!isBlank(logLevel))
68      {
69        newLevel = logLevel;
70        if (log.isDebugEnabled())
71        {
72          log.debug("Using configured level " + newLevel);
73        }
74      }
75      else
76      {
77        newLevel = determineLevel(log);
78        if (log.isDebugEnabled())
79        {
80          log.debug("Using Maven level " + newLevel);
81        }
82      }
83      configure(log, newLevel);
84    }
85  
86    private static void configure(final Log log, final String newLevel)
87    {
88      if (isAvailable("org.apache.log4j.Logger"))
89      {
90        runLog4jConfigure(log, newLevel);
91      }
92  
93      new SunLoggerConfigurator().configure(log, newLevel);
94    }
95  
96    private static void runLog4jConfigure(final Log log, final String newLevel)
97    {
98      try
99      {
100       final Class<?> clazz =
101           Class.forName("de.smartics.maven.util.log4j.Log4jConfigurator");
102       final Object instance = clazz.newInstance();
103       final Method method =
104           clazz.getMethod("configure", Log.class, String.class);
105       method.invoke(instance, log, newLevel);
106     }
107     catch (final Exception e)
108     {
109       if (log.isWarnEnabled())
110       {
111         log.warn("Cannot configure log4j logger.", e);
112       }
113     }
114   }
115 
116   private static boolean isAvailable(final String className)
117   {
118     try
119     {
120       Class.forName(className);
121       return true;
122     }
123     catch (final Exception e)
124     {
125       return false;
126     }
127   }
128 
129   /**
130    * Checks if the given string is blank or not.
131    * <p>
132    * Implemented to not require to include commons-lang.
133    * </p>
134    *
135    * @param value the value to check.
136    * @return <code>true</code> if the <code>value</code> is <code>null</code>,
137    *         empty or contains only whitespaces.
138    */
139   private static boolean isBlank(final String logLevel)
140   {
141     return (logLevel == null || "".equals(logLevel.trim()));
142   }
143 
144   /**
145    * Determines the level of the Maven logger.
146    *
147    * @param log the Maven logger to request the log level from.
148    * @return the log level for Java logging as {@link String}.
149    */
150   private static String determineLevel(final Log log)
151   {
152     if (log.isDebugEnabled())
153     {
154       return Level.FINEST.toString();
155     }
156     else if (log.isInfoEnabled())
157     {
158       return Level.INFO.toString();
159     }
160     else if (log.isWarnEnabled())
161     {
162       return Level.WARNING.toString();
163     }
164     else
165     {
166       return Level.SEVERE.toString();
167     }
168   }
169 
170   // --- object basics --------------------------------------------------------
171 
172 }