View Javadoc

1   package org.apache.maven.cli;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Locale;
23  
24  import org.codehaus.plexus.logging.AbstractLoggerManager;
25  import org.codehaus.plexus.logging.Logger;
26  import org.codehaus.plexus.logging.LoggerManager;
27  import org.codehaus.plexus.logging.console.ConsoleLogger;
28  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
29  
30  /**
31   * This is a simple logger manager that will only write the logging statements to the console.
32   * <p/>
33   * Sample configuration:
34   * <pre>
35   * <logging>
36   *   <implementation>org.codehaus.plexus.logging.ConsoleLoggerManager</implementation>
37   *   <logger>
38   *     <threshold>DEBUG</threshold>
39   *   </logger>
40   * </logging>
41   * </pre>
42   *
43   * @author Jason van Zyl
44   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
45   */
46  public class MavenLoggerManager
47      extends AbstractLoggerManager
48      implements LoggerManager, Initializable
49  {
50      /**
51       * Message of this level or higher will be logged.
52       * <p/>
53       * This field is set by the plexus container thus the name is 'threshold'. The field
54       * currentThreshold contains the current setting of the threshold.
55       */
56      private String threshold = "info";
57  
58      private int currentThreshold;
59  
60      private Logger logger;
61  
62      public MavenLoggerManager( Logger logger )
63      {
64          this.logger = logger;
65      }
66  
67      public void initialize()
68      {
69          debug( "Initializing ConsoleLoggerManager: " + this.hashCode() + "." );
70  
71          currentThreshold = parseThreshold( threshold );
72  
73          if ( currentThreshold == -1 )
74          {
75              debug( "Could not parse the threshold level: '" + threshold + "', setting to debug." );
76              currentThreshold = Logger.LEVEL_DEBUG;
77          }
78      }
79  
80      public void setThreshold( int currentThreshold )
81      {
82          this.currentThreshold = currentThreshold;
83      }
84  
85      public void setThresholds( int currentThreshold )
86      {
87          this.currentThreshold = currentThreshold;
88  
89          logger.setThreshold( currentThreshold );
90      }
91  
92      /** @return Returns the threshold. */
93      public int getThreshold()
94      {
95          return currentThreshold;
96      }
97  
98      public void setThreshold( String role,
99                                String roleHint,
100                               int threshold )
101     {
102     }
103 
104     public int getThreshold( String role,
105                              String roleHint )
106     {
107         return currentThreshold;
108     }
109 
110     public Logger getLoggerForComponent( String role,
111                                          String roleHint )
112     {
113         return logger;
114     }
115 
116     public void returnComponentLogger( String role,
117                                        String roleHint )
118     {
119     }
120 
121     public int getActiveLoggerCount()
122     {
123         return 1;
124     }
125 
126     private int parseThreshold( String text )
127     {
128         text = text.trim().toLowerCase( Locale.ENGLISH );
129 
130         if ( text.equals( "debug" ) )
131         {
132             return ConsoleLogger.LEVEL_DEBUG;
133         }
134         else if ( text.equals( "info" ) )
135         {
136             return ConsoleLogger.LEVEL_INFO;
137         }
138         else if ( text.equals( "warn" ) )
139         {
140             return ConsoleLogger.LEVEL_WARN;
141         }
142         else if ( text.equals( "error" ) )
143         {
144             return ConsoleLogger.LEVEL_ERROR;
145         }
146         else if ( text.equals( "fatal" ) )
147         {
148             return ConsoleLogger.LEVEL_FATAL;
149         }
150 
151         return -1;
152     }
153 
154     /**
155      * Remove this method and all references when this code is verified.
156      *
157      * @param msg
158      */
159     private void debug( String msg )
160     {
161     }
162 }