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