View Javadoc
1   package org.codehaus.plexus.logging.console;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.codehaus.plexus.logging.AbstractLoggerManager;
20  import org.codehaus.plexus.logging.Logger;
21  import org.codehaus.plexus.logging.LoggerManager;
22  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
23  
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Locale;
27  import java.util.Map;
28  
29  /**
30   * This is a simple logger manager that will only write the logging statements to the console.
31   *
32   * Sample configuration:
33   * <pre>
34   * <logging>
35   *   <implementation>org.codehaus.plexus.logging.ConsoleLoggerManager</implementation>
36   *   <logger>
37   *     <threshold>DEBUG</threshold>
38   *   </logger>
39   * </logging>
40   * </pre>
41   * 
42   * @author Jason van Zyl
43   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
44   * @version $Id$
45   */
46  public class ConsoleLoggerManager
47      extends AbstractLoggerManager
48      implements LoggerManager, Initializable
49  {
50      /**
51       * Message of this level or higher will be logged. 
52       * 
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 Map loggers;
61  
62      /** The number of active loggers in use. */
63      private int loggerCount;
64  
65      private boolean bootTimeLogger = false;
66  
67      public ConsoleLoggerManager()
68      {
69      }
70  
71      /**
72       * This special constructor is called directly when the container is bootstrapping itself.
73       */
74      public ConsoleLoggerManager( String threshold )
75      {
76          this.threshold = threshold;
77  
78          bootTimeLogger = true;
79  
80          initialize();
81      }
82  
83      public void initialize()
84      {
85          debug( "Initializing ConsoleLoggerManager: " + this.hashCode() + "." );
86  //        if ( !bootTimeLogger )
87  //            new Throwable().printStackTrace(System.err);
88          currentThreshold = parseThreshold( threshold );
89  
90          if ( currentThreshold == -1 )
91          {
92              debug( "Could not parse the threshold level: '" + threshold + "', setting to debug." );
93              currentThreshold = Logger.LEVEL_DEBUG;
94          }
95  
96          loggers = new HashMap();
97      }
98  
99      public void setThreshold( int currentThreshold )
100     {
101         this.currentThreshold = currentThreshold;
102     }
103 
104     public void setThresholds( int currentThreshold )
105     {
106         this.currentThreshold = currentThreshold;
107 
108         for (Object o : loggers.values()) {
109             Logger logger = (Logger) o;
110             logger.setThreshold(currentThreshold);
111         }
112     }
113 
114     /**
115      * @return Returns the threshold.
116      */
117     public int getThreshold()
118     {
119         return currentThreshold;
120     }
121 
122     // new stuff
123 
124     public void setThreshold( String role, String roleHint, int threshold ) {
125         ConsoleLogger logger;
126         String name;
127 
128         name = toMapKey( role, roleHint );
129         logger = (ConsoleLogger)loggers.get( name );
130 
131         if(logger == null) {
132             debug( "Trying to set the threshold of a unknown logger '" + name + "'." );
133             return; // nothing to do
134         }
135 
136         logger.setThreshold( threshold );
137     }
138 
139     public int getThreshold( String role, String roleHint ) {
140         ConsoleLogger logger;
141         String name;
142 
143         name = toMapKey( role, roleHint );
144         logger = (ConsoleLogger)loggers.get( name );
145 
146         if(logger == null) {
147             debug( "Trying to get the threshold of a unknown logger '" + name + "'." );
148             return Logger.LEVEL_DEBUG; // does not return null because that could create a NPE
149         }
150 
151         return logger.getThreshold();
152     }
153 
154     public Logger createLogger(int threshold, String name)
155     {
156         return new ConsoleLogger( threshold, name );
157     }
158 
159     public Logger getLoggerForComponent( String role, String roleHint )
160     {
161         Logger logger;
162         String name;
163 
164         name = toMapKey( role, roleHint );
165         logger = (Logger)loggers.get( name );
166 
167         if ( logger != null )
168             return logger;
169 
170         debug( "Creating logger '" + name + "' " + this.hashCode() + "." );
171         logger = createLogger( getThreshold(), name );
172         loggers.put( name, logger );
173 
174         return logger;
175     }
176 
177     public void returnComponentLogger( String role, String roleHint )
178     {
179         Object obj;
180         String name;
181 
182         name = toMapKey( role, roleHint );
183         obj = loggers.remove( name );
184 
185         if ( obj == null )
186         {
187             debug( "There was no such logger '" + name + "' " + this.hashCode() + ".");
188         }
189         else
190         {
191             debug( "Removed logger '" + name + "' " + this.hashCode() + ".");
192         }
193     }
194 
195     public int getActiveLoggerCount()
196     {
197         return loggers.size();
198     }
199 
200     private int parseThreshold( String text )
201     {
202         text = text.trim().toLowerCase( Locale.ENGLISH );
203 
204         if ( text.equals( "debug" ) )
205         {
206             return ConsoleLogger.LEVEL_DEBUG;
207         }
208         else if ( text.equals( "info" ) )
209         {
210             return ConsoleLogger.LEVEL_INFO;
211         }
212         else if ( text.equals( "warn" ) )
213         {
214             return ConsoleLogger.LEVEL_WARN;
215         }
216         else if ( text.equals( "error" ) )
217         {
218             return ConsoleLogger.LEVEL_ERROR;
219         }
220         else if ( text.equals( "fatal" ) )
221         {
222             return ConsoleLogger.LEVEL_FATAL;
223         }
224 
225         return -1;
226     }
227 
228     private String decodeLogLevel( int logLevel )
229     {
230         switch(logLevel) {
231         case ConsoleLogger.LEVEL_DEBUG: return "debug";
232         case ConsoleLogger.LEVEL_INFO: return "info";
233         case ConsoleLogger.LEVEL_WARN: return "warn";
234         case ConsoleLogger.LEVEL_ERROR: return "error";
235         case ConsoleLogger.LEVEL_FATAL: return "fatal";
236         case ConsoleLogger.LEVEL_DISABLED: return "disabled";
237         default: return "unknown";
238         }
239     }
240 
241     /**
242      * Remove this method and all references when this code is verified.
243      *
244      * @param msg
245      */
246     private void debug( String msg )
247     {
248 //        if ( !bootTimeLogger )
249 //            System.out.println( "[Console] " + msg );
250     }
251 }