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.log4j;
17  
18  import org.apache.log4j.Level;
19  import org.apache.log4j.Logger;
20  import org.apache.maven.plugin.logging.Log;
21  
22  /**
23   * Configures the log4j framework with the logging information.
24   *
25   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
26   * @version $Revision:591 $
27   */
28  public class Log4jConfigurator
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    /**
35     * Default constructor.
36     *
37     */
38    public Log4jConfigurator()
39    {
40    }
41  
42    // --- members --------------------------------------------------------------
43  
44    // ****************************** Initializer *******************************
45  
46    // ****************************** Constructors ******************************
47  
48    /**
49     * Configures the log4j logger.
50     *
51     * @param log the Maven logger to use for logging.
52     * @param level the new level to set the log4j logging system to.
53     */
54    public void configure(final Log log, final String level)
55    {
56      try
57      {
58        final Logger root = Logger.getRootLogger();
59        final Level log4jLevel = determineLog4jLevel(level);
60        root.setLevel(log4jLevel);
61      }
62      catch (final Exception e)
63      {
64        if (log.isWarnEnabled())
65        {
66          log.warn("Cannot configure log4j logger.", e);
67        }
68      }
69    }
70  
71    private Level determineLog4jLevel(final String level)
72    {
73      if("FINEST".equals(level))
74      {
75        return Level.TRACE;
76      }
77      else if("FINER".equals(level) || "FINE".equals(level))
78      {
79        return Level.DEBUG;
80      }
81      else if("WARNING".equals(level))
82      {
83        return Level.WARN;
84      }
85      else
86      {
87        return Level.toLevel(level);
88      }
89    }
90  
91    // ****************************** Inner Classes *****************************
92  
93    // ********************************* Methods ********************************
94  
95    // --- init -----------------------------------------------------------------
96  
97    // --- get&set --------------------------------------------------------------
98  
99    // --- business -------------------------------------------------------------
100 
101   // --- object basics --------------------------------------------------------
102 
103 }