View Javadoc
1   package org.codehaus.plexus.logging;
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.personality.plexus.lifecycle.phase.Initializable;
20  
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Locale;
24  import java.util.Map;
25  
26  /**
27   * Base class for all LoggerManagers which use cache of Loggers.
28   *
29   * @author <a href="mailto:michal@codehaus.org">Michal Maczka</a>
30   * @version $Id$
31   */
32  public abstract class BaseLoggerManager
33          extends AbstractLoggerManager implements Initializable
34  {
35      /** */
36      private Map loggerCache = new HashMap();
37  
38      private String threshold = "info";
39  
40      private int currentThreshold;
41  
42      public void initialize()
43      {
44          currentThreshold = parseThreshold( threshold );
45  
46          if ( currentThreshold == -1 )
47          {
48              currentThreshold = Logger.LEVEL_DEBUG;
49          }
50      }
51  
52      protected int parseThreshold( String text )
53      {
54          text = text.trim().toLowerCase( Locale.ENGLISH );
55  
56          if ( text.equals( "debug" ) )
57          {
58              return Logger.LEVEL_DEBUG;
59          }
60          else if ( text.equals( "info" ) )
61          {
62              return Logger.LEVEL_INFO;
63          }
64          else if ( text.equals( "warn" ) )
65          {
66              return Logger.LEVEL_WARN;
67          }
68          else if ( text.equals( "error" ) )
69          {
70              return Logger.LEVEL_ERROR;
71          }
72          else if ( text.equals( "fatal" ) )
73          {
74              return Logger.LEVEL_FATAL;
75          }
76  
77          return -1;
78      }
79  
80      /**
81       * Sets the threshold for all new loggers. It will NOT affect the existing loggers.
82       * <p/>
83       * This is usually only set once while the logger manager is configured.
84       *
85       * @param currentThreshold The new threshold.
86       */
87      public void setThreshold( int currentThreshold )
88      {
89          this.currentThreshold = currentThreshold;
90      }
91  
92      /**
93       * Sets the threshold for all new loggers. It will NOT affect the existing loggers.
94       * <p/>
95       * This is usually only set once while the logger manager is configured.
96       *
97       * @param currentThreshold The new threshold.
98       */
99      public void setThresholds( int currentThreshold )
100     {
101         this.currentThreshold = currentThreshold;
102 
103         for (Object o : loggerCache.values()) {
104             Logger logger = (Logger) o;
105             logger.setThreshold(currentThreshold);
106         }
107     }
108 
109     /**
110      * Returns the current threshold for all new loggers.
111      *
112      * @return Returns the current threshold for all new loggers.
113      */
114     public int getThreshold()
115     {
116         return currentThreshold;
117     }
118 
119     public void setThreshold( String role, String roleHint, int threshold )
120     {
121         AbstractLogger logger;
122 
123         String key = toMapKey( role, roleHint );
124 
125         logger = ( AbstractLogger ) loggerCache.get( key );
126 
127         if ( logger == null )
128         {
129             return; // nothing to do
130         }
131 
132         logger.setThreshold( threshold );
133     }
134 
135     public int getThreshold( String role, String roleHint )
136     {
137         AbstractLogger logger;
138 
139         String key = toMapKey( role, roleHint );
140 
141         logger = ( AbstractLogger ) loggerCache.get( key );
142 
143         if ( logger == null )
144         {
145             return Logger.LEVEL_DEBUG; // does not return null because that could create a NPE
146         }
147 
148         return logger.getThreshold();
149     }
150 
151     public Logger getLoggerForComponent( String role, String roleHint )
152     {
153         Logger logger;
154 
155         String key = toMapKey( role, roleHint );
156 
157         logger = ( Logger ) loggerCache.get( key );
158 
159         if ( logger != null )
160         {
161             return logger;
162         }
163 
164         logger = createLogger( key );
165 
166         loggerCache.put( key, logger );
167 
168         return logger;
169     }
170 
171     protected abstract Logger createLogger( String key );
172 
173     public void returnComponentLogger( String role, String roleHint )
174     {
175         Object obj;
176 
177         String key = toMapKey( role, roleHint );
178 
179         obj = loggerCache.remove( key );
180 
181         if ( obj == null )
182         {
183             // TODO: use a logger!
184             System.err.println( "There was no such logger '" + key + "' " + hashCode() + "." );
185         }
186     }
187 
188     public int getActiveLoggerCount()
189     {
190         return loggerCache.size();
191     }
192 
193     public String getThresholdAsString()
194     {
195         return threshold;
196     }
197 
198 }