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